1

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

4

2 回答 2

1

我知道这已经很老了,但我遇到了同样的问题(使用 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);
        }
    }
于 2017-08-23T18:36:10.063 回答
0

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

于 2011-01-11T13:01:16.237 回答