Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can I convert a byte[] to a zip archive?

Discussion in 'General Discussion' started by Proilios, Jan 19, 2022.

  1. Proilios

    Proilios

    Joined:
    May 17, 2018
    Posts:
    9
    Hey guys, I've been trying to set up a flow where I download a zip file from a web URL using UnityWebRequest, store that zip file somewhere in my system, and then access its contents.
    I've been able to download the file in the form of a byte[] i.e. downloadHandler.data, but I haven't been able to find out how to interpret that data as a zip archive and store it.

    Can anyone please help me out here?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    can save it as a file with .zip extension, File.WriteAllBytes(..)

    saving files in mobiles is sometimes bit complicated though, is that android or ios?
    i guess you'd like to save it into Downloads/ or so?
     
  3. Proilios

    Proilios

    Joined:
    May 17, 2018
    Posts:
    9
    Hey thanks for the suggestion!
    I did the following:

    Code (CSharp):
    1. File.WriteAllBytes(Application.persistentDataPath + "/MyZipFile.zip", results);
    This did create a zip file as you said, but the file was empty, whereas I can see on the web URL that it contains some content. Is there something additional I need to account for?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
  5. Proilios

    Proilios

    Joined:
    May 17, 2018
    Posts:
    9
    I am just executing this in the editor for now. My code threw no errors. However, I located the zip file where I stored it and tried to extract it. When I did that, I got the alert "The compressed zip folder is empty. Before you can extract files, you must copy files to this compressed (zipped) folder".
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    aa ok, what size the bytes data shows if print length?
     
  7. Proilios

    Proilios

    Joined:
    May 17, 2018
    Posts:
    9
    63658
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
  9. Proilios

    Proilios

    Joined:
    May 17, 2018
    Posts:
    9
    Code (CSharp):
    1. private IEnumerator Co_DownloadZipFile()
    2.     {
    3.         UnityWebRequest www = new UnityWebRequest(zipFileUrl);
    4.         www.downloadHandler = new DownloadHandlerBuffer();
    5.  
    6.         yield return www.SendWebRequest();
    7.  
    8.         if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
    9.         {
    10.             Debug.Log(www.error);
    11.         }
    12.         else
    13.         {
    14.             Debug.Log(www.downloadHandler.text.Contains("TestFile"));
    15.             byte[] results = www.downloadHandler.data;
    16.             Debug.Log(results.Length);
    17.  
    18.  
    19.             File.WriteAllBytes(Application.persistentDataPath + "/MyZipFile.zip", results);
    20.         }
    21.     }
    P.S. Line 14 returns true. "TestFile" is the name of one of the files in my zip file.