.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

How to decompress zip file in Java


Introduce how to decompress a zip file in Java:

public void unzip(File zipFile, String outputFolder)
    {
        byte[] buffer = new byte[1024];
        ZipEntry ze = null;
        try
        {
            File folder = new File(outputFolder);
            if (!folder.exists())
            {
                folder.mkdir();
            }
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
            while ((ze = zis.getNextEntry()) != null)
            {
                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator + fileName);
                System.out.println("unziping : " + newFile.getAbsoluteFile());
                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(buffer)) > 0)
                {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }
            zis.close();
        }
        catch (Exception ex)
        {
            if (zipFile != null)
            {
                System.out.println("Do not decompress file " + zipFile.getName() + " (size: 0kb)!");
            }
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: