In C# gzipping a file is easy, just use the good SharpLibZip library.

To use it, you need to include reference to ICSharpCode.SharpZipLib.dll and then add this using clause:

using ICSharpCode.SharpZipLib.GZip;

Then, given that filepath is a string containing a valid path for the to-be-gzipped file, this snippet will gzip it to a new file, without loading it all in memory (that’s good for big files).

Stream s = new GZipOutputStream(File.Create(filepath + “.gz”));
FileStream fs = File.OpenRead(filepath);
int size;
byte[] data = new byte[2048];
do
{
    size = fs.Read(data, 0, data.Length);
    s.Write(data, 0, size);
} while (size > 0);
s.Close();
fs.Close();

Given that filepath is C:\mypath\mygoodfile.ext it will create the gzipped version in C:\mypath\mygoodfile.ext.gz.




Scrivi un commento