I am using unity 2019.2.14f1 and visual studio 2019.I also tried with ICSharpCode.SharpZipLib but can't find any solution.I am trying with the code from the code which is available in the following url: Unzip a memorystream (Contains the zip file) and get the files Thanks in advance.
Asked
Active
Viewed 4,332 times
3 Answers
1
You can download files in Unity using UnityWebRequest.Get
. Note that using it like in the example shown on that page, you're going to want to make sure you know how to use Unity's Coroutines.
Also, instead of downloadHandler.text
you're going to want to use downloadHandler.data
to get the downloaded data as bytes you can put into a stream.
Once you've got those you can use the appropriate libraries or standard library calls to unzip your files.

TJ Michael
- 155
- 9
-
can you please explain me after completion of downloading where the file will be saved? – Silpi Mukherjee Dec 27 '19 at 06:34
-
It's saved to memory, not storage. It exists only in the byte array object returned by `downloadHandler.data`. You can use it with SharpZipLib by [putting it in a `MemoryStream`](https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream.-ctor?view=netframework-4.8#System_IO_MemoryStream__ctor_System_Byte___) and passing that to [`ZipFile`'s constructor](https://icsharpcode.github.io/SharpZipLib/help/api/ICSharpCode.SharpZipLib.Zip.ZipFile.html#ICSharpCode_SharpZipLib_Zip_ZipFile__ctor_System_IO_Stream_) – TJ Michael Dec 27 '19 at 08:27
-
1Thank you! It helped me implement my function. – unobatbayar Oct 15 '21 at 07:32
0
To download a file from a URL you can use system.net
WebClient client = new WebClient();
client.DownloadFile("http://whateverlinkyouhave.zip", @"C:\whateverlinkyouhave.zip")

Rohit5k2
- 17,948
- 8
- 45
- 57

Paulo Alves
- 164
- 8
-
Can I pass the path of the folder which I created in my android device internal storage as an argument in that client.DownloadFile method.Actually,previously I tried with how to fetch the android device internal storage data from unity but I can't find any solution then I am trying with the following: at first I am sending one zip url from my android project to unity and then trying to download the zip file in unity and unzip that file in unity and then render the AR model which is in the unzip file in unity. Is the above solution correct?please reply.thanks. – Silpi Mukherjee Dec 27 '19 at 05:46
0
Everyone's answer is correct, especially using UnityWebRequest
. One of things you must do is to turn the byte[]
data to zip
file. And then use some Unzip library.
Example implementation
using System;
using UnityEngine.Networking;
private IEnumerator DownloadZipFile(string url)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
string savePath = string savePath = string.Format("{0}/{1}.zip", Application.persistentDataPath, "zip_file_name");
System.IO.File.WriteAllBytes(savePath, www.downloadHandler.data);
}
}
}
Hope it helps.

unobatbayar
- 1,223
- 1
- 10
- 24