java 当压缩文件被解压到一个目录中时,只解压了1.5 GB中的100 MB,

hrirmatl  于 2023-03-06  发布在  Java
关注(0)|答案(2)|浏览(133)
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);
        }
    }
}

我试图解决与归档相关的问题,但改变缓冲区大小和重写类并没有带来理想的结果,同时,我确保归档本身没有损坏,问题的原因应该在其他地方找到,可能值得注意的是,其他参数或函数可能会影响与归档一起工作。

iibxawm4

iibxawm41#

其实是因为“Deflate”压缩方式的原因,文件有4GB多一点,而且因为这个原因,压缩方式是Deflate+Store,那是不可能正常处理的,据我了解,只处理压缩的部分,我一尝试处理未压缩的,马上就死机了,可能是因为这个类不是为未压缩的数据设计的。2请检查我的理论并给予反馈。

j13ufse2

j13ufse22#

这代码是移动到Zip4j库.这问题已经消失

相关问题