.netcompressionsharpziplib

Using SharpZipLib to unzip specific files?


I'm trying to use SharpZipLib to pull specified files from a zip archive. All of the examples I've seen always expect that you want to unzip the entire zip, and do something along the lines of:

       FileStream fileStreamIn = new FileStream (sourcePath, FileMode.Open, FileAccess.Read);

        ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
        ZipEntry entry;

        while (entry = zipInStream.GetNextEntry() != null)
        {
            // Unzip file
        }

What I want to do is something like:

ZipEntry entry = zipInStream.SeekToFile("FileName");

As my needs involve using a zip as a package and only grabbing files into memory as needed.

Is anyone familiar with SharpZipLib? Does anyone know if I can do this without running through the entire zip by hand?


Solution

  • ZipFile.GetEntry should do the trick:

    using (var fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
    using (var zf = new ZipFile(fs)) {
       var ze = zf.GetEntry(fileName);
       if (ze == null) {
          throw new ArgumentException(fileName, "not found in Zip");
       }
    
       using (var s = zf.GetInputStream(ze)) {
          // do something with ZipInputStream
       }
    }