首页 > 解决方案 > 如何在 C# 中验证多部分压缩(即 zip)文件是否包含所有部分?

问题描述

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

我还参考了一个DotNetZip相关问题。

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

在此处输入图像描述

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

在此处输入图像描述

另一件事是,我还想测试它是否也已损坏或不像 7z 软件。请参阅下面的屏幕截图了解我的要求。

在此处输入图像描述

请帮我解决这些问题。

标签: c#zip7zipdotnetzipcompressed-files

解决方案


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

我用过 nuget Package 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.Open) as Stream).ToList()), ZipArchiveMode.Read))
        {
            // Do whatever you want
        }
    }
    catch(InvalidDataException ex)
    {
        return false;
    }

    return true;
}

我不确定这是否是您要查找的内容,或者您​​需要错误中的更多详细信息。但是希望这可以帮助您解决问题。


推荐阅读