0

I unzip files in my Android Studio project like this:

 InputStream is = ...
 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
 try {
     ZipEntry ze;
     while ((ze = zis.getNextEntry()) != null) {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         byte[] buffer = new byte[1024];
         int count;
         while ((count = zis.read(buffer)) != -1) {
             baos.write(buffer, 0, count);
         }
         String filename = ze.getName();
         byte[] bytes = baos.toByteArray();
         // do something with 'filename' and 'bytes'...
     }
 } finally {
     zis.close();
 }

The original zip file is approx. 30 MB big and contains just *.txt files. Two of those text files are > 100 MB, so that I get this error message:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapp, PID: 15821 java.lang.OutOfMemoryError: Failed to allocate a 167083628 byte allocation with 33542384 free bytes and 136MB until OOM at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201) at com.example.myapp.managers.UnzipManager.decompressZip(UnzipManager.java:104) at com.example.myapp.managers.UnzipManager.(UnzipManager.java:43) at com.example.myapp.MyActivity.onCreate(MyActivity.java:103) at android.app.Activity.performCreate(Activity.java:6272) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) at android.app.ActivityThread.access$900(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5551) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

How can I decompress those files propery?

Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103

0 Answers0