Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

How to compress/decompress directories using GZipStream

By , 1 Feb 2012
Rate this:
Please Sign up or sign in to vote.
My alternative is to rather use a tried and tested compression api. It's simpler to use and you don't have to spend time developing helper methods to zip and unzip files.[^]
Create a zip file
 using (ZipFile zip = new ZipFile())
Extract a zip file
private void MyExtract()
      string zipToUnpack = "";
      string unpackDirectory = "Extracted Files";
      using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
          // here, we extract every entry, but we could extract conditionally
          // based on entry name, size, date, checkbox status, etc.  
          foreach (ZipEntry e in zip1)
            e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
Create a downloadable zip within
public void btnGo_Click (Object sender, EventArgs e)
  Response.BufferOutput= false;  // for large files
  String ReadmeText= "This is a zip file dynamically generated at " + System.DateTime.Now.ToString("G");
  string filename = System.IO.Path.GetFileName(ListOfFiles.SelectedItem.Text) + ".zip";
  Response.ContentType = "application/zip";
  Response.AddHeader("content-disposition", "filename=" + filename);
  using (ZipFile zip = new ZipFile()) 
    zip.AddFile(ListOfFiles.SelectedItem.Text, "files");
    zip.AddEntry("Readme.txt", "", ReadmeText);
as well as various other uses.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dean Oliver
Software Developer BBD Johannesburg
South Africa South Africa
Bsc (Hons) Business Information Systems.
MCTS: Web Applications Development with Microsoft .NET Framework 4
MCTS: Windows Communication Foundation Development with Microsoft .NET Framework 4
MCTS: Accessing Data with Microsoft .NET Framework 4
Microsoft Certified Professional Developer Certification.

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 1 Feb 2012
Article Copyright 2012 by Dean Oliver
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid