To compress and decompress a data it is suggested to use class DefalteStream or GZipStream
using System.IO;
using System.IO.Compression;
namespace zip_usage
{
class Program
{
static Stream CreateCompressStream(Stream inputStream, CompressionMode compressionMode)
{
//return new GZipStream(inputStream, compressionMode, true);
return new DeflateStream(inputStream, compressionMode, true);
}
static void Main()
{
string inputFileName = @"..\..\Data\inputData.bin";
string compressedFileName = @"..\..\Data\compressedData.zip";
string resultFileName = @"..\..\Data\outputData.bin";
byte[] inputBuffer = File.ReadAllBytes(inputFileName);
using (FileStream outputStream = File.OpenWrite(compressedFileName))
{
using (Stream compress = CreateCompressStream(outputStream, CompressionMode.Compress))
{
compress.Write(inputBuffer, 0, inputBuffer.Length);
}
}
byte[] resultBuffer = new byte[inputBuffer.Length];
using (FileStream compressedStream = File.OpenRead(compressedFileName))
{
using (Stream decompress = CreateCompressStream(compressedStream, CompressionMode.Decompress))
{
decompress.Read(resultBuffer, 0, resultBuffer.Length);
}
}
File.WriteAllBytes(resultFileName, resultBuffer);
}
}
}
Additional Links: System.IO.Compression Namespace
No comments:
Post a Comment