Extrakting Zip to SD-Card is very slow. How can i optimize performance?

后端 未结 2 945
鱼传尺愫
鱼传尺愫 2020-12-29 08:38

my app downloads a zip with about 350 files. A mix of JPG and HTML files. The function i wrote to do it works just fine but the unzipping takes for ever. At first i thought

相关标签:
2条回答
  • 2020-12-29 09:06

    Just use this method once and believe me its a super fast process.. It will unzip all the files without skipping any file with in 1 second.

        public boolean rajDhaniSuperFastUnzip(String inputZipFile, String destinationDirectory)
            {
        try {
            int BUFFER = 2048;
            List<String> zipFiles = new ArrayList<String>();
            File sourceZipFile = new File(inputZipFile);
            File unzipDestinationDirectory = new File(destinationDirectory);
            unzipDestinationDirectory.mkdir();
            ZipFile zipFile;
            zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
            Enumeration<?> zipFileEntries = zipFile.entries();
            while (zipFileEntries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
                String currentEntry = entry.getName();
                File destFile = new File(unzipDestinationDirectory, currentEntry);
                if (currentEntry.endsWith(".zip")) {
                    zipFiles.add(destFile.getAbsolutePath());
                }
    
                File destinationParent = destFile.getParentFile();
    
                destinationParent.mkdirs();
    
                try {
                    if (!entry.isDirectory()) {
                        BufferedInputStream is =
                                new BufferedInputStream(zipFile.getInputStream(entry));
                        int currentByte;
                        byte data[] = new byte[BUFFER];
    
                        FileOutputStream fos = new FileOutputStream(destFile);
                        BufferedOutputStream dest =
                                new BufferedOutputStream(fos, BUFFER);
                        while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                            dest.write(data, 0, currentByte);
                        }
                        dest.flush();
                        dest.close();
                        is.close();
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            zipFile.close();
    
            for (Iterator<String> iter = zipFiles.iterator(); iter.hasNext();) {
                String zipName = (String)iter.next();
                doUnzip(
                    zipName,
                    destinationDirectory +
                        File.separatorChar +
                        zipName.substring(0,zipName.lastIndexOf(".zip"))
                );
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false ;
        }
        return true;
    }
    

    Hope this will help you.. Happy Coding :)

    0 讨论(0)
  • 2020-12-29 09:08

    You are reading and writing a byte at a time. Consider reading and writing a larger block at a time.

    0 讨论(0)
提交回复
热议问题