New to .NET 2.0, the System.IO.Compression
namespace is very cool.
Using the GZipStream
it only took these few lines of code to take a file and compress it to a neighbouring file with an extra .gz extension.
public void CompressFile(string filename)
using (FileStream fs = new FileStream(filename + ".gz", FileMode.Create, FileAccess.Write, FileShare.None))
using (GZipStream compressor = new GZipStream(fs, CompressionMode.Compress, true)
byte buffer = File.ReadAllBytes(filename);
compressor.Write(buffer, 0, buffer.Length);
How easy is that? Nice.
13 Jun 2006
» Next Post:
SSIS Compress File Task
« Previous Post:
Assembly Initialize and CleanUp in Test Projects
Comments are closed for this post.
20 Jun 2006
It very intresting to see that .NET has built in compression in library. A very good find.
Too bad it can't compress files larger than 4GB ( ISO files and so forth ). But still its good enough.
18 Dec 2008
Do you have an equivalent CompressFile subroutine for VB .NET?
10 Dec 2011
I am facing actual issue while unzipping the file manually, zipped through SSIS with the above code. I can manually extract the file however its extracting WITHOUT its extension.
Thanks in advance,