Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 933
Philipp Redeker Avatar asked Oct 20 '10 08:10

Philipp Redeker


People also ask

Why does extracting ZIP files take so long?

A reason of the extremely slow unzipping on Windows can be Defender that runs in the background and scans each file. This usually happens when you try to unzip a file that was downloaded from an online storage (e.g. from Google Drive) or you received it as an email attachment.

How long does it take to unzip a 1GB file?

It takes less than 1 minute to unzip 1GB so you should not wait too long.

Why is WinZip so slow?

The Windows Attachment Manager service can slow the WinZip extraction process significantly. When you download files from a web page or receive file attachments in your email, Windows creates an alternate NTFS data stream for the file in most cases. This data stream contains Internet Zone information.


1 Answers

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

like image 128
CommonsWare Avatar answered Sep 18 '22 12:09

CommonsWare