Skip to content

Instantly share code, notes, and snippets.

@mrmwiebe
Forked from r2d2rigo/ExtractZipFile.cs
Last active November 13, 2016 17:28
Show Gist options
  • Save mrmwiebe/5ca642849c8d07f64d7d0a12f0794f6e to your computer and use it in GitHub Desktop.
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
//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