Here you can find the source of unzip(String sourceFile, String destDir)
public static void unzip(String sourceFile, String destDir) throws IOException
//package com.java2s; /*/*w w w. j a va2s . co m*/ * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { private static final int BUFFER_SIZE = 4096; public static void unzip(String sourceFile, String destDir) throws IOException { final FileInputStream fis = new FileInputStream(sourceFile); final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); final File destDirFile = new File(destDir); final byte[] data = new byte[BUFFER_SIZE]; ZipEntry entry; Set<String> visitedDirs = new HashSet<>(); createDir(destDir); while ((entry = zis.getNextEntry()) != null) { createDir(destDirFile, entry, visitedDirs); if (entry.isDirectory()) { continue; } int count; // write the file to the disk File dst = new File(destDir, entry.getName()); FileOutputStream fos = new FileOutputStream(dst); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE); while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } // close the output streams dest.flush(); dest.close(); } zis.close(); } private static void createDir(File destDirectory, ZipEntry entry, Set<String> visitedDirs) { String name = entry.getName(); int index = name.lastIndexOf(File.separator); if (index != -1) { String dirSequence = name.substring(0, index); if (visitedDirs.add(dirSequence)) { File newDirs = new File(destDirectory, dirSequence); newDirs.mkdirs(); } } } private static void createDir(String destDirectory) { File newDirs = new File(destDirectory + File.separator); newDirs.mkdirs(); } }