如何使用 ZipOutputStream 在 zip 文件中添加 EmptyFolder 或目录

发布于 2024-10-11 14:00:20 字数 186 浏览 6 评论 0原文

我希望能够使用 ICSharpCode.SharpZipLib.Zip.ZipOutputStream 创建包含空文件夹的 zip 文件。我可以使用 ICSharpCode.SharpZipLib.Zip.FastZip.CreateEmptyDirectories = true,但 FastZip 不允许使用 UTF8 文件名。

I want to be able creating zip files that contain empty folders, using ICSharpCode.SharpZipLib.Zip.ZipOutputStream. I can use ICSharpCode.SharpZipLib.Zip.FastZip.CreateEmptyDirectories = true, but FastZip doesn't allow UTF8 file names.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-10-18 14:00:20

我知道这已经很老了,但我遇到了同样的问题(使用 ZipOutputStream 压缩空目录)。

我正在根据不同的父目录进行递归调用。

public void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)
    {
        var files = Directory.GetFiles(path);
        var directories = Directory.GetDirectories(path);

        //this is where I add the empty directory
        //code begin
        if (files.Count() == 0 && directories.Count() == 0)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            string cleanName = ZipEntry.CleanName(path.Substring(folderOffset)) + "/";
            ZipEntry zipEntry = new ZipEntry(cleanName);
            zipEntry.DateTime = di.LastWriteTime;
            zipStream.PutNextEntry(zipEntry);
            zipStream.CloseEntry();
            return;
        }
        //code end

        foreach (string file in files)
        {
            FileInfo fileInfo = new FileInfo(file);
            string cleanName = ZipEntry.CleanName(file.Substring(folderOffset));
            ZipEntry zipEntry = new ZipEntry(cleanName);
            zipEntry.DateTime = fileInfo.LastWriteTime;
            zipEntry.Size = fileInfo.Length;
            zipStream.PutNextEntry(zipEntry);
            byte[] numArray = new byte[4096];
            using (FileStream fileStream = File.OpenRead(file))
            {
                StreamUtils.Copy(fileStream, zipStream, numArray);
            }
            zipStream.CloseEntry();
        }

        foreach (string directory in directories)
        {
            CompressFolder(directory, zipStream, folderOffset);
        }
    }

I know this is very old but I came across the same issue (zipping empty directories with ZipOutputStream).

I am making a recursive call based on different parent directories.

public void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)
    {
        var files = Directory.GetFiles(path);
        var directories = Directory.GetDirectories(path);

        //this is where I add the empty directory
        //code begin
        if (files.Count() == 0 && directories.Count() == 0)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            string cleanName = ZipEntry.CleanName(path.Substring(folderOffset)) + "/";
            ZipEntry zipEntry = new ZipEntry(cleanName);
            zipEntry.DateTime = di.LastWriteTime;
            zipStream.PutNextEntry(zipEntry);
            zipStream.CloseEntry();
            return;
        }
        //code end

        foreach (string file in files)
        {
            FileInfo fileInfo = new FileInfo(file);
            string cleanName = ZipEntry.CleanName(file.Substring(folderOffset));
            ZipEntry zipEntry = new ZipEntry(cleanName);
            zipEntry.DateTime = fileInfo.LastWriteTime;
            zipEntry.Size = fileInfo.Length;
            zipStream.PutNextEntry(zipEntry);
            byte[] numArray = new byte[4096];
            using (FileStream fileStream = File.OpenRead(file))
            {
                StreamUtils.Copy(fileStream, zipStream, numArray);
            }
            zipStream.CloseEntry();
        }

        foreach (string directory in directories)
        {
            CompressFolder(directory, zipStream, folderOffset);
        }
    }
戴着白色围巾的女孩 2024-10-18 14:00:20

请参阅http://community.sharpdevelop.net/forums/p/10856/29901。 aspx 其中(我引用):
“我们当前的路线图包括 UTF 条目文件名处理 [...]”
这会让我明白目前不支持 UTF 文件名。

See http://community.sharpdevelop.net/forums/p/10856/29901.aspx where (I quote):
"Our current roadmap includes UTF entry filename handling [...]"
Which would lead me to understand that UTF filenames are not currently supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文