我有一个奇怪的文件,使用DotNetZip压缩后会创建一个“无法解压”的存档文件。当我尝试使用7zip解压它时,会出现错误消息:CRC failed in 'AjaxControlToolkit.dll'. File is broken.
使用7zip手动压缩后可以正常解压。
是否有人遇到过DotNetZip不能正确压缩简单二进制文件的情况?或者是我在错误地使用DotNetZip?
https://dl.dropbox.com/u/65419748/AjaxControlToolkit.dll
using System.IO;
using Ionic.Zip;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
var source = new FileInfo(@"C:\ZipDemo\AjaxControlToolkit.dll");
var target = new FileInfo(Path.ChangeExtension(source.FullName, "zip"));
var folder = new DirectoryInfo(Path.ChangeExtension(source.FullName, null));
if (target.Exists)
target.Delete();
if (folder.Exists)
folder.Delete(true);
using (var zip = new ZipFile(target.FullName))
{
zip.AddFile(source.FullName, string.Empty);
zip.Save();
}
using (var zip = new ZipFile(target.FullName))
zip.ExtractAll(folder.FullName);
}
}
}
抛出:
Unhandled Exception: Ionic.Zip.BadReadException: bad read of entry AjaxControlToolkit.dll from compressed archive.
at Ionic.Zip.ZipEntry._CheckRead(Int32 nbytes)
at Ionic.Zip.ZipEntry.ExtractOne(Stream output)
at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, String password)
at Ionic.Zip.ZipFile._InternalExtractAll(String path, Boolean overrideExtractExistingProperty)
at Ionic.Zip.ZipFile.ExtractAll(String path)
at ConsoleApplication1.Program.Main(String[] args) in C:\ZipDemo\ConsoleApplication1\ConsoleApplication1\Program.cs:line 27
编辑:
如果我添加一个额外的字节,它就能正常工作,但这不是一个可接受的解决方案。没有 + 1
会失败。
var bytes = new byte[source.Length + 1];
File.ReadAllBytes(source.FullName).CopyTo(bytes, 0);
zip.AddEntry(source.Name, bytes);
更新:
放弃使用DotNetZip,转而使用SharpZipLib进行简单提取,但是知道DotNetZip出了什么问题肯定会更好,因为它拥有更好的API。
更新2:
文件长度似乎导致问题,1179647和1179649字节可以正确压缩和解压缩。
var source = new FileInfo(@"C:\ZipDemo\foo.txt");
using (var writer = source.CreateText())
writer.Write(new string('a', 1179648));