c# - 解压缩 MemoryStream(包含 zip 文件)并获取文件

标签 c#

我有一个内存流,其中包含 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/

相关文章:

c# - 以编程方式在方法体结束前插入代码行

c# - DriveInfo.DriveFormat 上的设备未就绪错误

c# - C# 中的等效 C 代码。 (uint8_t *) 强制转换?

c# - Visual Studio 2015 更新 3 中缺少 "ASP.NET Core Web Application (.NET Core)"模板?

c# - MySql.Data.MySqlClient.MySqlException : “The host localhost does not support SSL connections.”

c# - 业务逻辑层和数据访问层 : circular dependency

c# - .NET中是否有类似于Qt::QueuedConnection的东西?

c# - ASP.Net MVC 中错误模型的编译时错误

c# - 在对象中转换匿名类型并检索一个字段

C# UWP 异步绑定(bind)