Search code examples
javaandroidzipunzip

When the archive is unzipped into a directory, only 100 MB of 1.5 GB is unzipped. Then - crash


private class UnZipTask extends AsyncTask <String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            String filePath = params[0];
            String destinationPath = params[1];
            try {
                File outdir = new File(destinationPath);
                if (!outdir.exists()) {
                    outdir.mkdirs();
                }
                File file = new File(filePath);
                if (!file.exists()) {
                    System.out.println("DEBUGLOG_0");
                    return null;
                }
                ZipInputStream zin = new ZipInputStream(new FileInputStream(file));
                ZipEntry entry;
                String name, dir;
                while ((entry = zin.getNextEntry()) != null) {
                    name = entry.getName();
                    if (entry.isDirectory()) {
                        mkdirs(outdir, name);
                        continue;
                    }
                    dir = dirpart(name);
                    if (dir != null) {
                        mkdirs(outdir, dir);
                    }
                    byte[] buffer = new byte[4096];
                    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outdir, name)));

                    int publishprogress = 0;
                    long length = entry.getSize();
                    int count;
                    while ((count = zin.read(buffer)) != -1) {
                        out.write(buffer, 0, count);
                        publishprogress += count;
                        publishProgress((int) (publishprogress * 100 / length));
                        System.out.println("DEBUGLOG_19 " + count + "-------publishprogress " + (int) (publishprogress * 100 / length) +"         "+ entry.getName());
                    }
                    System.out.println("DEBUGLOG_05");
                    out.close();
                }
                System.out.println("DEBUGLOG_06");
                zin.close();
            } catch (IOException e) {
                e.printStackTrace();
                writeLog(e);
            }
            return null;
        }


        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            loading.setText("Unzipping...                  "+progress[0] + "%");
            progressloading.setProgress(progress[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            loading.setVisibility(View.INVISIBLE);
            progressloading.setVisibility(View.INVISIBLE);
            com.luxury.launcher.other.Utils.delete(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/game.zip"));

            StartGame();
        }

        @Override

        protected void onPreExecute() {
            super.onPreExecute();
            loading.setVisibility(View.VISIBLE);
            progressloading.setVisibility(View.VISIBLE);
            progressloading.setIndeterminate(false);
            progressloading.setMax(100);
        }


        private void mkdirs(File outdir, String path) {
            File file = new File(outdir, path);
            if (!file.exists()) {
                file.mkdirs();
            }
        }

        private String dirpart(String name) {
            int index = name.lastIndexOf(File.separator);
            return index == -1 ? null : name.substring(0, index);
        }
    }
}

I tried to solve the problem related to the archive, but changing the buffer size and rewriting the class did not bring the desired result. At the same time, I made sure that the archive itself is not damaged, and the cause of the problem should be found elsewhere. It may be worth paying attention to other parameters or functions that may affect the work with the archive.


Solution

  • the code was moved to the Zip4j library. the problem has disappeared