locked
Zip a folder and containts RRS feed

  • Question

  • I've been searching for hints and tips on how to zip up a folder and it's contents using C#. I've found a few examples that have yielded nothing major.

    Anyone have a code snipit that'd help me out? I have the file path and I want to take the directory and zip it up using the standard libraries and not a third party dll.

    Thanks for any help!

    - JB
    Wednesday, January 7, 2009 9:17 PM

Answers

  •  
    AlexB
    • Proposed as answer by Harry Zhu Monday, January 12, 2009 2:08 AM
    • Marked as answer by JBeaulieu Tuesday, January 13, 2009 6:38 PM
    Wednesday, January 7, 2009 9:59 PM
  • private void ZipDirectory(string ZipFile, string DirToZip)  
        {  
          if (!System.IO.File.Exists(ZipFile)) PrepareEmptyZipFile(ZipFile);  
          Shell32.Shell sh = new Shell32.Shell();  
          Shell32.Folder sf = sh.NameSpace(DirToZip);  
          Shell32.Folder df = sh.NameSpace(ZipFile);  
          df.CopyHere(sf, 0);  //Consult help for options.  
        }  
        private void PrepareEmptyZipFile(string ZipFile)  
        {  
          byte[] b = new byte[22];  
          Array.Clear(b, 0, 22);  
          Array.Copy(new byte[] { 80, 75, 5, 6 }, b, 4);  
          System.IO.File.WriteAllBytes(ZipFile, b);  
        } 
    • Proposed as answer by Harry Zhu Monday, January 12, 2009 2:08 AM
    • Marked as answer by JBeaulieu Tuesday, January 13, 2009 6:38 PM
    Wednesday, January 7, 2009 10:22 PM

All replies

  •  
    AlexB
    • Proposed as answer by Harry Zhu Monday, January 12, 2009 2:08 AM
    • Marked as answer by JBeaulieu Tuesday, January 13, 2009 6:38 PM
    Wednesday, January 7, 2009 9:59 PM
  • private void ZipDirectory(string ZipFile, string DirToZip)  
        {  
          if (!System.IO.File.Exists(ZipFile)) PrepareEmptyZipFile(ZipFile);  
          Shell32.Shell sh = new Shell32.Shell();  
          Shell32.Folder sf = sh.NameSpace(DirToZip);  
          Shell32.Folder df = sh.NameSpace(ZipFile);  
          df.CopyHere(sf, 0);  //Consult help for options.  
        }  
        private void PrepareEmptyZipFile(string ZipFile)  
        {  
          byte[] b = new byte[22];  
          Array.Clear(b, 0, 22);  
          Array.Copy(new byte[] { 80, 75, 5, 6 }, b, 4);  
          System.IO.File.WriteAllBytes(ZipFile, b);  
        } 
    • Proposed as answer by Harry Zhu Monday, January 12, 2009 2:08 AM
    • Marked as answer by JBeaulieu Tuesday, January 13, 2009 6:38 PM
    Wednesday, January 7, 2009 10:22 PM
  • If u are using .net 2.0 u can use sharpziplib. its an open source component.
    http://www.icsharpcode.net/OpenSource/SharpZipLib/
    • Proposed as answer by Harry Zhu Monday, January 12, 2009 2:08 AM
    Thursday, January 8, 2009 4:15 AM
    • Proposed as answer by Harry Zhu Monday, January 12, 2009 2:08 AM
    Thursday, January 8, 2009 1:55 PM
  • Hello!
    We once faced the similar situation where I have to zip and unzip directory to store and retrieve from Database. What we did is that we use Open Source Solution i.e. #ZipLib (Pronounced as SharpZipLib) which is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. 

    Please visit the its website to download library.
    • Proposed as answer by Adil Mughal Thursday, January 8, 2009 2:57 PM
    Thursday, January 8, 2009 2:57 PM