0
我试图使用压缩与DotNetZip此代码2个PDF文件:压缩PDF文件DotNetZip返回损坏的压缩文件
void Main()
{
byte[] file1 = File.ReadAllBytes(@"C:\temp\a.pdf");
byte[] file2 = File.ReadAllBytes(@"C:\temp\b.pdf");
//byte[] file3 = File.ReadAllBytes(@"C:\temp\c.pdf");
byte[] zip = Zipper.CreateZipFromFileContentList(new List<Tuple<string, byte[]>> {
new Tuple<string, byte[]>(@"a.pdf", file1),
new Tuple<string, byte[]>(@"b.pdf", file2)//,
//new Tuple<string, byte[]>(@"c.pdf", file3)
});
File.WriteAllBytes(@"C:\temp\c.zip", zip);
using(Ionic.Zip.ZipFile zipFile = Ionic.Zip.ZipFile.Read(@"C:\temp\c.zip"))
{
foreach(ZipEntry entry in zipFile)
{
entry.Extract(@"C:\temp\t");
}
}
}
// Define other methods and classes here
static class Zipper
{
public static byte[] CreateZipFromFileContentList(IList<Tuple<string, byte[]>> fileContentList)
{
try
{
using (ZipFile zip = new ZipFile())
{
int i = 0;
foreach (var item in fileContentList)
{
ZipEntry entry = null;
entry = zip.AddEntry(item.Item1, item.Item2);
++i;
}
MemoryStream ms = new MemoryStream();
zip.Save(ms);
return ms.GetBuffer();
}
}
catch (Exception)
{
throw;
}
}
}
其实一切正常,因为在创建归档后的提取过程的工作。但是,如果我尝试使用winRar打开zip文件,则会收到一条消息,指出存档已损坏。如果我尝试使用7Zip,我会收到一条消息,指出有用数据块的末尾有数据(翻译后,我不知道英文版是否提供了完全相同的消息)。
如果我拉链1个或3个文件,我没有问题的。 我该如何解决这个问题?
你有使用普通的文本文件,尝试这样做?你能压缩和解压2个文本文件成功吗? – rossum
你的拉链产量可能有额外的字节,因为.GetBuffer返回流内部缓冲区 - 仅包含有效的数据部分,使用.ToArray()代替。 –
@AlexK。解决问题,请回答,我会接受它作为正确的 – Phate01