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. Dismiss Notice

Crash while writing zip file on Android

Discussion in 'Editor & General Support' started by EricNumeri4D, Mar 14, 2016.

  1. EricNumeri4D

    EricNumeri4D

    Joined:
    Sep 24, 2013
    Posts:
    4
    Hi there,

    I'm currently stuck with one big problem. I'm downloading a zip file from the server and while writing the zip file in persistent data path, my app crash with no log, no error, no exception. I searched everywhere but didn't find anything.

    This is the code writing zip file :

    Code (CSharp):
    1.             Debug.Log("Start downloading ALL ZIP");
    2.             downloadAllZip = new WWW(NetworkManager.Instance.ServerAddress + "/ws/file/index/");
    3.             LoadingScreen.Instance.SetText ("Loading resources from internet, This may take several minutes... ");
    4.            
    5.             while (!downloadAllZip.isDone) {
    6. //                Debug.Log ("Progress " + downloadAllZip.progress);
    7.                 yield return new WaitForEndOfFrame();
    8.             }
    9.             Debug.Log ("Zip downloaded");
    10.             try {  
    11.                 if(!string.IsNullOrEmpty(downloadAllZip.error)) {
    12.                     Debug.LogError("Error in download all zip : " + downloadAllZip.error);
    13.                 }
    14.                 else
    15.                 {
    16.                     Debug.Log("End of download ALL ZIP");
    17.                     LoadingScreen.Instance.SetText ("Saving resources ... ");
    18.                     ResourcesPath =  ResourcesPath.Replace("\\","/");
    19.        
    20.                     Debug.Log ("Writing zip at : "+ResourcesPath + " - size : " + (downloadAllZip.size / 1000000f).ToString("F2") + "Mb");
    21.                     DataManager.WriteFile(ResourcesPath, downloadAllZip.bytes);
    22.     //                System.IO.File.WriteAllBytes(ResourcesPath,  downloadAllZip.bytes);
    23.                     Debug.Log("Zip saved");
    The last display is :
    "Writing zip at : /storage/sdcard0/Android/data/com.allucyne.colmarafdts/files/Data/Resources/resources.zip - size : 205.26Mb"
    And after it crashes.

    Note that I have Unity 4.6.8f1, and the DataManager is used in other projects and is working. There is error management in DataManager, and no error is fired.

    Just to show you the WriteFile method :

    Code (CSharp):
    1.         /// <summary>
    2.         /// Writes a file with the datas
    3.         /// </summary>
    4.         /// <param name="_filePath">file path.</param>
    5.         /// <param name="_data">data to write</param>
    6.         public static void WriteFile(string _filePath, byte[] _data)
    7.         {
    8.             try
    9.             {
    10.                 _filePath = ConvertPath (_filePath);
    11.                 if(_data != null)
    12.                 {
    13.     //                Debug.Log("Write file : " + _filePath + " - size : " + (_data.LongLength / 1000f).ToString("F3") + "Kb");
    14.                     try
    15.                     {
    16.     #if UNITY_WP8 || NETFX_CORE
    17.                         Debug.Log("Write file with unityengine windows method");
    18.                         UnityEngine.Windows.File.WriteAllBytes (_filePath, _data);
    19.     #else
    20.                         Debug.Log("Write file with mscorlib method");
    21.                         File.WriteAllBytes(_filePath, _data);
    22.     #endif
    23.                         Debug.Log("Write successful");
    24.                     }
    25.                     catch(Exception e)
    26.                     {
    27.                         Debug.LogError("Error writing file : " + _filePath + ", error : " + e.Message);
    28.                     }
    29.                 }
    30.                 else
    31.                 {
    32.                     Debug.LogError("Error, data is null");
    33.                 }
    34.             }
    35.             catch(Exception ex)
    36.             {
    37.                 Debug.LogError("[DataManager] Error while writing file " + _filePath + " : " + ex.Message);
    38.             }
    39.         }
    I'm stuck since a lot of weeks with this...I don't know if the file is too big or anything else. I'm using Ionic.Zip for the ZipFile but here i'm just writing bytes, so Zip has nothing to do with the problem I think.

    Thanks a lot.
     
  2. ThinhHB

    ThinhHB

    Joined:
    Oct 24, 2014
    Posts:
    9
    I don't know if you already solve this problem yet. I faced the same problem, when using File.WriteAllBytes(), FileStream ... on Android devices, it cause the crash issue. It cost me a day to look around for solution, but finally, I found out that, it all because the Stripping Level in PlayerSetting for Android, set it to DISABLE and everything will be ok. I think the Stripping setting may cause some Stream APIs (File, FileStream) broken, I'll do some more tests and report to Unity later.
    Hope it help.