Search code examples
c#zip7zipdotnetzipcompressed-files

How to validate multi part compressed (i.e zip) files have all parts or not in C#?


I want to validate multipart compressed files like Zip because when any part missing for compressed files then it raises an error, but I want to validate it before extraction and different software creates a different naming structure.

I also refer one DotNetZip related questions.

The below screenshot is from 7z software.

enter image description here

And the second screenshot is from DotNetZip from C#.

enter image description here

One more thing is that I also want to test that it's also corrupted or not like 7z software. Please refer below screenshot for my requirements.

enter image description here

Please help me with these issues.


Solution

  • From your comments I understood that the issue you have is to identify the files (get the list of parts belonging together). You can get a list of files like

    List<string> files = System.IO.Directory.EnumerateFiles(@"D:\Zip\ForExtract\multipart\",
                "500mbInputData.*", SearchOption.TopDirectoryOnly).OrderBy(x => x).ToList();
    

    or for your second case

    List<string> files = System.IO.Directory.EnumerateFiles(@"D:\Zip\ForExtract\multipart\", 
                "500mbInputData.zip.*", SearchOption.TopDirectoryOnly).OrderBy(x => x).ToList();
    

    and then use the file list in your CombinationStream. The rest of the code would look like Manoj Choudhari wrote. You could also put the path and the file name with wild card into a parameter, so I'd suggest to add the following parameters to the function:

    public static bool IsZipValid(string basePath, string fileNameWithWildcard)
    {
        try
        {
            List<string> files = System.IO.Directory.EnumerateFiles(
                        basePath, fileNameWithWildcard, 
                        SearchOption.TopDirectoryOnly).OrderBy(x => x).ToList();
    
            using (var zipFile = // ... rest is as Manoj wrote
    

    and use it like:

    if (IsZipValid(@"D:\Zip\ForExtract\multipart\", "500mbInputData.*")) { // ... }
    

    or

    if (IsZipValid(@"D:\Zip\ForExtract\multipart\", "500mbInputData.zip.*")) { // ... }
    

    To find out which kind of files you have in the basepath, you could write a helper function like

    List<string> getZipFormat(string path)
    {
        bool filesFound(string basePath, string pattern) => System.IO.Directory.EnumerateFiles(
                basePath, pattern, SearchOption.TopDirectoryOnly).Any();
    
        var isTar = filesFound(path, "*.tar.???");
        var isZip = filesFound(path, "*.z??");
        var is7Zip = filesFound(path, "*.7z.???");
    
        var result = new List<string>();
        if (isTar) result.Add("TAR");
        if (isZip) result.Add("ZIP");
        if (is7Zip) result.Add("7ZIP");
        return result;
    }
    

    Modify it to your needs - it will return a list of strings containing "TAR", "ZIP" or "7ZIP" (or more than one of them), depending on the patterns matching against the files in the base directory.

    Usage (example for multi-zipformat check):

        var isValid = true;
        var basePath = @"D:\Zip\ForExtract\multipart\";
        foreach(var fmt in getZipFormat(basePath))
        switch (fmt)
        {
        case "TAR": 
            isValid = isValid & IsZipValid(basePath, "500mbInputData.tar.*");
            break;
        case "ZIP":
            isValid = isValid & IsZipValid(basePath, "500mbInputData.zip.*");
            break;
        case "7ZIP":
            isValid = isValid & IsZipValid(basePath, "500mbInputData.7z.*");
            break;
        default: 
            break;
        }
    

    Note: As per my experiments with this, it could happen that the files remain open although your program has ended - meaning your files will still be locked the next time you run your code. So, I'd strongly suggest to explicitly close them, like

        var fStreams = files.Select(x => 
                new FileStream(x, FileMode.Open) as System.IO.Stream).ToList();
        using (var cStream = new CombinationStream(fStreams))
        using (var zipFile = new ZipArchive(cStream, ZipArchiveMode.Read))
        {
            // Do whatever you want...
    
            // ... but ensure you close the files
            fStreams.Select(s => { s.Close(); return s; });
        };