我正在尝试使用 SharpZipLib 从 zip 存档中提取指定的文件。我见过的所有示例都希望您解压缩整个 zip,然后执行以下操作:
FileStream fileStreamIn = new FileStream (sourcePath, FileMode.Open, FileAccess.Read);
ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
ZipEntry entry;
while (entry = zipInStream.GetNextEntry() != null)
{
// Unzip file
}
我想做的是:
ZipEntry entry = zipInStream.SeekToFile("FileName");
因为我的需要涉及使用 zip 作为包,并且只根据需要将文件抓取到内存中。
有人熟悉SharpZipLib吗?有谁知道我是否可以在不用手穿过整个 zipper 的情况下做到这一点?
最佳答案
ZipFile.GetEntry 应该可以解决问题:
using (var fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
using (var zf = new ZipFile(fs)) {
var ze = zf.GetEntry(fileName);
if (ze == null) {
throw new ArgumentException(fileName, "not found in Zip");
}
using (var s = zf.GetInputStream(ze)) {
// do something with ZipInputStream
}
}
关于.net - 使用 SharpZipLib 解压缩特定文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/328343/