要注意PC和安卓区别:存储路径设置
可使用如下两种方法。
1️⃣ 将Zip保存到本地,解压本地Zip
推荐第3种方法。因为本种方法在PC成功,在安卓不能解压。
string downLoadPath=写入zip的路径.zip;
string unZipPath=要解压到的文件夹;
//下载结束写入本地
File.WriteAllBytes(downLoadPath, 下载的数据);
//解压本地zip
Skode_UnzipFile(downLoadPath, unZipPath, null);
public static bool Skode_UnzipFile(string _filePathName, string _outputPath, string _password = null)
{
try
{
return Skode_UnzipFile(File.OpenRead(_filePathName), _outputPath, _password);
}
catch (Exception _e)
{
Debug.LogError("[ZipUtility.UnzipFile]: " + _e.ToString());
return false;
}
}
public static bool Skode_UnzipFile(Stream _inputStream, string _outputPath, string _password = null)
{
// 创建文件目录
if (!Directory.Exists(_outputPath))
Directory.CreateDirectory(_outputPath);
// 解压Zip包
ZipEntry entry = null;
Encoding gbk = Encoding.GetEncoding("gbk");
ZipConstants.DefaultCodePage = gbk.CodePage;
using (ZipInputStream zipInputStream = new ZipInputStream(_inputStream))
{
if (!string.IsNullOrEmpty(_password))
zipInputStream.Password = _password;
while (null != (entry = zipInputStream.GetNextEntry()))
{
if (null == entry || string.IsNullOrEmpty(entry.Name))
continue;
string filePathName = Path.Combine(_outputPath, entry.Name);
// 创建文件目录
if (entry.IsDirectory)
{
Directory.CreateDirectory(filePathName);
continue;
}
// 写入文件
try
{
Skode_EventHandler.instance.Skode_StartUnZip();
using (FileStream fileStream = File.Create(filePathName))
{
byte[] bytes = new byte[1024];
while (true)
{
int count = zipInputStream.Read(bytes, 0, bytes.Length);
if (count > 0)
fileStream.Write(bytes, 0, count);
}
}
}
catch (System.Exception _e)
{
Debug.LogError("[ZipUtility.UnzipFile]: " + _e.ToString());
return false;
}
}
}
return true;
}2️⃣ 直接将下载的byte[]解压到指定路径
直接传入下载的数据,解压出来。即省去保存为zip这一步。
注意:要引入ICSharpCode.SharpZipLib.Zip dll文件。
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SkodeUnPack: MonoBehaviour
{
/// <summary>
/// 解压功能(下载后直接解压压缩文件到指定目录)
/// </summary>
/// <param name="unZipPath">解压目录</param>
/// <param name="ZipByte">网络下载的数据</param>
/// <param name="password">密码</param>
/// <returns>解压结果</returns>
public static bool SaveZip(string unZipPath, byte[] ZipByte, string password = null)
{
bool result = true;
FileStream fs = null;
ZipInputStream zipStream = null;
ZipEntry ent = null;
string fileName;
if (!Directory.Exists(unZipPath))
{
Directory.CreateDirectory(unZipPath);
}
try
{
//直接使用 将byte转换为Stream,省去先保存到本地在解压的过程
Stream stream = new MemoryStream(ZipByte);
zipStream = new ZipInputStream(stream);
if (!string.IsNullOrEmpty(password))
{
zipStream.Password = password;
}
while ((ent = zipStream.GetNextEntry()) != null)
{
if (!string.IsNullOrEmpty(ent.Name))
{
fileName = Path.Combine(unZipPath, ent.Name);
#region Android
fileName = fileName.Replace('\\', '/');
if (fileName.EndsWith("/"))
{
Directory.CreateDirectory(fileName);
continue;
}
#endregion
fs = File.Create(fileName);
int size = 2048;
byte[] data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
//fs.Write(data, 0, data.Length);
fs.Write(data, 0, size);//解决读取不完整情况
}
else
break;
}
}
}
}
catch (Exception e)
{
UnityEngine.Debug.Log(e.ToString());
result = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (zipStream != null)
{
zipStream.Close();
zipStream.Dispose();
}
if (ent != null)
{
ent = null;
}
GC.Collect();
GC.Collect(1);
}
return result;
}
}安卓版使用二——2方法。但需再引用几个dll文件,否则在安卓无法解压:
在Unity搜索 I18N ,将这几个文件复制到Unity:Asset/DLL文件夹即可。

大家还有什么问题,欢迎在下方留言!