SharpZipLib Examine and select contents of a ZIP file

强颜欢笑 提交于 2019-12-12 10:43:28

问题


I am using SharpZipLib in a project and am wondering if it is possible to use it to look inside a zip file, and if one of the files within has a data modified in a range I am searching for then to pick that file out and copy it to a new directory? Does anybody know id this is possible?


回答1:


Yes, it is possible to enumerate the files of a zip file using SharpZipLib. You can also pick files out of the zip file and copy those 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 example above I use a MemoryStream to store the ZipEntry in memory (for further analysis). You could also store the ZipEntry (if it meets certain criteria) on disk.

Hope, this helps.



来源:https://stackoverflow.com/questions/8313791/sharpziplib-examine-and-select-contents-of-a-zip-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!