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?
To unzip the file, use unzip(). To tar the file, use tar(). To untar the file, use untar().
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.
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 } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With