• How to compress or unzip files & folders in C#

    Posted on March 21, 2012 by in C#, Dotnet

    There are so many free opensource tools out there that would achieve this functionality, but I feel ICSharpCode.SharpZipLib is the best in business. It gives you so much flexibility to zip/unzip folders/files with or without password protection. In this article, I would give you some samples that would cover wide range of situations.

    Where can I download the assembly?

    http://www.icsharpcode.net/opensource/sharpziplib/download.aspx

    Example 1: How to zip a file?

    Ziplib API provides a ZipOutputStream which we could use to achieve this.

    public static void Zip(string SrcFile, string DstFile)
            {
                FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
                FileStream fileStreamOut = new FileStream(DstFile, FileMode.Create, FileAccess.Write);
                ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
    
                byte[] buffer = new byte[fileStreamIn.Length];
    
                ZipEntry entry = new ZipEntry(Path.GetFileName(SrcFile));
                zipOutStream.PutNextEntry(entry);
    
                int size;
                do
                {
                    size = fileStreamIn.Read(buffer, 0, buffer.Length);
                    zipOutStream.Write(buffer, 0, size);
                } while (size > 0);
    
                zipOutStream.Close();
                fileStreamOut.Close();
                fileStreamIn.Close();
            }

    Example 2: How to zip a folder including all the files & subfolders?

    public static void ZipFolders()
            {
                ICSharpCode.SharpZipLib.Zip.FastZip fz = new FastZip();
                fz.CreateZip(@"C:\Temp\zip\test.zip", @"C:\Temp\zip\test", true, "", "");
            }

    Example 3: You want to add a new file to an existing zip, no problem

    public static void AddFileToZip(string sourceFile, string zipFolderPath)
            {
                ZipFile zipFile = new ZipFile(zipFolderPath);
    
                zipFile.BeginUpdate();            
                zipFile.UseZip64 = UseZip64.On;
                zipFile.Add(sourceFile, Path.GetFileName(sourceFile));
                                        
                zipFile.CommitUpdate(); 
                zipFile.IsStreamOwner = true;            
                zipFile.Close();
    
            }

    Example 4: lets cut to the chase. I don’t want o pay money to WINZIP for unzipped my folder.

    public static void UnZip(string sourcePath, string targetPath)
            {            
                ICSharpCode.SharpZipLib.Zip.FastZip fz = new FastZip();
                fz.ExtractZip(sourcePath, targetPath, "");
                
            }
    Be Sociable, Share!

    No related posts.

    Written by

    Software architect with over 10 years of proven experience in designing & developing n-tier and web based software applications, for Finance, Telecommunication, Manufacturing, Internet and other Commercial industries. He believes that success depends on one's ability to integrate multiple technologies to solve a simple as well as complicated problem.

    View all articles by

    Email : [email protected]