[英]File compression in Unity3D
我正在嘗試從我擁有的Unity3D項目中壓縮文件。 我正在使用以下代碼壓縮文件:
private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {
string[] files = Directory.GetFiles(path);
foreach (string filename in files) {
FileInfo fi = new FileInfo(filename);
string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
// Zip the file in buffered chunks
// the "using" will close the stream even if an exception occurs
byte[ ] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(filename)) {
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
string[ ] folders = Directory.GetDirectories(path);
foreach (string folder in folders) {
CompressFolder(folder, zipStream, folderOffset);
}
}
該功能實際上將帶有文件夾的目錄作為輸入,並逐個壓縮它們。 我的問題是,進行壓縮時,主要項目處於凍結狀態。 我該如何克服這個問題?
最簡單的方法是使用忍者線程,多線程協程: https ://www.assetstore.unity3d.com/en/#! / content/ 15717
using UnityEngine;
using System.Collections;
using CielaSpike; //include ninja threads
public class MyAsyncCompression : MonoBehaviour {
public void ZipIt(){
//ready data for commpressing - you will not be able to use any Unity functions while inside the asynchronous coroutine
this.StartCoroutineAsync(MyBackgroundCoroutine()); // "this" means MonoBehaviour
}
IEnumerator MyBackgroundCoroutine(){
//Call CompressFolder() here
yield return null;
}
private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {
/*...
*...
...*/
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.