[英]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.