将 Zip 提取到 SD 卡的速度非常慢。我怎样才能优化性能?

发布于 2024-09-28 02:46:19 字数 2173 浏览 5 评论 0原文

我的应用程序下载包含大约 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();

性能差异很大。 多谢。

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 the reason might be that writing to the sd-card is slow. but when i unzip the same zip with an other app on my phone it works much faster. is there anything that i could do to optimize it?

here is the code:

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

thanks to CommonsWare i modified my code like this:

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

big performance difference.
Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2024-10-05 02:46:19

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

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

巷雨优美回忆 2024-10-05 02:46:19

只需使用此方法一次,相信我,这是一个超级快的过程。它将在 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;
}

希望这会对您有所帮助..快乐编码:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文