Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
315 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...