I am trying to compress files from a Unity3D project I have. I am using the following code to compress my files:
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);
}
}
This function actually takes as an input the a dir with folders and zip them one by one. My issue here is that when the compression is taking place the main project is freezing. How can I overcome this problem?
The simplest way is to use ninja threads, multithread coroutines: 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) {
/*...
*...
...*/
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.