Home > C# > How to create a ZIP file using C#

How to create a ZIP file using C#

In my previous blog post we saw how to create CAB files using Windows Installer XML (WiX) toolset; In this post we will see how to create ZIP files using WiX toolset.

In your .NET project add references to the following DLLs:
Microsoft.Deployment.Compression.dll
Microsoft.Deployment.Compression.Zip.dll

And we will be using this library (Microsoft.Deployment.Compression.Zip) for creating CAB (cabinet) files.

using Microsoft.Deployment.Compression.Zip;

In our example we will see how to add individual files to the ZIP file and how to add a folder (directory) to the ZIP file and finally how to extract (unpack) the ZIP file.

// CREATING ZIP FILE BY ADDING LIST OF FILES
ZipInfo zip = new ZipInfo(@"C:\testarchive1.zip");
List<string> files = new List<string>();
files.Add(@"C:\test1.txt");
files.Add(@"C:\test2.txt");
files.Add(@"C:\test3.txt");
zip.PackFiles(null, files, null);

// CREATING ZIP FILE BY ADDING FOLDER (WITH SUB-FOLDERS) USING MAXIMUM COMPRESSION
zip = new ZipInfo(@"C:\testarchive2.zip");
zip.Pack(@"C:\Balaji", true, Microsoft.Deployment.Compression.CompressionLevel.Max, null);

// EXTRACTING (UNPACKING) FILES FROM ZIP FILE
zip = new ZipInfo(@"C:\testarchive2.zip");
zip.Unpack(@"C:\ArchiveDir");
Advertisements
  1. August 10, 2011 at 3:33 PM

    I appreciate taking a few minutes go over doing this, I’m ardently to sort it out as well as seriously like exploring more on the concern. When possibilities, as you may reach mastery, do you opinions bringing up-to-date your internet utilizing a lot more details? This is very helpful for i am.

  2. August 15, 2011 at 2:06 AM

    Great website helpful info and nice background.

  3. Anonymous
    February 21, 2012 at 6:12 AM

    how to include empty subfolder in the zip file ?

  4. Anonymous
    February 21, 2012 at 6:14 AM

    I want to include empty subfolders in the zip file, is there a way to do that uzing Microsoft.Deployment.Compression.Zip?

  5. Anonymous
    February 21, 2012 at 6:15 AM

    is there a way to include empty subfolders in the zip?

  6. amplemaddy
    May 24, 2012 at 2:14 AM

    public void MakeZip(string SourceFileName,string ZipFileName)// Zip functionality added by madhu
    {
    System.Diagnostics.Process scriptProc = new System.Diagnostics.Process();
    if (File.Exists(SourceFileName))
    {
    string command = “zip ” + ZipFileName + ” ” + SourceFileName; // Building dos commands
    System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(“cmd”, “/c ” + command);
    scriptProc.StartInfo = procStartInfo;
    scriptProc.Start();
    int id = scriptProc.Id; //get process id
    start:
    System.Diagnostics.Process[] processlist = System.Diagnostics.Process.GetProcessesByName(“cmd”);
    foreach (System.Diagnostics.Process theprocess in processlist)
    {
    if (theprocess.Id == id) //check proc id is found or not
    {
    goto start;
    }
    }
    scriptProc.Close();
    }
    }

    • shivkumar
      October 31, 2012 at 3:34 AM

      dear i have used the same code.
      there is no error but i am not getting the proper output(Zip file).
      plz advice me you can mail me also on shivakuti@gmail.com

      thanks
      shivkumar

      • Balaji Baskar
        March 18, 2013 at 8:43 AM

        shivkumar, I am not sure if your question is intended to the code block the other user included or to the one within this blog. let me know if I can help you better.

  7. Balaji Baskar
    June 8, 2012 at 7:58 AM

    For all the folks referencing to this blog post, Do you know that .Net Framework 4.5 “now” offers support for “ZIP” under “System.IO.Compression” namespace? Refer to this article in VisualStudio magazine: http://visualstudiomagazine.com/articles/2012/05/21/net-framework-gets-zip.aspx I will also come up with a sample program for this!

    • Mohit
      February 20, 2013 at 9:10 PM

      Do you still use goto statement

      • Balaji Baskar
        March 18, 2013 at 8:41 AM

        goto statement…??? where???

  8. July 15, 2012 at 2:01 PM

    Good one.. The above sample and the new System.IO.Compression are generic Zip libraries provided by MSDN. There are lot of third party zip libraries that are more efficient than these.
    DotNetZip library is one of those libraries which is very simple to use, fast and also free.

    • Jess
      February 9, 2013 at 8:12 AM

      But those 3rd party libs require the .dll to be shipped with the .exe In some environments (i.e. the one I work in), this isn’t allowed. any time we can use something from the framework instead of 3rd party, we always jump on that.

  1. September 4, 2012 at 3:54 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: