解压 ZIP 文件时 NET Core 抛出 InvalidDataException,Windows 资源管理器解压成功

问题描述 投票:0回答:1

我正在尝试解压缩嵌套的 zip 文件,但我得到了

InvalidDataException
。当我尝试在 Windows 资源管理器中解压缩同一文件时,它解压缩成功。为什么 Windows 资源管理器能够解压缩它而不是 .NET Core 压缩库?

我怀疑 zip 文件有问题,但如果 Windows 资源管理器能够做到这一点,那么在 .NET Core 项目中一定可以做到。

我尝试解压缩父 zip 文件,但这会引发

InvalidDataException 'A local file header is corrupt.'

ZipFile.ExtractToDirectory("parent.zip", outputFolder) // throws exception

我尝试打开父 zip 文件并解压缩嵌套的 zip 文件,但这也会引发

InvalidDataException 'End of Central Directory record could not be found.'

using (var archive = ZipFile.OpenRead("parent.zip"))
{
    var nestedZip = archive.GetEntry("nested.zip");
    
    using (var stream = nestedZip.Open())
    using (var nestedArchive = new Archive(stream)) // throws exception
    {
        nestedArchive.ExtractToDirectory(outputFolder)
    }
}
c# .net-core zip unzip c#-ziparchive
1个回答
0
投票

使用不同的 zip 文件尝试该场景,看看是否有效,如果有效则意味着 zip 文件存在问题。

还可以尝试使用不同的库。

© www.soinside.com 2019 - 2024. All rights reserved.