1

我正在使用 Ionic.zip 压缩一堆文件。这是我将这些文件添加到 zip 中的代码。

ZipFile zip = new ZipFile();
foreach (string filepath in listoffile) //5 files
{                             
     zip.AddFile(filepath, "");
}
zip.Save(mypath + "attachment.zip");

当我检查 zip 中的文件数时,它显示 5,但是当我打开 zip 文件时,它里面只有 4 个文件,并且缺少文件名中包含中文字符的文件。我尝试了多次和不同的文件,只有当文件中包含中文字符时才会发生文件丢失。无论如何我可以做些什么来解决这个问题?

4

1 回答 1

2

看起来是中的错误或限制,最好通过记录从哪里获得问题的问题来解决;但是,一种解决方法可能是:不要使用它- 以下内容适用于内置的 .NET 类型(在某些框架上,您可能需要添加包引用System.IO.Compression)。它有点手动,但是:它有效。

    using (var output = File.Create("attachment.zip"))
    using (var zip = new ZipArchive(output, ZipArchiveMode.Create))
    {
        foreach (var file in listoffile)
        {
            var entry = zip.CreateEntry(file);
            using (var source = File.OpenRead(file))
            using (var destination = entry.Open())
            {
                source.CopyTo(destination);
            }
        }
    }
于 2020-06-23T13:41:16.327 回答