Java Unzip File unzip(File src, File target)

Here you can find the source of unzip(File src, File target)

Description

unzip a file to a folder

License

Apache License

Parameter

Parameter Description
src a parameter
target a parameter

Declaration

public static void unzip(File src, File target) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w w w .  jav a  2  s. co  m*/
 * z2env.org - (c) ZFabrik Software KG
 * 
 * Licensed under Apache 2.
 * 
 * www.z2-environment.net
 */

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    private static final int UNZIP_BUFFER = 64 * 1024;

    /**
     * unzip a file to a folder
     * 
     * @param src
     * @param target
     */
    public static void unzip(File src, File target) throws IOException {
        InputStream in = new FileInputStream(src);
        ZipInputStream zi = new ZipInputStream(in);
        try {
            String fn;
            ZipEntry ze;
            File f;
            OutputStream out;
            int l;
            byte[] buffer = new byte[UNZIP_BUFFER];
            while ((ze = zi.getNextEntry()) != null) {
                fn = ze.getName();
                if (fn.endsWith("/")) {
                    // its a folder
                    f = new File(target, fn.substring(0, fn.length() - 1));
                    f.mkdirs();
                } else {
                    // it's a regular file
                    f = new File(target, fn);
                    f.getParentFile().mkdirs();
                    out = new FileOutputStream(f);
                    try {
                        while ((l = zi.read(buffer)) >= 0) {
                            out.write(buffer, 0, l);
                        }
                    } finally {
                        out.close();
                    }
                }
            }
        } finally {
            zi.close();
        }
    }
}

Related

  1. unzip(File jar, File target)
  2. unzip(File jarFile, File destDir)
  3. unzip(File sourceFile, File rootDir)
  4. unzip(File sourceZipfile, File directory)
  5. unzip(File src, File dest)
  6. unzip(File srcZipFile, File tgtDir)
  7. unzip(File tarFile, File destinationDir)
  8. unzip(File target)
  9. unzip(File targetZip, File dirToExtract)