将Zip文件解压到SD卡上非常缓慢。我该如何优化性能?

14

我的应用程序下载了大约350个文件的zip包。其中包含JPG和HTML文件。我编写的函数可以正常工作,但解压缩需要很长时间。起初我认为原因可能是写入SD卡很慢。但当我使用手机上的另一个应用程序解压缩相同的zip时,速度快得多。有什么方法可以优化这个问题吗?

以下是代码:

private void extract() {

    try {
        FileInputStream inStream = new FileInputStream(targetFilePath);
        ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(inStream));
        ZipEntry entry;
        ZipFile zip = new ZipFile(targetFilePath);

                    //i know the contents for the zip so i create the dirs i need in advance
        new File(targetFolder).mkdirs();
        new File(targetFolder + "META-INF").mkdir();
        new File(targetFolder + "content").mkdir();

        int extracted = 0;

        while((entry = zipStream.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                new File(targetFolder + entry.getName()).mkdirs();
            } else {
                FileOutputStream outStream = new FileOutputStream(targetFolder + entry.getName());
                for (int c = zipStream.read(); c != -1; c = zipStream.read()) {
                    outStream.write(c);
                }
                zipStream.closeEntry();
                outStream.close();

                extracted ++;
            }

            publishProgress(""+(int)extracted*100/zip.size());
        }

        zipStream.close();
        inStream.close();
        //
        new File(targetFilePath).delete();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

多亏了CommonsWare,我修改了我的代码:

                    int size;
                byte[] buffer = new byte[2048];

                FileOutputStream outStream = new FileOutputStream(targetFolder + entry.getName());
                BufferedOutputStream bufferOut = new BufferedOutputStream(outStream, buffer.length);

                while((size = zipStream.read(buffer, 0, buffer.length)) != -1) {
                    bufferOut.write(buffer, 0, size);
                }

                bufferOut.flush();
                bufferOut.close();

性能差异很大。 非常感谢。

2个回答

15

您正在逐字逐句地读写。建议一次读写更大的块。


1
谢谢!那真的起作用了。 - Philipp Redeker
@CommonsWare 链接失效了,您能修复一下吗? - Scorchio
请问您能否提供一个链接或代码,以便我可以一次读写更大的数据块? - Deepak Sharma
@DeepakSharma:如果你看了问题,你会看到:“感谢CommonsWare,我修改了我的代码”,并且提供了一个使用2K块的示例。 - CommonsWare
非常感谢CommonsWare提供的答案,也感谢notme分享这个答案。 :) - Jomia

0

只需使用此方法一次,相信我它是一个超级快速的过程... 它将在1秒钟内解压缩所有文件,而不会跳过任何文件。

    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;
}

希望这能对你有所帮助..祝编程愉快 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接