gpt4 book ai didi

c# - .Extract 上的 DotNetZip BadReadException

转载 作者:IT王子 更新时间:2023-10-29 04:39:00 27 4
gpt4 key购买 nike

我有一个奇怪的文件,当使用 DotNetZip 压缩时,它创建了一个“无法解压缩”的存档。当我尝试使用 7zip 解压缩它时,它失败并显示 CRC failed in 'AjaxControlToolkit.dll'。文件已损坏。 当我用 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);

更新:

放弃并切换到 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));

最佳答案

您的 dll 大小为 53*128k (6954496/131072 = 53),并且 DotNetZip 中存在错误,您可以在此处阅读:https://dotnetzip.codeplex.com/workitem/14087 .只需在您的代码中使用:

zip.ParallelDeflateThreshold = -1;

我的很多文件都遇到了这个问题,现在它工作得很好 ;)

关于c# - .Extract 上的 DotNetZip BadReadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15337186/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com