Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 383
FlySwat Avatar asked Nov 30 '08 02:11

FlySwat


People also ask

How do I unzip a file in Rstudio?

To unzip the file, use unzip(). To tar the file, use tar(). To untar the file, use untar().

How do I use unzip?

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.


2 Answers

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    } } 
like image 95
Mark Brackett Avatar answered Oct 09 '22 11:10

Mark Brackett


FastZip.ExtractZip (string zipFileName, string targetDirectory, string fileFilter) 

can extract one or more files based on a file filter (ie regular expressoin string)

Here's the doc regarding the file filter:

// A filter is a sequence of independant <see cref="Regex">regular expressions</see> separated by semi-colons ';' // Each expression can be prefixed by a plus '+' sign or a minus '-' sign to denote the expression // is intended to include or exclude names.  If neither a plus or minus sign is found include is the default // A given name is tested for inclusion before checking exclusions.  Only names matching an include spec // and not matching an exclude spec are deemed to match the filter. // An empty filter matches any name. // </summary> // <example>The following expression includes all name ending in '.dat' with the exception of 'dummy.dat' // "+\.dat$;-^dummy\.dat$" 

so for a file named myfile.dat you could use "+.*myfile\.dat$" as your file filter.

like image 42
Todd Smith Avatar answered Oct 09 '22 12:10

Todd Smith