SharpZipLib检查并选择ZIP文件的内容

时间:2011-11-29 15:58:55

标签: c# zip sharpziplib

我在项目中使用SharpZipLib,我想知道是否可以使用它来查看zip文件,如果其中一个文件中有一个数据在我正在搜索的范围内被修改,那么就选择该文件out并将其复制到新目录?有人知道这是可能的吗?

1 个答案:

答案 0 :(得分:9)

是的,可以使用SharpZipLib枚举zip文件的文件。您还可以从zip文件中选择文件,并将这些文件复制到磁盘上的目录中。

这是一个小例子:

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);
        }*/
      }            
    }
  }
}

在上面的示例中,我使用MemoryStreamZipEntry存储在内存中(供进一步分析)。您还可以在磁盘上存储ZipEntry(如果它符合特定条件)。

希望,这有帮助。

相关问题