首先导入ICSharpCode.SharpZipLib这个库,然后代码如下,写完下面这个方法记得在按钮上拖拽复制.
using System;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using UnityEngine;
public class SharpZip : MonoBehaviour
{
public static SharpZip _instance;
private string targetPath = "";
private string zipPath = "";
public GameObject backImage;
public GameObject SharpText;
public GameObject SharpFinishText;
public GameObject SharpFinishBut;
public GameObject SharpButton;
public int Gameid;
public GameObject SharpPanel;
public string url;
private int HaveGameZipId=0;
public GameObject SharpWaitingPanel;
void Start()
{
url = Environment.CurrentDirectory;
}
void Awake()
{
_instance = this;
}
public void GetHaveGameZipId(int id)
{
HaveGameZipId = id;
Debug.Log("HaveGameZipId--" + HaveGameZipId);
}
public void SharpOnClick(int id)
{
if (id == 0&&HaveGameZipId!=0)
{
id = HaveGameZipId;
HaveGameZipId =0;
}
else
{
id = TestHttpManager._instance.SharpId;
}
Debug.Log(id);
Gameid = id;
SharpButton.SetActive(false);
SharpText.SetActive(false);
SharpWaitingPanel.SetActive(true);
zipPath = url + @"\Game\Zip\" + id + @".zip";
targetPath = url + @"\Game\" + id;
StartCoroutine(UnZip(zipPath, targetPath));//创建一个文件夹存放的解压
}
public void SharpFinishClick()
{
CleanSharpZipPanel();
Debug.Log("SharpFinishClick:" + Gameid);
GetGameId._instance.OnClick(Gameid);
}
public void CleanSharpZipPanel()
{
SharpText.SetActive(true);
SharpFinishText.SetActive(false);
SharpButton.SetActive(true);
backImage.SetActive(false);
SharpFinishBut.SetActive(false);
SharpPanel.SetActive(false);
}
public IEnumerator UnZip(string zipFilePath, string unZipDir)
{
if (zipFilePath == string.Empty)
{
throw new Exception("压缩文件不能为空!");
}
if (!File.Exists(zipFilePath))
{
throw new System.IO.FileNotFoundException("压缩文件不存在!");
}
//文件夹里建一个带有时间戳的文件夹
//unZipDir += "weather" + DateTime.Now.ToString("yyyyMMddHHmmsss");
//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
if (unZipDir == string.Empty)
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("\\"))
unZipDir += "\\";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
{
Directory.CreateDirectory(unZipDir + directoryName);
//将解压后的文件放到带根据id创建的文件夹里
}
if (!directoryName.EndsWith("\\"))
directoryName += "\\";
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
var lt = s.Position;
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
SharpWaitingPanel.SetActive(false);
SharpText.SetActive(false);
SharpFinishText.SetActive(true);
SharpFinishBut.SetActive(true);
//backImage.SetActive(false);
Debug.Log("解压完成:");
break;
}
}
}
}
}
}
yield return new WaitForEndOfFrame();
}
}