简体   繁体   English

通过 DotNetZip 库以编程方式提取 ZIP 文件?

[英]Extract a ZIP file programmatically by DotNetZip library?

I have a function that get a ZIP file and extract it to a directory (I use DotNetZip library.)我有一个获取 ZIP 文件并将其解压缩到目录的函数(我使用DotNetZip库。)

public void ExtractFileToDirectory(string zipFileName, string outputDirectory)
{
     ZipFile zip = ZipFile.Read(zipFileName);
     Directory.CreateDirectory(outputDirectory);
     zip.ExtractAll(outputDirectory,ExtractExistingFileAction.OverwriteSilently);
}

My ZIP file contains multiple files and directories.我的 ZIP 文件包含多个文件和目录。 But I want to extract only some of these files, not all of them.但我只想提取其中的一些文件,而不是全部。

How can I make this work?我怎样才能使这项工作?

You need to test each ZipEntry to see if you want to extract it:您需要测试每个ZipEntry以查看是否要提取它:

public void ExtractFileToDirectory(string zipFileName, string outputDirectory)
{
     ZipFile zip = ZipFile.Read(zipFileName);
     Directory.CreateDirectory(outputDirectory);
      foreach (ZipEntry e in zip)
      {
        // check if you want to extract e or not
        if(e.FileName == "TheFileToExtract") 
          e.Extract(outputDirectory, ExtractExistingFileAction.OverwriteSilently);
      }
}

There is a ExtractSelectedEntries method in ZipFile class. ZipFile 类中有一个 ExtractSelectedEntries 方法。 here's the method signature.这是方法签名。

public void ExtractSelectedEntries(string selectionCriteria, string directoryPathInArchive, string extractDirectory, ExtractExistingFileAction extractExistingFile)

So in your program, you can simply extract the specified files by providing the selectionCriteria.因此,在您的程序中,您可以通过提供 selectionCriteria 来简单地提取指定的文件。

public void ExtractFileToDirectory(string zipFileName, string outputDirectory)
{
     ZipFile zip = ZipFile.Read(zipFileName);
     Directory.CreateDirectory(outputDirectory);
     zip.ExtractSelectedEntries("name = *.doc", "document\", outputDirectory, ExtractExistingFileAction.OverwriteSilently);
}

You can combine criteria with the conjunctions AND or OR.您可以将条件与连词 AND 或 OR 结合使用。 Using a string like "name = *.txt AND size >= 100k" for the selectionCriteria retrieves entries whose names end in .txt, and whose uncompressed size is greater than or equal to 100 kilobytes.使用类似“name = *.txt AND size >= 100k”的字符串作为 selectionCriteria 将检索名称以 .txt 结尾且未压缩大小大于或等于 100 KB 的条目。

here are some criteria samples这里有一些标准样本


criteria (Files retrieved)标准(检索的文件)

name != *.xls (any file with an extension that is not .xls) name != *.xls(扩展名不是 .xls 的任何文件)

name = *.mp3 (any file with a .mp3 extension)名称 = *.mp3(任何带有 .mp3 扩展名的文件)

*.mp3 (same as above, any file with a .mp3 extension) *.mp3(同上,任何带有 .mp3 扩展名的文件)

attributes = A (all files whose attributes include the Archive bit)属性 = A(属性包括存档位的所有文件)

attributes != H (all files whose attributes do not include the Hidden bit)属性 != H(属性不包括隐藏位的所有文件)

mtime > 2009-01-01 (all files with a last modified time after January 1st, 2009) mtime > 2009-01-01(最后修改时间在2009年1月1日之后的所有文件)

size > 2gb (all files whose uncompressed size is greater than 2gb)大小 > 2gb(所有未压缩大小大于 2gb 的文件)


For more reference, you should read the API document alone with the library.如需更多参考,您应该单独阅读 API 文档和库。

You can also use LINQ to select which entries you want to extract.您还可以使用 LINQ 来选择要提取的条目。 For example:例如:

using (var zip = ZipFile.Read(ArchiveToRead))
{
    var selection = from e in zip.Entries
        where System.IO.Path.GetFileName(e.FileName).StartsWith("C")
        select e;

    foreach (var e in selection)
        e.Extract(extractDir);
}

Of course you can use whatever query criteria you want in the where clause.当然,您可以在where子句中使用您想要的任何查询条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM