Geeks With Blogs
Blog Moved to http://podwysocki.codebetter.com/ Blog Moved to http://podwysocki.codebetter.com/
When I was studying for the 70-536 exam, I concentrated heavily on streams as it was a good portion of the exam.  One stream that stuck in my mind the most was the GZipStream which encompasses the industry standard for lossless file compression and decompression in the GZip format.
 
As I always did during the reading process was to whip up little examples of using each technology.  I decided to create a little example of how to use the GZipStream in a few lines of code while compressing a known document.  I took the C# 2.0 Specification document and compressed it using the GZipStream. 
 
Below is the code I used:
 
static void Main(string[] args)
{
     // Get bytes from input stream
     FileStream inFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, "C# Language Specification 2.0.doc"), FileMode.Open);
     byte[] buffer = new byte[inFileStream.Length];
     inFileStream.Read(buffer, 0, buffer.Length);
     inFileStream.Close();
    
     // Create GZip file stream and compress input bytes
     FileStream outFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, "C# Language Specification 2.0.doc.gzip"), FileMode.Create);
     GZipStream compressedStream = new GZipStream(outFileStream, CompressionMode.Compress);
     compressedStream.Write(buffer, 0, buffer.Length);
     compressedStream.Close();
     outFileStream.Close();
}
 
As you can see, the code above is very simple.  What this does is read in the C# Specification 2.0 doc and get the bytes, then read it in to a new compressed filestream.  This creates a file called C# Specification 2.0.doc.gzip. 
 
You can verify the results of the operation, as it reduced the size of the document from 861KB to 271KB.  If you open the gzip archive with an appropriate viewer, such as WinRAR, or 7-Zip (my favorite).  Once you open it, you will notice that indeed it has the document in there nicely compacted.
 
Below is a picture of what it should look like:
 
 
Like I said above, there really wasn't much to this except for experimentation purposes.  There are other examples on MSDN worth looking into such as one located here
 
Posted on Wednesday, June 21, 2006 1:06 PM Microsoft , .NET , MCP | Back to top


Comments on this post: Compression and GZipStream

# re: Compression and GZipStream
Requesting Gravatar...
did you pass your test?
Left by Mox on Apr 30, 2007 3:37 PM

# re: Compression and GZipStream
Requesting Gravatar...
Hi,
was wondering if I can use the namespace for http compression. I was trying to use it for my website, www.uniforms-4u.com, and I couldn't find a working way to do so.
Thanks!
Ahmad
Left by Ahmad on Jul 23, 2007 11:16 AM

# re: Compression and GZipStream
Requesting Gravatar...
Hi,

Could you please tell me, why some files are not getting compressed? (infact when tried to compress them using GZipStream, it results in size greater than the actual one)

Regards,
JG Vimalan
http://vimalan.brainuse.com/
Left by JG Vimalan on Jan 16, 2008 3:16 AM

# re: Compression and GZipStream
Requesting Gravatar...
Yes, there are several types of files that will get larger with a GZip. This is a known problem. Files such as JPEG, PNG and other image files are already compressed. Applying the GZip algorithm, at least the Microsoft implementation thereof, will cause the size of the file to increase. I would advise against zipping those files across the wire in a stream.
Left by Matthew Podwysocki on Jan 16, 2008 5:14 PM

# re: Compression and GZipStream
Requesting Gravatar...
Hi,
When I opened compressed file using 7zip it displaying files names without extension
ex: "C# Language Specification 2.0.doc" will be displayed "C# Language Specification 2.0".

Is there any solution for this?

thanks
Left by nrk on Mar 12, 2008 9:34 AM

# re: Compression and GZipStream
Requesting Gravatar...
"When I opened compressed file using 7zip it displaying files names without extension
ex: "C# Language Specification 2.0.doc" will be displayed "C# Language Specification 2.0".

because no file name(FNAME) info in zip file

look this:

internal void WriteGzipHeader(int compression_level)
{
this.outputBuf[this.outputPos++] = 0x1f;
this.outputBuf[this.outputPos++] = 0x8b;
this.outputBuf[this.outputPos++] = 8;
this.outputBuf[this.outputPos++] = 0;
this.outputBuf[this.outputPos++] = 0;
this.outputBuf[this.outputPos++] = 0;
this.outputBuf[this.outputPos++] = 0;
this.outputBuf[this.outputPos++] = 0;
if (compression_level == 10)
{
this.outputBuf[this.outputPos++] = 2;
}
else
{
this.outputBuf[this.outputPos++] = 4;
}
this.outputBuf[this.outputPos++] = 0;
}

no time,no any additional header info


Left by naitniq on Apr 18, 2008 5:51 AM

# re: Compression and GZipStream
Requesting Gravatar...
Hello

Its glad that your told program is working fine but could you please write the code for decompression too.

Regards
Priyanka
Left by Priyanka on Dec 27, 2008 4:06 AM

Your comment:
 (will show your gravatar)


Copyright © Matthew Podwysocki | Powered by: GeeksWithBlogs.net