Java Unzip File unzip(File jar, File target)

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

Description

unzip

License

Apache License

Declaration

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

Method Source Code


//package com.java2s;
/*//ww  w . jav  a 2 s . c o  m
 * Copyright ? 2014 Stefan Niederhauser (nidin@gmx.ch)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    public static void unzip(File jar, File target) throws IOException {
        final ZipFile in = new ZipFile(jar);
        final Enumeration<? extends ZipEntry> entries = in.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                final File file = new File(target, entry.getName());
                file.getParentFile().mkdirs();
                copy(in.getInputStream(entry), new FileOutputStream(file), true);
            }
        }
        in.close();
    }

    private static void copy(InputStream in, OutputStream out, boolean closeOut) throws IOException {
        final byte[] buf = new byte[10000];
        int read;
        while ((read = in.read(buf)) > 0) {
            out.write(buf, 0, read);
        }
        in.close();
        if (closeOut) {
            out.close();
        }
    }
}

Related

  1. unzip(File input, File outputDir)
  2. unzip(File input, File outputDirectory)
  3. unzip(File inputFile, File outputDir)
  4. unzip(File inputFile, File unzipDestFolder)
  5. unzip(File intoFolder, ZipFile zipFile)
  6. unzip(File jarFile, File destDir)
  7. unzip(File sourceFile, File rootDir)
  8. unzip(File sourceZipfile, File directory)
  9. unzip(File src, File dest)