Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Split Zip Files With DotNetZip

Tags:

c#

zip

dotnetzip

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();  
    }
like image 939
riteshkp Avatar asked Aug 13 '26 21:08

riteshkp


1 Answers

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.

like image 184
Michal Kandel Avatar answered Aug 15 '26 09:08

Michal Kandel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!