解压缩内存流(包含zip文件)并获取文件

use*_*791 37 c#

我有一个包含byte []格式的zip文件的内存流.

有什么方法可以解压缩这个内存流,而无需将文件写入磁盘?

一般情况下我使用ICSharpCode.SharpZipLib.Zip.FastZip来解压缩文件,但是解压内存流的方法是什么?并根据zip中的文件/文件夹将文件存储在另一个内存流或byte []格式中?

我可以在这种情况下使用Memorymapped文件功能吗?

gig*_*uck 95

是的,.Net 4.5现在支持更多Zip功能.

这是一个基于您的描述的代码示例.

在项目中,右键单击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
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Bob*_*son 16

我们使用DotNetZip,我可以将zip文件的内容从Stream内存解压缩到内存中.下面是用于从stream(LocalCatalogZip)中提取特定命名文件并返回流以读取该文件的示例代码,但它很容易在其上展开.

private static MemoryStream UnZipCatalog()
{
    MemoryStream data = new MemoryStream();
    using (ZipFile zip = ZipFile.Read(LocalCatalogZip))
    {
        zip["ListingExport.txt"].Extract(data);
    }
    data.Seek(0, SeekOrigin.Begin);
    return data;
}
Run Code Online (Sandbox Code Playgroud)

它不是您现在使用的库,但如果您可以更改,则可以获得该功能.


这是一个变量,它将返回Dictionary<string,MemoryStream>一个zip文件的每个文件的内容.

private static Dictionary<string,MemoryStream> UnZipToMemory()
{
    var result = new Dictionary<string,MemoryStream>();
    using (ZipFile zip = ZipFile.Read(LocalCatalogZip))
    {
        foreach (ZipEntry e in zip)
        {
            MemoryStream data = new MemoryStream();
            e.Extract(data);
            result.Add(e.FileName, data);
        }
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)


kip*_*r_t 10

我刚刚遇到了类似的问题,我发现我认为似乎相当优雅的答案是使用#ZipLib(可以使用nuget)并执行以下操作:

private byte[] GetUncompressedPayload(byte[] data)
{
    using (var outputStream = new MemoryStream())
    using (var inputStream = new MemoryStream(data))
    {
        using (var zipInputStream = new ZipInputStream(inputStream))
        {
            zipInputStream.GetNextEntry();
            zipInputStream.CopyTo(outputStream);
        }
        return outputStream.ToArray();
    }
}
Run Code Online (Sandbox Code Playgroud)

这似乎是一种享受.希望这可以帮助.


Sco*_*ain 5

是的,使用FastZipTo进行更改new ZipFile(stream),但这仅在您的流可以搜索时才有效。(只需使用MemoryStream new ZipFile(fs);而不是像示例一样读取文件流。)

C#
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

public void ExtractZipFile(string archiveFilenameIn, string password, string outFolder) {
    ZipFile zf = null;
    try {
        FileStream fs = File.OpenRead(archiveFilenameIn);
        zf = new ZipFile(fs);
        if (!String.IsNullOrEmpty(password)) {
            zf.Password = password;     // AES encrypted entries are handled automatically
        }
        foreach (ZipEntry zipEntry in zf) {
            if (!zipEntry.IsFile) {
                continue;           // Ignore directories
            }
            String entryFileName = zipEntry.Name;
            // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
            // Optionally match entrynames against a selection list here to skip as desired.
            // The unpacked length is available in the zipEntry.Size property.

            byte[] buffer = new byte[4096];     // 4K is optimum
            Stream zipStream = zf.GetInputStream(zipEntry);

            // Manipulate the output filename here as desired.
            String fullZipToPath = Path.Combine(outFolder, entryFileName);
            string directoryName = Path.GetDirectoryName(fullZipToPath);
            if (directoryName.Length > 0)
                Directory.CreateDirectory(directoryName);

            // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
            // of the file, but does not waste memory.
            // The "using" will close the stream even if an exception occurs.
            using (FileStream streamWriter = File.Create(fullZipToPath)) {
                StreamUtils.Copy(zipStream, streamWriter, buffer);
            }
        }
    } finally {
        if (zf != null) {
            zf.IsStreamOwner = true; // Makes close also shut the underlying stream
            zf.Close(); // Ensure we release resources
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是不可搜索的流,请使用ZipInputStream。

// Calling example:
    WebClient webClient = new WebClient();
    Stream data = webClient.OpenRead("http://www.example.com/test.zip");
    // This stream cannot be opened with the ZipFile class because CanSeek is false.
    UnzipFromStream(data, @"c:\temp");

public void UnzipFromStream(Stream zipStream, string outFolder) {

    ZipInputStream zipInputStream = new ZipInputStream(zipStream);
    ZipEntry zipEntry = zipInputStream.GetNextEntry();
    while (zipEntry != null) {
        String entryFileName = zipEntry.Name;
        // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
        // Optionally match entrynames against a selection list here to skip as desired.
        // The unpacked length is available in the zipEntry.Size property.

        byte[] buffer = new byte[4096];     // 4K is optimum

        // Manipulate the output filename here as desired.
        String fullZipToPath = Path.Combine(outFolder, entryFileName);
        string directoryName = Path.GetDirectoryName(fullZipToPath);
        if (directoryName.Length > 0)
            Directory.CreateDirectory(directoryName);

        // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
        // of the file, but does not waste memory.
        // The "using" will close the stream even if an exception occurs.
        using (FileStream streamWriter = File.Create(fullZipToPath)) {
            StreamUtils.Copy(zipInputStream, streamWriter, buffer);
        }
        zipEntry = zipInputStream.GetNextEntry();
    }
}
Run Code Online (Sandbox Code Playgroud)

摘自ICSharpCode Wiki的示例