3

I am trying to extract files from zip files using the DotNetZip library. I am able to extract files when it is a single .zip file. However, when I try to extract files from a multi volume zip file like Something.zip.0 or Something.zip.1, I get the following two exceptions:

-Exception thrown: 'Ionic.Zip.BadReadException' in Ionic.Zip.dll

-Exception thrown: 'Ionic.Zip.ZipException' in Ionic.Zip.dll

Is it possible for DotNetZip to read these type of files, or should I be looking into an alternative approach? I am working on Visual Studios using C#.

Here's a snippet of how I implement my zip file extraction.

using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(_pathToZip))
    {
        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
        foreach(Ionic.Zip.ZipEntry ze in zip)
            {
                string fileName = ze.FileName;
                bool isThereItemToExtract = isThereMatch(fileName.ToLower(), _folderList, _fileList);
                if (isThereItemToExtract)
                {    
                    string pathOfFileToExtract = (_destinationPath + "\\" + ze.FileName).Replace('/', '\\'); 
                    string pathInNewZipFile = goUpOneDirectoryRelative(ze.FileName);  
                    ze.Extract(_destinationPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);   
                    _newZip.AddItem(pathOfFileToExtract, pathInNewZipFile);   
                }
            }
        _newZip.Save();  
    }
riteshkp
  • 31
  • 4

1 Answers1

2

Please refer the DotNetZipLibrary code examples:

using Ionic.Zip;

private void MyExtract(string zipToUnpack, string unpackDirectory)
{
    using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
    {
          // here, we extract every entry, but we could extract conditionally
          // based on entry name, size, date, checkbox status, etc.  
         foreach (ZipEntry e in zip1)
         {
            e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
         }
     }
  }

This method should be able to extract either split and not split zip files. Every zip entry will be extracted with its full path as specified in the zip archive, relative to the current unpackDirectory.

  • There's no need to check if zip entry exsists (isThereItemToExtract). Interating the zip entries with foreach should do the job.
  • To avoid collisions you need to check if file with same name as zipEntry exsists in the unpackDirectory, or use ExtractExistingFileAction.OverwriteSilently flag.

Is it possible for DotNetZip to read these type of files, or should I be looking into an alternative approach? I am working on Visual Studios using C#.

In my experience, this is the best library to deal with split zip files.

Michal Kandel
  • 303
  • 2
  • 10
  • Thanks for the response @michal-kandel. I have tried this approach and it did not work for the split zip files I am working with. However, I was able to solve this issue by calling on 7z executable. – riteshkp Jul 16 '18 at 20:26
  • I can only see a place to pass the path to a single ZIP file in this code example. I've tried this with the first and last parts as well as with ".*" just to try it. None of these work. Shouldn't I need to pass in a collection of path strings? – billpg Jan 12 '21 at 17:04
  • The Ionic.Zip infrastructure will be able to find and read them by itself. Just make sure you store all the split parts at the same location as the main zip file and keep their original naming. – Michal Kandel Jul 11 '21 at 15:00