C# 通过 DotNetZip 库以编程方式提取 ZIP 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2324626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract a ZIP file programmatically by DotNetZip library?
提问by Ehsan
I have a function that get a ZIP file and extract it to a directory (I use DotNetZiplibrary.)
我有一个获取 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. But I want to extract only some of these files, not all of them.
我的 ZIP 文件包含多个文件和目录。但我只想提取其中的一些文件,而不是全部。
How can I make this work?
我怎样才能使这项工作?
采纳答案by Oded
You need to test each ZipEntryto 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);
}
}
.
.
ZipFile zip = ZipFile.Read(zipFileName); //Runs in framework 4.5
回答by misaxi
There is a ExtractSelectedEntries method in ZipFile class. here's the method signature.
ZipFile 类中有一个 ExtractSelectedEntries 方法。这是方法签名。
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. 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.
您可以将条件与连词 AND 或 OR 结合使用。使用类似“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 文档和库。
回答by Cheeso
You can also use LINQ to select which entries you want to extract. For example:
您还可以使用 LINQ 来选择要提取的条目。例如:
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
子句中使用任何您想要的查询条件。