1、Unity下載Zip壓縮文件主要使用UnityWebRequest類。
可以參考以下方法:
webRequest = UnityWebRequest.Get(Path1); //壓縮文件路徑
webRequest.timeout = 60;
webRequest.downloadHandler = new DownloadHandlerBuffer();
long fileSize = GetLocalFileSize(Path2); //存貯路徑
webRequest.SetRequestHeader("Range", "bytes=" + fileSize + "-");
webRequest.SendWebRequest();
while (!webRequest.isDone)
{
float progress = Mathf.Clamp01(webRequest.downloadProgress);
progressBar.fillAmount = progress;
progressText.text = string.Format("{0}%", Mathf.RoundToInt(progress * 100f));
yield return null;
}
if (webRequest.isNetworkError || webRequest.isHttpError)
{
progressObj.SetActive(false);
}
else
{
byte[] downloadedData = webRequest.downloadHandler.data;
File.WriteAllBytes(Path2, downloadedData);
}
其中這里我還用個(gè)while循環(huán)寫了個(gè)下載進(jìn)度條。?
2、解壓Zip壓縮文件用到的System.IO.Compression下的ZipFile.OpenRead()方法。文章來源:http://www.zghlxwxcb.cn/news/detail-775385.html
具體可以參考以下代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-775385.html
/// <summary>
/// 解壓
/// </summary>
/// <param name="zipFilePath">壓縮文件路徑</param>
/// <param name="extractPath">解壓路徑</param>
public void ExtractZipFile(string zipFilePath, string extractPath)
{
using (ZipArchive archive = ZipFile.OpenRead(zipFilePath))
{
try
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string entryPath = Path.Combine(extractPath, entry.FullName);
if (entry.Name == "")
{
Directory.CreateDirectory(entryPath);
}
else
{
entry.ExtractToFile(entryPath, true);
}
}
}
catch(Exception e)
{
UnityEngine.Debug.Log(e.Message);
}
}
}
到了這里,關(guān)于Unity 下載Zip壓縮文件并且解壓縮的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!