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();

性能差异大。非常感谢。

4

2 回答 2

15

您一次读取和写入一个字节。考虑一次读取和写入一个更大的块。

于 2010-10-20T11:44:58.403 回答
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;
}

希望这会帮助你..快乐编码:)

于 2014-11-18T12:06:27.153 回答