- Home /
Loading resources from a ZIP file from a server
Is it possible to load resources such as the following from a ZIP file (or other compression format) that has been retrieved from the web using WWW or perhaps the UniWeb extension?
PNG images
Sounds
Javascript code
This needs to be cross-platform compatible (Mac, PC, iPad, Android -- I don't care about Webplayer or Flash).
Also, Asset Bundles don't seem to be an option since you need Unity to create them, and I want to create them from a server program that won't have access to Unity (unless the file format is now open & documented).
I have found some older posts asking similar questions, but they are out of date and implied that a better way might be available soon.
Answer by MindJuice · Mar 01, 2012 at 05:42 AM
I found various bits and pieces regarding this on blogs and Unity Answers posts. The code below is what I pieced together.
With this, I was able to download a ZIP file, save it to the Documents folder on the iPhone and then unzip the two files that were in the test.zip file. Obviously this is not an example of a good way to setup your code, but it shows you how the pieces work to download and unzip the files.
using UnityEngine;
using System;
using System.Text;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
public class ZipTest : MonoBehaviour {
private WWW www;
private bool isUnzipped = false;
// Use this for initialization
void Start () {
string url = "http://www.yoursite.com/files/test.zip";
www = new WWW(url);
}
// Update is called once per frame
void Update () {
if (www.isDone && !isUnzipped)
{
Debug.Log("Load of test.zip complete");
byte[] data = www.bytes;
string docPath = Application.dataPath;
docPath = docPath.Substring(0, docPath.Length - 5);
docPath = docPath.Substring(0, docPath.LastIndexOf("/"));
docPath += "/Documents/test.zip";
Debug.Log("docPath=" + docPath);
System.IO.File.WriteAllBytes(docPath, data);
using (ZipInputStream s = new ZipInputStream(File.OpenRead(docPath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
Console.WriteLine(theEntry.Name);
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if ( directoryName.Length > 0 )
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
string filename = docPath.Substring(0, docPath.Length - 8);
filename += theEntry.Name;
Debug.Log("Unzipping: " + filename);
using (FileStream streamWriter = File.Create(filename))
{
int size = 2048;
byte[] fdata = new byte[2048];
while (true)
{
size = s.Read(fdata, 0, fdata.Length);
if (size > 0)
{
streamWriter.Write(fdata, 0, size);
}
else
{
break;
}
}
}
}
}
isUnzipped = true;
}
}
}
}
I just created an empty Unity project and added a Cube to the scene and attached ZipTest.cs (the file that contains the above code) to the Cube object.
Also, note that the path manipulation done here is for iOS only. You will need to create different file paths for other platforms.
Here is the website: SharpZipLib Site
And here is the download link for the source code to include in your project: SharpZipLib Source Code Download
I removed the source folders for BZip2, GZip, Lzw and Tar since I didn't need those. This will make the code a bit smaller. It may be possible to remove other files too such as Encryption, depending on your needs.
HI there I download the required file but issue is I am getting lots of error in it
please let me know if you have solution. my contact id is a.verma.2906@gmail.com actually it is not able to find the ICSharpCode file
most excellent comments here. I tried it and it didnt work.
Answer by OfficeThrashing · Sep 27, 2018 at 06:12 AM
I am getting these much errors . how to solve them?? @mindjuice