压缩时缺少文件



我正在使用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个文件,并且缺少文件名中包含中文字符的文件。我尝试多次和不同的文件,只有当文件中包含中文字符时,才会发生文件的丢失。无论如何我能做些什么来解决这个问题吗?

似乎是ionic-zip中的一个错误或限制,最好通过记录问题来解决,无论从哪里获得它; 但是,解决方法可能是:不要使用它- 以下内容适用于内置的 .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);
}
}
}

最新更新