小编典典

如何从zip文件中读取数据而不必解压缩整个文件

c#

无论如何,.Net(C#)中是否可以从zip文件中提取数据而不解压缩整个文件?

我可能只是想从zip文件的开头提取数据(文件),显然这取决于压缩算法是否以确定的顺序压缩文件。


阅读 518

收藏
2020-05-19

共1个答案

小编典典

DotNetZip是您的朋友在这里。

一样容易:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
  ZipEntry e = zip["MyReport.doc"];
  e.Extract(OutputStream);
}

(您也可以提取到文件或其他目标位置)。

读取zip文件的目录很容易:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
  foreach (ZipEntry e in zip)
  {
    if (header)
    {
      System.Console.WriteLine("Zipfile: {0}", zip.Name);
      if ((zip.Comment != null) && (zip.Comment != "")) 
        System.Console.WriteLine("Comment: {0}", zip.Comment);
      System.Console.WriteLine("\n{1,-22} {2,8}  {3,5}   {4,8}  {5,3} {0}",
                               "Filename", "Modified", "Size", "Ratio", "Packed", "pw?");
      System.Console.WriteLine(new System.String('-', 72));
      header = false;
    }
    System.Console.WriteLine("{1,-22} {2,8} {3,5:F0}%   {4,8}  {5,3} {0}",
                             e.FileName,
                             e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"),
                             e.UncompressedSize,
                             e.CompressionRatio,
                             e.CompressedSize,
                             (e.UsesEncryption) ? "Y" : "N");

  }
}

编辑要注意:
DotNetZip曾经居住在Codeplex。Codeplex已关闭。旧的存档仍可在Codeplex上获得。看起来代码已迁移到Github:


2020-05-19