如何验证多部分压缩(即zip)文件在C#中是否具有所有部分?

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

我想验证像Zip这样的多部分压缩文件,因为当压缩文件缺少任何部分时,都会引发错误,但是我想在提取之前验证它,并且不同的软件会创建不同的命名结构。

我也提到一个DotNetZip相关问题。

下面的屏幕截图来自7z软件。

enter image description here

第二张屏幕截图来自C#的DotNetZip。

enter image description here

还有一件事,我还想测试它是否也损坏或不像7z软件那样。请参考以下屏幕截图,了解我的要求。

enter image description here

请帮助我解决这些问题。

c# zip 7zip dotnetzip compressed-files
1个回答
0
投票

我不确定您是否能够看到快照中显示的确切错误。但是我有一个代码,可以帮助您查找多部分文件是否可读。

我使用了nuget软件包CombinationStream

如果流不可读,则ZipArchive构造函数将抛出ArgumentExceptionInvalidDataException

下面是代码:

public static bool IsZipValid()
{
    try
    {
        string basePath = @"C:\multi-part-zip\";
        List<string> files = new List<string> {
                                basePath + "somefile.zip.001",
                                basePath + "somefile.zip.002",
                                basePath + "somefile.zip.003",
                                basePath + "somefile.zip.004",
                                basePath + "somefile.zip.005",
                                basePath + "somefile.zip.006",
                                basePath + "somefile.zip.007",
                                basePath + "somefile.zip.008"
                            };

        using (var zipFile = new ZipArchive(new CombinationStream(files.Select(x => new FileStream(x, FileMode
        {
            // Do whatever you want
        }
    }
    catch(InvalidDataException ex)
    {
        return false;
    }

    return true;
}

我不确定这是您要找的内容,还是需要更多有关错误的详细信息。但是希望这可以帮助您解决问题。

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