Java Unzip File unZip(File zipf, String targetDir)

Here you can find the source of unZip(File zipf, String targetDir)

Description

un Zip

License

Open Source License

Declaration

@SuppressWarnings("rawtypes")
    public static void unZip(File zipf, String targetDir) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w w w  .j a  v a  2  s.  com
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    @SuppressWarnings("rawtypes")
    public static void unZip(File zipf, String targetDir) throws IOException {
        ZipFile zfile = new ZipFile(zipf);
        Enumeration zList = zfile.entries();
        ZipEntry ze = null;
        byte[] buf = new byte[1024];
        while (zList.hasMoreElements()) {
            ze = (ZipEntry) zList.nextElement();
            if (ze.isDirectory()) {
                File f = new File(String.format("%s/%s", targetDir, ze.getName()));
                f.mkdir();
                continue;
            }
            OutputStream os = new BufferedOutputStream(
                    new FileOutputStream(String.format("%s/%s", targetDir, ze.getName())));
            InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
            int readLen = 0;
            while ((readLen = is.read(buf, 0, 1024)) != -1) {
                os.write(buf, 0, readLen);
            }
            is.close();
            os.close();
        }
        zfile.close();
    }
}

Related

  1. unzip(File zip, File location)
  2. unzip(File zip, File targetDir)
  3. unzip(File zip, File toDir)
  4. unzip(File zip, File toDir)
  5. unzip(File zip, String folder, File into)
  6. unzip(File zipFile, File dest)
  7. unzip(File zipFile, File destDir)
  8. unzip(File zipFile, File destDir)
  9. unzip(File zipFile, File destDir)