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

Duk*_*ade 6 c# zip sharpziplib

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

Han*_*ans 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);
        }*/
      }            
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我使用a MemoryStream来存储ZipEntry内存(用于进一步分析).您还可以ZipEntry在磁盘上存储(如果它符合某些条件).

希望这可以帮助.