【发布时间】:2011-08-23 11:55:14
【问题描述】:
.Net (C#) 中是否有从 zip 文件中提取数据而不解压缩完整文件的方法?
我可能想从 zip 文件的开头提取数据(文件),如果压缩算法压缩使用的文件是确定性的顺序。
【问题讨论】:
-
但是另一个线程是 12 岁。也许过时了?
标签: c# zip compression
.Net (C#) 中是否有从 zip 文件中提取数据而不解压缩完整文件的方法?
我可能想从 zip 文件的开头提取数据(文件),如果压缩算法压缩使用的文件是确定性的顺序。
【问题讨论】:
标签: c# zip compression
使用 .Net Framework 4.5(使用 ZipArchive):
using (ZipArchive zip = ZipFile.Open(zipfile, ZipArchiveMode.Read))
foreach (ZipArchiveEntry entry in zip.Entries)
if(entry.Name == "myfile")
entry.ExtractToFile("myfile");
在 zipfile 中找到“myfile”并解压。
【讨论】:
System.IO.Compression.dll 和 System.IO.Compression.FileSystem.dll
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 已关闭。旧档案仍然是available at Codeplex。看起来代码已经迁移到Github了:
【讨论】:
如果你想使用 SharpZipLib,这样的东西会一一列出并提取文件:
var zip = new ZipInputStream(File.OpenRead(@"C:\Users\Javi\Desktop\myzip.zip"));
var filestream = new FileStream(@"C:\Users\Javi\Desktop\myzip.zip", FileMode.Open, FileAccess.Read);
ZipFile zipfile = new ZipFile(filestream);
ZipEntry item;
while ((item = zip.GetNextEntry()) != null)
{
Console.WriteLine(item.Name);
using (StreamReader s = new StreamReader(zipfile.GetInputStream(item)))
{
// stream with the file
Console.WriteLine(s.ReadToEnd());
}
}
基于此示例:content inside zip file
【讨论】:
以下是如何将 UTF8 文本文件从 zip 存档读取到字符串变量(.NET Framework 4.5 及更高版本):
string zipFileFullPath = "{{TypeYourZipFileFullPathHere}}";
string targetFileName = "{{TypeYourTargetFileNameHere}}";
string text = new string(
(new System.IO.StreamReader(
System.IO.Compression.ZipFile.OpenRead(zipFileFullPath)
.Entries.Where(x => x.Name.Equals(targetFileName,
StringComparison.InvariantCulture))
.FirstOrDefault()
.Open(), Encoding.UTF8)
.ReadToEnd())
.ToArray());
【讨论】:
Zip 文件有一个目录。每个 zip 实用程序都应该能够仅查询 TOC。或者您可以使用 7zip -t 之类的命令行程序打印目录并将其重定向到文本文件。
【讨论】:
在这种情况下,您将需要解析 zip 本地标头条目。 zip文件中存储的每个文件都有前面的Local File Header条目,其中(通常)包含足够的信息进行解压缩,一般可以对流中的此类条目进行简单解析,选择需要的文件,将header +压缩文件数据复制到其他文件,然后在该部分调用 unzip(如果您不想处理整个 Zip 解压缩代码或库)。
【讨论】:
以下代码可以将特定文件读取为字节数组:
using ZipArchive zipArchive = ZipFile.OpenRead(zipFilePath);
foreach(ZipArchiveEntry zipArchiveEntry in zipArchive.Entries)
{
if(zipArchiveEntry.Name.Equals(fileName,StringComparison.OrdinalIgnoreCase))
{
Stream stream = zipArchiveEntry.Open();
using MemoryStream memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
}
【讨论】: