Search code examples
c#compressiondotnetzip

DotNetZip - Adding Folders


i imagine this is very simple but i can find nothing in the DotNetZip examples or documentation to help me. I need to add a folder to a zip that contains both folders and files, i need to maintain the folders rather than only zipping their files but using the following it always strips away the folders:

 using (ZipFile zip = new ZipFile())
                {
                    string[] files = Directory.GetFiles(@TempLoc);
                    string[] folders = Directory.GetDirectories(@TempLoc);
                    zip.AddFiles(files, "Traces");

                    foreach (string fol in folders)
                    {
                        zip.AddDirectory(fol, "Traces");
                    }

                    zip.Comment = "These traces were gathered " + System.DateTime.Now.ToString("G");
                    zip.Save(arcTraceLoc + userName.Text + "-Logs.zip");
                }

I'm using the loop as i could not find a function for folders similar to 'AddFiles' in DotNetZip.

Thanks.


Solution

  • I think this is what you need:

      bool recurseDirectories = true;
      using (ZipFile zip = new ZipFile())
      {
        zip.AddSelectedFiles("*", @TempLoc, string.Empty, recurseDirectories);
        zip.Save(ZipFileToCreate);
      }