0

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.

Dan Hall
  • 1,474
  • 2
  • 18
  • 43
  • Does this work? `zip.AddDirectory(fol, Path.Combine("Traces", fol));` – Tim S. Oct 21 '13 at 17:27
  • That 'worked' but put the full path to the folder under the same directory as 'Traces', i need the exact data present in 'fol' to go into the specified 'Traces' directory within the zip. – Dan Hall Oct 21 '13 at 18:03
  • `zip.AddDirectory(fol, Path.Combine("Traces", new DirectoryInfo(fol).Name));` – Tim S. Oct 21 '13 at 18:33

1 Answers1

5

I think this is what you need:

  bool recurseDirectories = true;
  using (ZipFile zip = new ZipFile())
  {
    zip.AddSelectedFiles("*", @TempLoc, string.Empty, recurseDirectories);
    zip.Save(ZipFileToCreate);
  }
TcKs
  • 25,849
  • 11
  • 66
  • 104
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • Thanks Tim but unfortunately this throws an exception: "System.Windows.Forms.MouseEventArgs" Not the most helpful error...i guess that we're missing some parameters maybe? – Dan Hall Oct 21 '13 at 18:21
  • That's not helpful at all...are you sure there's not more to the exception that you're just not seeing? – Tim S. Oct 21 '13 at 18:33
  • Fiddled a little more and found you have to add a 'string.empty' to the 3rd Arg on AddSelectedFiles(), works a treat now :) Cheers. – Dan Hall Oct 21 '13 at 18:33
  • In any typical case, `""` and `string.Empty` are the same. Which you use is mainly a stylistic choice, and I find it highly unlikely that this is what really fixed your problem. I edited my answer to include `""` and you posted about `string.Empty` fixing it at about the same time I think, so maybe you just missed it. – Tim S. Oct 21 '13 at 18:57