Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

How to UnZip archive in Java

Posted by Piotr Gabryanczyk on May 7, 2008

Why am I writing about it?

Because I could not find complete solution for unzipping archive in java.

Dependencies

You will need commons-io.jar from apache commons.

Random thoughts…

It is too small to release it as an open source library, and Apache Commons does not contain such a function, when I am writing it this post. Please feel free to copy/use/modify this code.

public class ZipUtils {
    Logger log;

    public ZipUtils(Logger log) {
        this.log = log;        
    }

    public void unzipArchive(File archive, File outputDir) {
        try {
            ZipFile zipfile = new ZipFile(archive);
            for (Enumeration e = zipfile.entries(); e.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                unzipEntry(zipfile, entry, outputDir);
            }
        } catch (Exception e) {
            log.error("Error while extracting file " + archive, e);
        }
    }

    private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {


        if (entry.isDirectory()) {
            createDir(new File(outputDir, entry.getName()));
            return;
        }
        
        File outputFile = new File(outputDir, entry.getName());
        if (!outputFile.getParentFile().exists()){
            createDir(outputFile.getParentFile());
        }

        log.debug("Extracting: " + entry);
        BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

        try {
            IOUtils.copy(inputStream, outputStream);
        } finally {
            outputStream.close();
            inputStream.close();
        }
    }

    private void createDir(File dir) {
        log.debug("Creating dir "+dir.getName());
        if(!dir.mkdirs()) throw new RuntimeException("Can not create dir "+dir);
    }
}
Advertisements

7 Responses to “How to UnZip archive in Java”

  1. Also consider https://truezip.dev.java.net/

  2. Eugene said

    Or you can use this method instead of the commons class:

    public static void copy(InputStream in, OutputStream out)
    throws IOException
    {
    if (in == null)
    throw new NullPointerException(“InputStream is null!”);
    if (out == null)
    throw new NullPointerException(“OutputStream is null”);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0)
    {
    out.write(buf, 0, len);
    }
    in.close();
    out.close();
    }

  3. Xiara said

    Thanks, I needed it.

  4. yossi said

    Thanks , I needed that

  5. Gorgee said

    Thanks, really helpful.
    Just needs a little tweak: zipfile.close() at the end of unzipArchive method, otherwise I was unable to delete the original .zip file after extraction.

  6. Igor Azarny said

    Thanks, it save me time

  7. flipouk said

    Many thanks!

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: