Answered Can't zip nested folders

  • Tuesday, October 25, 2005 1:48 PM
     
     
    Hi people ! does anyone here used the java.util.zip package ? I need to zip a series of nested folders  similar to this

    MyProjects/MicrosoftProjects/MyProgram/bin/Debug/MyProgram.dll

    however the ZipOutput stream requires a file,in this case MyProgram.dll.Does anyone have a workaroud regading the above issue ? I keeping hitting the FileNotFoundException as i specifed the sourcestream at MyProjects/


    regards
    Wilson

Answers

  • Wednesday, October 26, 2005 4:56 AM
    Moderator
     
     Answered

    Define a new ZipEntry using the root folder

    and recursive loop the folder and file under the folder and add it to the ZipOutputStream

    public void ZipFolder(string RootFolder, string CurrentFolder, ZipOutputStream zStream)
    {
     string[] SubFolders = System.IO.Directory.GetDirectories(CurrentFolder);
     foreach (string Folder in SubFolder)
     {
      ZipFolder(RootFolder, Folder, zStream);
     }

     ZipEntry entry;
     entry = new ZipEntry(CurrentFolder.Substring(RootFolder.Length) + "/");
     entry.DateTime = DateTime.Now;

     zStream.PutNextEntry(entry);

     //Start loop files under CurrentFolder
     //End
    }

All Replies

  • Wednesday, October 26, 2005 4:00 AM
     
     
     bmann225 wrote:
    Hi people ! does anyone here used the java.util.zip package ? I need to zip a series of nested folders  similar to this

    MyProjects/MicrosoftProjects/MyProgram/bin/Debug/MyProgram.dll

    however the ZipOutput stream requires a file,in this case MyProgram.dll.Does anyone have a workaroud regading the above issue ? I keeping hitting the FileNotFoundException as i specifed the sourcestream at MyProjects/


    regards
    Wilson


    Does anyone noe how to zip a directory using the ICSharpCode.SharpZipLib dll ?
  • Wednesday, October 26, 2005 4:56 AM
    Moderator
     
     Answered

    Define a new ZipEntry using the root folder

    and recursive loop the folder and file under the folder and add it to the ZipOutputStream

    public void ZipFolder(string RootFolder, string CurrentFolder, ZipOutputStream zStream)
    {
     string[] SubFolders = System.IO.Directory.GetDirectories(CurrentFolder);
     foreach (string Folder in SubFolder)
     {
      ZipFolder(RootFolder, Folder, zStream);
     }

     ZipEntry entry;
     entry = new ZipEntry(CurrentFolder.Substring(RootFolder.Length) + "/");
     entry.DateTime = DateTime.Now;

     zStream.PutNextEntry(entry);

     //Start loop files under CurrentFolder
     //End
    }

  • Wednesday, October 26, 2005 8:13 AM
     
     

    Hi jacky, thanks for the example,however, are you using the ICSharpCode dll or the java.util.zip ?

    Another question, when i intially call the method ? Do I pass in the root folders into the method ?>

  • Wednesday, October 26, 2005 8:23 AM
    Moderator
     
     

    I'm using ICSharpCode.SharpZipLib.dll

    when i intially call the method?
    just call the function when you want to zip folder, the function will loop to get all folders and files inside the root folder where you pass in.
    P.S. you need to declare the ZipOutputStream before call the method.

    Do I pass in the root folders into the method?
    yes, just call

    ZipOutputStream s = new ZipOutputStream(SomewhereYouWantToZip);
    s.SetLevel(6); // 0 - store only to 9 - means best compression
    ZipFolder(SomewhereYouWantToZip, SomewhereYouWantToZip, s);

    hope it can help you.

  • Wednesday, October 26, 2005 8:31 AM
     
     
    What happens if at the end of the basecase i need to zip additional files ??

    given the structure

    Pictures/Family/25102005_Outing.jpg
    Pictures/Family/26102005_BBQ.jpg

    What i want to achieve is 1 zip file created and the final Family folder has the 2 photos.
  • Wednesday, October 26, 2005 8:37 AM
    Moderator
     
     

    FYI.

    string SomewhereYouWantToZip = "Pictures";

    ZipOutputStream s = new ZipOutputStream(SomewhereYouWantToZip);
    s.SetLevel(6); // 0 - store only to 9 - means best compression
    ZipFolder(SomewhereYouWantToZip, SomewhereYouWantToZip, s);

  • Wednesday, October 26, 2005 8:50 AM
     
     
     Jacky Yiu wrote:

    FYI.

    string SomewhereYouWantToZip = "Pictures";

    ZipOutputStream s = new ZipOutputStream(SomewhereYouWantToZip);
    s.SetLevel(6); // 0 - store only to 9 - means best compression
    ZipFolder(SomewhereYouWantToZip, SomewhereYouWantToZip, s);



    Sorry Jacky,

    however the ZipOutPutStream() requires a System.IO.Stream path to be passed in and not a string.
  • Wednesday, October 26, 2005 9:21 AM
    Moderator
     
     

    sorry, the thing should be.

    string SomewhereYouWantToZip = "Pictures";
    string TheZipFilePath = "Somewhere";

    ZipOutputStream s = new ZipOutputStream(File.Create(TheZipFilePath));
    s.SetLevel(6); // 0 - store only to 9 - means best compression
    ZipFolder(SomewhereYouWantToZip, SomewhereYouWantToZip, s);

    Sorry for the mistake...
     :$

  • Wednesday, October 26, 2005 9:45 AM
    Moderator
     
     

    Actually, if you are using ICSharpCode.SharpZipLib you can download the sample in their website, there has a sample project called CreateZipFile, you can reference that project. and just modify the zip file part to the code what I post before.

    http://www.icsharpcode.com/OpenSource/SharpZipLib/Download.aspx