.NET Tips and Tricks

Blog archive

Create .zip Files from Code

If you've ever wanted to create a .zip file from your application, the System.IO.Compression.GZipStream will let you do it, assuming that you're comfortable with your file being in the industry-standard .gzip format (though I'm told you can swap in other compression schemes).

For this tip, I'm assuming that the file is small enough that you can read it all in one gulp. In that scenario, my first move is to define a FileStream object for the file I want to compress, read the file into memory, and close the FileStream:

Dim unCompressedFile As FileStream
unCompressedFile = File.OpenRead("C:\GreatBigFile.txt")
Dim bytes(5000) As Byte
Dim bytesRead As Integer
bytesRead = unCompressedFile.Read(bytes, 0, 5000)
unCompressedFile.Close()

My next move is to create a FileStream object for the compressed file I want to create:

Dim CompressedFile As FileStream
CompressedFile = File.Create("C:\IttyBittyFile.gzip")

With all the files in place, I'm ready to create the GZipStream object that will do the actual compression. You have to pass two parameters when creating the GZipStream object: The first is the FileStream for the compressed file you intend to create; the second indicates that you want to compress the file.

Here's my code to create a compressed file called IttyBittyFile.gzip:

Dim zipper As GZipStream
zipper = New GZipStream(CompressedFile, CompressionMode.Compress)

Now I can use the GZipStream Write method to write out the compressed file, passing the bytes I retrieved from the original file:

zipper.Write(bytes, 0, bytesRead)

Finally, of course, I close the GZipStream object and my compressed file:

zipper.Close()
CompressedFile.Close()

As you probably suspect, if you change the CompressionMode and pass in a compressed file, you can use GZipStream to decompress a file.

Posted by Peter Vogel on 12/10/2015


comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube