SharpZipLib Examine and select the contents of the ZIP file

I use SharpZipLib in a project, and I wonder if it can be used to search in a zip file, and if one of the files inside has data changed in a range, I am looking to select this file and copy it to a new directory? Does anyone know that this is possible?

+6
source share
1 answer

Yes, you can list zip file files using SharpZipLib. You can also select files from a zip file and copy these files to a directory on your disk.

Here is a small example:

using (var fs = new FileStream(@"c:\temp\test.zip", FileMode.Open, FileAccess.Read)) { using (var zf = new ZipFile(fs)) { foreach (ZipEntry ze in zf) { if (ze.IsDirectory) continue; Console.Out.WriteLine(ze.Name); using (Stream s = zf.GetInputStream(ze)) { byte[] buf = new byte[4096]; // Analyze file in memory using MemoryStream. using (MemoryStream ms = new MemoryStream()) { StreamUtils.Copy(s, ms, buf); } // Uncomment the following lines to store the file // on disk. /*using (FileStream fs = File.Create(@"c:\temp\uncompress_" + ze.Name)) { StreamUtils.Copy(s, fs, buf); }*/ } } } } 

In the above example, I use a MemoryStream to store a ZipEntry in memory (for further analysis). You can also save ZipEntry (if it meets certain criteria) to disk.

Hope this helps.

+8
source

Source: https://habr.com/ru/post/902663/


All Articles