Cheeso, Compress directory with sub directory

Dec 4, 2009 at 6:29 AM

I want to compress directory with dotnetzip lib, and unzip it with explore. code is as following:

files = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);
                emptyDirs = GetAllEmptyDirectories(dir);

                using (ZipFile zip = new ZipFile(Encoding.Default))
                    zip.Password = pwd;
                    //zip.UseUnicodeAsNecessary = true;
                    zip.BufferSize = Constants.ZIP_BLOCK_SIZE;
                    zip.SaveProgress += this.SaveProgress;
                    //zip.ProvisionalAlternateEncoding = Encoding.Default;

                    for (int i = 0; i < files.Length; i++)
                        if (stopFlg)
                            return false;

                        tmpPath = Path.GetDirectoryName(files[i]);
                        if (!tmpPath.EndsWith(@"\"))
                            tmpPath = tmpPath.PadRight(tmpPath.Length + 1, '\\');

                        if (basePath.EndsWith(@"\"))
                            basePath = basePath.TrimEnd(@"\".ToCharArray());

                        tmpPath = tmpPath.Remove(0, basePath.Length);

                        zip.AddFile(files[i], tmpPath);

                    if (emptyDirs != null && emptyDirs.Length > 0)
                        for (int i = 0; i < emptyDirs.Length; i++)
                            zip.AddDirectoryByName(emptyDirs[i].Remove(0, basePath.Length).TrimStart("\\".ToCharArray()));


When i open the compressing result with explorer, the not empty directory and file is right, but the empty directory added by AddDirectoryByName function is incorrect. It's my usage problem or the Adddirectory function problem?



Dec 4, 2009 at 8:23 AM

> empty directory added by AddDirectoryByName function is incorrect

What do you mean "incorrect" ?

what's the problem?


Dec 4, 2009 at 8:45 AM

sorry, what i said above is not clear.

when the empty directory name is Japanese, i add it to zip archive with AddDirectoryByName, then open the zip archive with compressed (zipped) folder [explorer], it is lost.

But when i add the empty directory to the zip archive with AddDirectory, then open the zip archive with  compressed (zipped) folder [explorer], it exists in the zip archive.

Dec 5, 2009 at 12:28 PM

This code works for me:

using (var zip = new ZipFile())
    zip.UseUnicodeAsNecessary = true;
    zip.AddDirectoryByName("Ho Ho Ho を見てふと書い");

(I don't know Japanese, so I'm not sure if that makes sense)

This generates a compliant ZIP file, that uses UTF-8 to encode filenames.  The problem with this is, Windows Explorer does not respect the UTF-8 encoding in the zip. 

I wrote a lot about this in the documentation for UseUnicodeAsNecessary and ProvisionalAlternateEncoding.  I won't repeat that information here.

I suggest you do several things:

  • simplify the situation - use a very simple test case that isolates the situation.
  • try different encodings:  UTF-8,  Encoding.Default (which I guess would be a Japanese code page, but I don't know which code page that is) .  See if the zip files you create can be opened in Explorer.
  • If there is a difference in behavior between AddDirectory() and AddDirectoryByName(), produce a very small test case that demonstrates the discrepancy, and post it here.  I'll be able to fix the problem if you can demonstrate it.


Dec 7, 2009 at 12:42 AM

Cheeso, thanks for your patience.

Can i use the AddEntry method to add a directory (not including the files and subdirectory under the didrectory) to the zip archive?

Dec 7, 2009 at 1:17 PM

I think you can do what you want with zip.AddDirectoryByName().  What it does is add an entry to the zipfile. The entry added is a directory, and the directory need not exist on the filesystem.

check the doc for more information on that method call.

I think I don't understand the problem you are having, using that method call.