-
-
Save mrmwiebe/5ca642849c8d07f64d7d0a12f0794f6e to your computer and use it in GitHub Desktop.
Using SharpZipLib to extract zip files from Unity in a coroutine-friendly way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Standalone version of r2d2rigo's coroutine unzip | |
//Leaves room for other tools as well | |
using UnityEngine; | |
using System.Collections; | |
using System.IO; | |
using ICSharpCode.SharpZipLib.Zip; | |
public class ZipTools { | |
public static IEnumerator ExtractZipFile(byte[] zipFileData, string targetDirectory, int bufferSize = 256 * 1024) | |
{ | |
Directory.CreateDirectory(targetDirectory); | |
using (MemoryStream fileStream = new MemoryStream()) | |
{ | |
fileStream.Write(zipFileData, 0, zipFileData.Length); | |
fileStream.Flush(); | |
fileStream.Seek(0, SeekOrigin.Begin); | |
ZipFile zipFile = new ZipFile(fileStream); | |
foreach (ZipEntry entry in zipFile) | |
{ | |
string targetFile = Path.Combine(targetDirectory, entry.Name); | |
using (FileStream outputFile = File.Create(targetFile)) | |
{ | |
if (entry.Size > 0) | |
{ | |
Stream zippedStream = zipFile.GetInputStream(entry); | |
byte[] dataBuffer = new byte[bufferSize]; | |
int readBytes; | |
while ((readBytes = zippedStream.Read(dataBuffer, 0, bufferSize)) > 0) | |
{ | |
outputFile.Write(dataBuffer, 0, readBytes); | |
outputFile.Flush(); | |
yield return null; | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment