Posted by: Rhonda | June 7, 2008

File Compression Using the .NET GZipStream Class

The GZipStream class is a part of the System.IO.Compression namespace. This class is used for the compression of data. Before the .NET 2.0 Framework, the only choice we had for compression were third party tools.

Common Methods (Full List)

  • BeginRead – Begins an asynchronous read operation.
  • BeginWrite – Begins an asynchronous write operation.
  • Close – Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream.
  • EndRead – Waits for the pending asynchronous read to complete.
  • EndWrite – Handles the end of an asynchronous write operation.
  • Read – Reads a number of decompressed bytes into the specified byte array.
  • Write – Writes compressed bytes to the underlying stream from the specified byte array.

Below is a simple C# console application that takes an input file, compresses it and creates a compressed zip file.

View the C# code sample [HERE]

The command-line illustration below shows the application at work.

  • If no argument is provided, the usage syntax is echoed to the screen
  • If the argument provided cannot be located, error is echoed to the screen
  • If correct argument is provided, the file is compressed and a new file is created

compression1

The result (compressed) file created.

compression2

View the compressed file in a Zip application.

compression3

The GZipStream class is not as feature-rich as the third-party tools, but it is a convenient way to compress data.

Related Content

Happy programming…


Responses

  1. [...] File Compression Using the .NET GZipStream Class (Rhonda Tipton) [...]

  2. [...] File Compression Using the .NET GZipStream Class – Rhonda Tipton ‘ The GZipStream class is not as feature-rich as the third-party tools, but it is a convenient way to compress data ‘ [...]


Leave a response

You must be logged in to post a comment.

Categories