How to add files in archieve without paths

Feb 16, 2013 at 4:48 PM
Edited Feb 16, 2013 at 4:50 PM
How to add files in archieve without paths?

I have a folder c:\aa\bb\cc\dd\ee\ with 10 files
and make archieve c:\aa\bb\cc\dd\ee\all.zip

If I add files as
          var filenames = Directory.GetFiles(directoryToZip);
          foreach (var filename in filenames)
                            zip.AddFile(filename);
          zip.Save(zipFileToCreate);
in archieve I must step over all this folders
     aa <Enter>
     bb <Enter>
     cc <Enter>
     dd <Enter>
     ee <Enter>
 and only then I see my files!
Feb 17, 2013 at 8:51 AM
Edited Feb 17, 2013 at 8:53 AM
Hello

This can be done very easily by passing String.Empty, or "" as the second parameter to the Zip.AddFile method. This will place all the files in the root of the ,zip file (without any directory information) - see the modified code snippet below
 var filenames = Directory.GetFiles(directoryToZip);
          foreach (var filename in filenames)
                            zip.AddFile(filename, String.Empty);
          zip.Save(zipFileToCreate);
Be aware that if you use this method, and have files with the same name in different directories, it will cause an error as it will attempt to place the same file name more than once in the root of the .zip file.

Hope this helps
Feb 17, 2013 at 9:42 AM
Yes! This is what I want. Thanks!