Add the System.IO.Compression.ZipFile NuGet to your project
Use this code to Zip and Unzip your file using GZip
static void ZipMyFile(string source, string target)
{
using (FileStream sourceStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (FileStream targetStream = File.Create(target))
{
using (GZipStream compressionStream = new GZipStream(targetStream, CompressionMode.Compress))
{
sourceStream.CopyTo(compressionStream);
}
}
}
}
static void UnzipMyFile(string source, string target)
{
using (FileStream sourceStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (FileStream targetStream = File.Create(target))
{
using (GZipStream decompressionStream = new GZipStream(sourceStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(targetStream);
}
}
}
}
Comments
Post a Comment