How to speed up unzipping time in Java / Android?

后端 未结 6 1571
名媛妹妹
名媛妹妹 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:14

    The URL that helped me learn how to zip and unzip can be found here.

    I used that URL in conjuction with user3203118's answer above for unzipping. This is for future references for people who run in to this issue and need help solving it.

    Below is the ZipManager code I am using:

    public class ZipManager {
    
        private static final int BUFFER = 80000;
    
        public void zip(String[] _files, String zipFileName) {
            try {
                BufferedInputStream origin = null;
                FileOutputStream dest = new FileOutputStream(zipFileName);
                ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
                byte data[] = new byte[BUFFER];
    
                for (int i = 0; i < _files.length; i++) {
                    Log.v("Compress", "Adding: " + _files[i]);
                    FileInputStream fi = new FileInputStream(_files[i]);
                    origin = new BufferedInputStream(fi, BUFFER);
    
                    ZipEntry entry = new ZipEntry(_files[i].substring(_files[i]
                        .lastIndexOf("/") + 1));
                    out.putNextEntry(entry);
                    int count;
    
                    while ((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                    }
                    origin.close();
                }
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void unzip(String _zipFile, String _targetLocation) {
    
    
            // create target location folder if not exist
            dirChecker(_targetLocation);
    
            try {
                FileInputStream fin = new FileInputStream(_zipFile);
                ZipInputStream zin = new ZipInputStream(fin);
                ZipEntry ze = null;
                while ((ze = zin.getNextEntry()) != null) {
    
                    // create dir if required while unzipping
                    if (ze.isDirectory()) {
                        dirChecker(ze.getName());
                    } else {
                        FileOutputStream fout = new FileOutputStream(
                        _targetLocation + "/" + ze.getName());
                        BufferedOutputStream bufout = new BufferedOutputStream(fout);
                        byte[] buffer = new byte[1024];
                        int read = 0;
                        while ((read = zin.read(buffer)) != -1) {
                            bufout.write(buffer, 0, read);
                        }
    
                        zin.closeEntry();
                        bufout.close();
                        fout.close();
                    }
                }
                zin.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    
        private void dirChecker(String dir) {
            File f = new File(dir);
            if (!f.isDirectory()) {
                f.mkdirs();
            }
        }
    }
    

提交回复
热议问题