How do you add a folder to a zip archive with ICSharpCode.SharpZipLib

痴心易碎 提交于 2019-12-05 08:17:46

I figured it out:

  • You can simply do new ZipEntry("Folder1/Archive.txt"); and new ZipEntry("Folder2/Archive2.txt");

The answer above will work for several scenarios, but it will not work when you want to add an empty folder to a zip file.

I sifted through the SharpZipLib code and found that the only thing you need to do to create a folder is a trailing "/" forward slash on the ZipEntry name.

Here's the code from the library:

public bool IsDirectory {
    get {
        int nameLength = name.Length;
        bool result =
            ((nameLength > 0) &&
            ((name[nameLength - 1] == '/') || (name[nameLength - 1] == '\\'))) ||
            HasDosAttributes(16)
            ;
        return result;
    }
}

So, just create folders as though they are files with ZipEntry, and put a forward slash on the end. It works. I've tested it.

The best solution at our project was to switch to the way better

https://dotnetzip.codeplex.com

https://github.com/haf/DotNetZip.Semverd

the methods are more straight forward to use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!