How to speed up unzipping time in Java / Android?

后端 未结 6 1561
名媛妹妹
名媛妹妹 2020-12-02 08:42

Unzipping files on android seems to be dreadfully slow. At first I thought this was just the emulator but it appears to be the same on the phone. I\'ve tried different compr

6条回答
  •  抹茶落季
    2020-12-02 09:34

    Just call this method and it will give you much better performance..

        public boolean unzip(Context context) {
        try {
            FileInputStream fin = new FileInputStream(_zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            BufferedInputStream in = new BufferedInputStream(zin);
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                Log.v("Decompress", "Unzipping " + ze.getName());
    
                if (ze.isDirectory()) {
                    _dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(_location
                            + ze.getName());
                        BufferedOutputStream out = new BufferedOutputStream(fout);
                        byte b[] = new byte[1024];
                    for (int c = in.read(b,0,1024); c != -1; c = in.read()) {
                        out.write(b,0,c);
                    }
                    zin.closeEntry();
                    fout.close();
                }
            }
            zin.close();
            return true;
        } catch (Exception e) {
            Log.e("Decompress", "unzip", e);
            return false;
        }
    }
    
        private void _dirChecker(String dir) {
        File f = new File(_location + dir);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
    }
    

提交回复
热议问题