using System;
using System.IO;
using System.IO.Compression;
public static class GZ
{
public static void Compress(string srcFile, string dstFile = "", bool overwrite = true)
{
if (dstFile.Length == 0)
dstFile = srcFile + ".gz";
if (File.Exists(dstFile))
if (overwrite)
File.Delete(dstFile);
else
return;
var fi = new FileInfo(srcFile);
using (FileStream inFile = fi.OpenRead())
using (FileStream outFile = File.Create(dstFile))
using (var compress = new GZipStream(outFile, CompressionMode.Compress))
inFile.CopyTo(compress);
}
public static void Decompress(string srcFile, string dstFile = "", bool overwrite = true)
{
if (dstFile.Length == 0)
dstFile = srcFile.Remove(srcFile.Length - Path.GetExtension(srcFile).Length);
if (File.Exists(dstFile))
if (overwrite)
File.Delete(dstFile);
else
return;
var fi = new FileInfo(srcFile);
using (FileStream inFile = fi.OpenRead())
using (FileStream outFile = File.Create(dstFile))
using (var decompress = new GZipStream(inFile, CompressionMode.Decompress))
decompress.CopyTo(outFile);
}
}
A simple class (took from MSDN) to compress and decompress any file into .gz
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.