我有一个内存流,其中包含 byte[]
格式的 zip 文件。有什么方法可以解压缩这个内存流,而不需要将文件写入磁盘?
一般来说,我使用 ICSharpCode.SharpZipLib.Zip.FastZip
来解压缩文件,但是有什么方法可以解压缩内存流,也许是通过将文件存储在另一个 MemoryStream
或根据 zip 中存在的文件/文件夹采用 byte[]
格式?
在这种情况下我可以使用内存映射文件功能吗?
最佳答案
是的,.Net 4.5 now supports more Zip functionality .
这是根据您的描述编写的代码示例。
在您的项目中,右键单击 References 文件夹并添加对 System.IO.Compression
的引用using System.IO.Compression;
Stream data = new MemoryStream(); // The original data
Stream unzippedEntryStream; // Unzipped data from a file in the archive
ZipArchive archive = new ZipArchive(data);
foreach (ZipArchiveEntry entry in archive.Entries)
{
if(entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
unzippedEntryStream = entry.Open(); // .Open will return a stream
// Process entry data here
}
}
希望这对您有所帮助。
关于c# - 解压缩 MemoryStream(包含 zip 文件)并获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12715945/