下面代码是对网上代码精简,网上代码,先下载zip然后解压到指定目录,而下面代码是解压直接得到纹理贴图,取消了写入磁盘过程,而直接放在内存中
icsharpcode.sharpziplib 下载地址 : https://www.zhaodll.com/dll/i/201206/43009.html
网上很多提供得地址下载得文件csharpcode.sharpziplib 都报错,此时,请用我提供得下载地址下载,选用版本是:0.84.0.0
我用的unity版本:2018.2.6f1
如果你是下载后直接使用,不打算将解压的文件保存到本地,那么本篇文章就很有用,写入磁盘会导致程序体验变差,并且程序稍微有些卡
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ICSharpCode.SharpZipLib.Zip;
public static class ZipUtility
{
#region UnzipCallback
public abstract class UnzipCallback
{
/// <summary>
/// 解压单个文件或文件夹前执行的回调
/// </summary>
/// <param name="_entry"></param>
/// <returns>如果返回true,则压缩文件或文件夹,反之则不压缩文件或文件夹</returns>
public virtual bool OnPreUnzip(ZipEntry _entry)
{
return true;
}
/// <summary>
/// 解压单个文件或文件夹后执行的回调
/// </summary>
/// <param name="_entry"></param>
public virtual void OnPostUnzip(ZipEntry _entry) { }
/// <summary>
/// 解压执行完毕后的回调
/// </summary>
/// <param name="_result">true表示解压成功,false表示解压失败</param>
public virtual void OnFinished(bool _result) { }
}
#endregion
/// <summary>
/// 解压Zip包
/// </summary>
/// <param name="_fileBytes">Zip包字节数组</param>
/// <param name="_outputPath">解压输出路径</param>
/// <param name="_password">解压密码</param>
/// <param name="_unzipCallback">UnzipCallback对象,负责回调</param>
/// <returns></returns>
public static bool UnzipFile(byte[] _fileBytes, string _password = null, UnzipCallback _unzipCallback = null)
{
if (null == _fileBytes)
{
if (null != _unzipCallback)
_unzipCallback.OnFinished(false);
return false;
}
bool result = UnzipFile(new MemoryStream(_fileBytes), _password, _unzipCallback);
if (!result)
{
if (null != _unzipCallback)
_unzipCallback.OnFinished(false);
}
return result;
}
/// <summary>
/// 解压Zip包
/// </summary>
/// <param name="_inputStream">Zip包输入流</param>
/// <param name="_outputPath">解压输出路径</param>
/// <param name="_password">解压密码</param>
/// <param name="_unzipCallback">UnzipCallback对象,负责回调</param>
/// <returns></returns>
private static bool UnzipFile(Stream _inputStream, string _password = null, UnzipCallback _unzipCallback = null)
{
if (null == _inputStream)
{
if (null != _unzipCallback)
_unzipCallback.OnFinished(false);
return false;
}
// 解压Zip包
ZipEntry entry = null;
using (ZipInputStream zipInputStream = new ZipInputStream(_inputStream))
{
if (!string.IsNullOrEmpty(_password))
zipInputStream.Password = _password;
while (null != (entry = zipInputStream.GetNextEntry()))
{
if (string.IsNullOrEmpty(entry.Name))
continue;
if ((null != _unzipCallback) && !_unzipCallback.OnPreUnzip(entry))
continue; // 过滤
byte[] bytes = new byte[1024];
List<byte> mm = new List<byte>();
while (true)
{
int count = zipInputStream.Read(bytes, 0, bytes.Length);
if (count > 0)
{
for(int i = 0; i < count; i++)
{
mm.Add(bytes[i]);
}
}
else
{
if (mm.Count > 0)
{
byte[] bytess = new byte[mm.Count];
for (int ii = 0; ii < mm.Count; ii++)
{
bytess[ii]=mm[ii];
}
Texture2D tex2d = new Texture2D(256, 256);
tex2d.name = entry.Name;
tex2d.LoadImage(bytess);
SharpZip.tex.Add(entry.Name,tex2d);
}
if (null != _unzipCallback)
_unzipCallback.OnPostUnzip(entry);
break;
}
}
}
}
if (null != _unzipCallback)
_unzipCallback.OnFinished(true);
return true;
}
}
QQ技术交流群:386476712