Java Unzip File unzip(File dest, String jar)

Here you can find the source of unzip(File dest, String jar)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File dest, String jar) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w w w .  jav a2 s . c o m
 *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

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 {
    public static void unzip(File dest, String jar) throws IOException {
        dest.mkdirs();
        ZipFile zf = new ZipFile(jar);
        try {
            Enumeration es = zf.entries();
            while (es.hasMoreElements()) {
                ZipEntry je = (ZipEntry) es.nextElement();
                String n = je.getName();
                File f = new File(dest, n);
                if (je.isDirectory()) {
                    f.mkdirs();
                } else {
                    if (f.exists()) {
                        f.delete();
                    } else {
                        f.getParentFile().mkdirs();
                    }
                    InputStream is = zf.getInputStream(je);
                    FileOutputStream os = new FileOutputStream(f);
                    try {
                        copyStream(is, os);
                    } finally {
                        os.close();
                    }
                }
                long time = je.getTime();
                if (time != -1)
                    f.setLastModified(time);
            }
        } finally {
            zf.close();
        }
    }

    public static void copyStream(InputStream is, OutputStream os) throws IOException {
        byte[] buffer = new byte[1 << 14];
        while (true) {
            int r = is.read(buffer);
            if (r > 0) {
                os.write(buffer, 0, r);
            } else if (r == -1)
                break;
        }
        os.flush();
    }
}

Related

  1. unzip(File archive)
  2. unzip(File archive, File output)
  3. unzip(File archiveFile, File destination)
  4. unzip(File archiveFile, File destination)
  5. unzip(File archiveFile, File targetDir, boolean skipRoot)
  6. unzip(File destDir, InputStream is)
  7. unzip(File epubfile, File destination)
  8. unzip(File file, File dest)
  9. unzip(File file, File destDir)