How to NOT include full directory paths from zipped directories

Coordinator
Aug 6, 2008 at 6:51 PM
Hello,
thank you very much for this really easy to use and efficient library!

While using it with directories I get some surprising results: the parent directories (ie the beginning of the directory path) are also included in the zip file.
I use DotNet Zip Library 1.5 and .Net 3.0; on XP SP2

I have the following directory structure:
c:\Temp\RepToZip
contains: file test01.txt; test02.txt and a sub directory named RepSubDir; which also has some files in it

To test the library, I put the .dll file and Zipit.exe in c:\temp directory.

In the command line, I call:
zipit.exe test.zip "c:\temp\RepToZip"
This should zip only the RepToZip dir and its content.

What I get when I unzip it in c:\temp is another "temp" folder containing the RepToZip directory.

Is there a way not to get it, but to get the RepToZip and its content instead?


Thank you very much
Coordinator
Aug 6, 2008 at 6:58 PM

YES, it is possible to not include the full directory path in zipped archives created by DotNetZip.

To do this, you need to use the overload of Add{File,Directory,Item} that specifies a root directory for the added item. 

For your scenario, you might call ZipFile.AddDirectory("C:\temp\RepToZip", ""); 

or you might want ZipFile.AddDirectory("C:\temp\RepToZip", "RepToZip"); 

Consult the documentation for more information.  

There is more to the story, though. I originally created the zipit.exe utility as an example of how to use the library. It is necessarily an incomplete example - the library has capability that is not exposed through that utility. One of the things you cannot do within Zipit.exe is specify the DirectoryPath for the Add{File,Directory,Item} overload method. I can modify the zipit.exe to expose this capability, via a new command-line option, but I haven't done that yet.

As a workaround, one way to get what you want with the existing zipit.exe is to change the current directory before zipping. For example:

  cd \temp && zipit.exe c:\foo\test.zip RepToZip && cd \foo 
Coordinator
Aug 6, 2008 at 7:18 PM
Edited Aug 16, 2008 at 2:45 AM

Ok, I just added this capability to the zipit.exe example.

it is now in changeset 21951

http://www.codeplex.com/DotNetZip/SourceControl/DownloadSourceCode.aspx?changeSetId=21951

This is in v1.6 preview and will be in the v1.6 final release. (Expected before the end of August).

With this change, using the -d argument on the zipit.exe command line, you can now specify the DirectoryPath that is used in the AddFile / AddDirectory / AddItem overload methods.