本文整理汇总了C#中GZipStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# GZipStream.Write方法的具体用法?C# GZipStream.Write怎么用?C# GZipStream.Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GZipStream
的用法示例。
在下文中一共展示了GZipStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compress
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Compress(byte[] data)
{
using (var compressedStream = new MemoryStream())
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
{
zipStream.Write(data, 0, data.Length);
zipStream.Close();
return compressedStream.ToArray();
}
}
示例2: Compress
public static byte[] Compress(byte[] b)
{
MemoryStream ms = new MemoryStream();
GZipStream zs = new GZipStream(ms, CompressionMode.Compress);
zs.Write(b, 0, b.Length);
return ms.ToArray();
}
示例3: Compress
public static void Compress(string text, string outfile)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
string MimiBase64 = Convert.ToBase64String (gzBuffer);
File.WriteAllText (outfile, MimiBase64);
Console.WriteLine ("Base64 string saved as "+outfile+"\n");
//return Convert.ToBase64String (gzBuffer);
}
示例4: CompressedFiles
/// <summary>
/// Yields an Enumerable list of paths to GZTestData files
/// </summary>
public static IEnumerable<object[]> CompressedFiles()
{
if (_compressedFiles == null)
{
_compressedFiles = new List<object[]>();
// Crypto random data
byte[] bytes = new byte[100000000];
var rand = RandomNumberGenerator.Create();
rand.GetBytes(bytes);
string filePath = PerfUtils.GetTestFilePath() + ".gz";
using (FileStream output = File.Create(filePath))
using (GZipStream zip = new GZipStream(output, CompressionMode.Compress))
zip.Write(bytes, 0, bytes.Length);
_compressedFiles.Add(new object[] { filePath });
// Create a compressed file with repeated segments
bytes = Text.Encoding.UTF8.GetBytes(PerfUtils.CreateString(100000));
filePath = PerfUtils.GetTestFilePath() + ".gz";
using (FileStream output = File.Create(filePath))
using (GZipStream zip = new GZipStream(output, CompressionMode.Compress))
for (int i = 0; i < 1000; i++)
zip.Write(bytes, 0, bytes.Length);
_compressedFiles.Add(new object[] { filePath });
}
return _compressedFiles;
}
示例5: SliceGZip
private static void SliceGZip(string sourceFile, string destinationDir, int numParts)
{
FileInfo fileInfo = new FileInfo(sourceFile);
int size = (int)fileInfo.Length;
int partSize = size / numParts;
int lastPartSize = size - (numParts - 1) * partSize;
string name = fileInfo.Name;
string[] nameExtArr = name.Split('.');
FileStream source = new FileStream(sourceFile, FileMode.Open);
source.Seek(0, SeekOrigin.Current);
for (int i = 0; i < numParts; i++)
{
int currentSize = (i == (numParts - 1)) ? lastPartSize : partSize;
byte[] buffer = new byte[currentSize];
source.Read(buffer, 0, currentSize);
string currentFilePath = destinationDir + "Part-" + i + "." + nameExtArr[1] + ".gz";
using (FileStream dest = new FileStream(currentFilePath, FileMode.Create))
using (GZipStream gzipDest = new GZipStream(dest, CompressionMode.Compress, false))
gzipDest.Write(buffer, 0, currentSize);
}
source.Close();
}
示例6: SplitFile
public static void SplitFile(string inputFile, int parts, string path)
{
byte[] buffer = new byte[4096];
DirectoryInfo directorySelected = new DirectoryInfo(destination);
using (Stream originalFile = File.OpenRead(sourceFile))
{
int index = 1;
while (originalFile.Position < originalFile.Length)
{
using (Stream compressedFile = File.Create(path + "\\" + index + ".gz"))
{
using (GZipStream compression = new GZipStream(compressedFile, CompressionMode.Compress))
{
int chunkBytesRead = 0;
while (chunkBytesRead < originalFile.Length / parts)
{
int bytesRead = originalFile.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break;
}
chunkBytesRead += bytesRead;
compression.Write(buffer, 0, bytesRead);
}
}
}
index++;
}
}
}
示例7: Main
static void Main(string[] args)
{
if (args.Length < 1)
{
usage();
return;
}
else
{
string inputFile = args[0];
string outputFile = inputFile + ".gz";
try
{
// Get bytes from input stream
FileStream inFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, inputFile), FileMode.Open);
byte[] buffer = new byte[inFileStream.Length];
inFileStream.Read(buffer, 0, buffer.Length);
inFileStream.Close();
// Create GZip file stream and compress input bytes
FileStream outFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, outputFile), FileMode.Create);
GZipStream compressedStream = new GZipStream(outFileStream, CompressionMode.Compress);
compressedStream.Write(buffer, 0, buffer.Length);
compressedStream.Close();
outFileStream.Close();
Console.WriteLine("The file has been compressed. UR Da Bomb!!!");
}
catch (FileNotFoundException)
{
Console.WriteLine("Error: Specified file cannot be found.");
}
}
}
示例8: Compress
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
示例9: Slice
public static void Slice(string sourceFile, string destinationDirectory, int parts)
{
using (var source = new FileStream(sourceFile, FileMode.Open))
{
byte[] buffer = new byte[4096];
List<byte> bytes = new List<byte>();
while (true)
{
int readBytes = source.Read(buffer, 0, buffer.Length);
if (readBytes == 0)
{
break;
}
for (int i = 0; i < readBytes; i++)
{
bytes.Add(buffer[i]);
}
}
int partSize = bytes.Count / parts;
int leftOver = bytes.Count - partSize * parts;
for (int i = 0; i < parts; i++)
{
var newFile = destinationDirectory + "part-" + i + ".gz";
using (var copy = new FileStream(newFile, FileMode.Create))
{
using (var compressionStream = new GZipStream(copy, CompressionMode.Compress, false))
{
if (i == parts - 1)
{
compressionStream.Write(bytes.ToArray(), i * partSize, partSize + leftOver);
}
else
{
compressionStream.Write(bytes.ToArray(), i * partSize, partSize);
}
}
}
}
}
}
示例10: Compress
public static byte[] Compress(this byte[] bytes)
{
using (MemoryStream ms = new MemoryStream()) {
GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);
Compress.Write(bytes, 0, bytes.Length);
Compress.Close();
return ms.ToArray();
}
}
示例11: CompressGZip
/// <summary>
/// A string extension method that compress the given string to GZip byte array.
/// </summary>
/// <param name="stringToCompress">The stringToCompress to act on.</param>
/// <returns>The string compressed into a GZip byte array.</returns>
/// <example>
/// <code>
/// using System;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
/// using Z.ExtensionMethods;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_String_CompressGZip
/// {
/// [TestMethod]
/// public void CompressGZip()
/// {
/// // Type
/// string @this = "FizzBuzz";
///
/// // Exemples
/// var result = @this.CompressGZip();
///
/// // Unit Test
/// Assert.AreEqual("FizzBuzz", result.DecompressGZip());
/// }
/// }
/// }
/// </code>
/// </example>
public static byte[] CompressGZip(this string stringToCompress)
{
byte[] stringAsBytes = Encoding.Default.GetBytes(stringToCompress);
using (var memoryStream = new MemoryStream())
{
using (var zipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
zipStream.Write(stringAsBytes, 0, stringAsBytes.Length);
zipStream.Close();
return (memoryStream.ToArray());
}
}
}
示例12: CompressArray
public static Byte[] CompressArray(Byte[] inBuf)
{
Logger.Enter();
MemoryStream memory = new MemoryStream();
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress))
{
gzip.Write(inBuf, 0, inBuf.Length);
}
Logger.Leave();
return memory.ToArray();
}
示例13: SliceCompress
static void SliceCompress(string sourceFile, string destinationDirectory, int parts)
{
using (FileStream input = new FileStream(sourceFile, FileMode.Open))
{
string extension = ".gz";
byte[] buffer = new byte[input.Length / parts];
long bytesRead = 0;
for (int part = 1; part <= parts; part++)
{
using (FileStream output = new FileStream(destinationDirectory + @"File Part " + part + extension, FileMode.Create))
{
filesToCombine.Add(destinationDirectory + @"File Part " + part + extension);
using (GZipStream outputCompressed = new GZipStream(output, CompressionMode.Compress, false))
{
if (part < parts)
{
int readBytes = input.Read(buffer, 0, buffer.Length);
bytesRead += readBytes;
if (readBytes == 0)
{
break;
}
outputCompressed.Write(buffer, 0, readBytes);
}
else
{
while (true)
{
int readBytes = input.Read(buffer, 0, buffer.Length);
bytesRead += readBytes;
if (readBytes == 0)
{
break;
}
outputCompressed.Write(buffer, 0, readBytes);
}
}
}
}
}
}
}
示例14: saveStream
public static void saveStream(Stream stream, String location)
{
FileStream outStream = File.Create(location);
GZipStream compress = new GZipStream(outStream, CompressionMode.Compress, false);
byte[] buffer = new Byte[stream.Length];
int read = stream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
compress.Write(buffer, 0, read);
read = stream.Read(buffer, 0, buffer.Length);
}
compress.Close();
outStream.Close();
stream.Close();
}
示例15: CompressFile
public static void CompressFile(string toCompressFileName,string targetFileName,bool IsDeleteSourceFile)
{
FileStream reader;
reader = File.Open(toCompressFileName, FileMode.Open);
FileStream writer;
writer = File.Create(targetFileName);
//压缩相关的流
MemoryStream ms = new MemoryStream();
GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress, true);
//往压缩流中写数据
byte[] sourceBuffer = new byte[reader.Length];
reader.Read(sourceBuffer, 0, sourceBuffer.Length);
zipStream.Write(sourceBuffer, 0, sourceBuffer.Length);
//一定要在内存流读取之前关闭压缩流
zipStream.Close();
zipStream.Dispose();
//从内存流中读数据
ms.Position = 0; //注意,不要遗漏此句
byte[] destBuffer = new byte[ms.Length];
ms.Read(destBuffer, 0, destBuffer.Length);
writer.Write(destBuffer, 0, destBuffer.Length);
//关闭并释放内存流
ms.Close();
ms.Dispose();
//关闭并释放文件流
writer.Close();
writer.Dispose();
reader.Close();
reader.Dispose();
if (IsDeleteSourceFile)
{
File.Delete(toCompressFileName);
}
}