我正在使用Ionic.Zip使用此方法将ZipFile提取到内存流:
private MemoryStream GetReplayZipMemoryStream()
{
MemoryStream zipMs = new MemoryStream();
using (ZipFile zip = ZipFile.Read(myFile.zip))
{
foreach (ZipEntry zipEntry in zip)
{
if (zipEntry.FileName.StartsWith("Aligning") || zipEntry.FileName.StartsWith("Sensing"))
{
zipEntry.Extract(zipMs);
}
}
}
zipMs.Seek(0, SeekOrigin.Begin);
return zipMs;
}
完成提取后,我想读取流并根据条目文件名获取一些条目。我该怎么做?
我尝试使用以下代码调用,但ZipFile.Read(ms)
出现错误,上面写着:
无法将其读取为压缩文件
Stream ms = GetReplayZipMemoryStream();
using (ZipFile zip = ZipFile.Read(ms))
{
ZipEntry imageEntry = zip.Entries.First(x => x.FileName.EndsWith(".png"));
using (Stream s = imageEntry.OpenReader())
{
var image = Image.FromStream(s);
pictureBox1.Image = image;
}
}
提前感谢您的帮助!
这可能是有点老的问题和迟到的答案,但我做了一些事情来获取文件作为字节集合及其文件名,如下所示
public static Dictionary<string, byte[]> Decompress(Stream targFileStream)
{
Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
using(ZipFile z = ZipFile.Read(targFileStream))
{
foreach (ZipEntry zEntry in z)
{
MemoryStream tempS = new MemoryStream();
zEntry.Extract(tempS);
files.Add(zEntry.FileName, tempS.ToArray());
}
}
return files;
}