8

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

4

1 回答 1

13

是的,可以使用 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);
        }*/
      }            
    }
  }
}

在上面的示例中,我使用 aMemoryStream将其存储ZipEntry在内存中(以供进一步分析)。您还可以将ZipEntry(如果满足某些条件)存储在磁盘上。

希望这可以帮助。

于 2011-11-29T19:17:50.883 回答