簡體   English   中英

使用Ionic.zip從字節數組讀取zip文件

[英]Reading zip file from byte array using Ionic.zip

我有一段代碼可以解壓縮字節數組:

public static byte[] Decompress(this byte[] data)
{
    using (ZipFile zout = ZipFile.Read(data))
    {
        ZipEntry entry = zout.FirstOrDefault();
        Assert.ObjectIsNotNull(entry, "Unable to find default ZIP entry");
        MemoryStream zos = new MemoryStream();
        entry.Extract(zos);
        return zos.ToArray();
    }
}

我已升級到最新版本的Ionic.zip,現在出現以下錯誤:

無法將byte []轉換為字符串。

重載ZipFile.Read(byte[])在最新版本中不再可用。

如何從字節數組讀取zip文件?

ZipFile.Read方法使用文件名或流來讀取,因此您需要提供一個流以供其讀取:

using (MemoryStream stream = new MemoryStream(data))
using (ZipFile zout = ZipFile.Read(stream))
{
    // ....

您可以使用System.IO.Commpression的內置ZipArchive類。

using(var stream = new MemoryStream(data))
{
    using(var archive = new ZipArchive(stream))
    {
        // Use the archive
    }
 }

ZipArchive https://msdn.microsoft.com/zh-cn/library/hh158268(v=vs.110).aspx

MemoryStream https://msdn.microsoft.com/zh-cn/library/e55f3s5k(v=vs.110).aspx

您將需要添加對System.IO.Compression的引用,該引用不在mscorlib

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM