Here you can find the source of unzip(File archiveFile, File destination)
Parameter | Description |
---|---|
archiveFile | archive file to be unzipped |
destination | location to put the unzipped file |
Parameter | Description |
---|---|
IOException | throws when fail to create the directory structure when unzipping a file. |
public static void unzip(File archiveFile, File destination) throws IOException
//package com.java2s; /*/*from w ww. j av a2s . com*/ * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * 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.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { private static final int BUFFER = 2048; /** * Unzip a given archive to a given destination. * * @param archiveFile archive file to be unzipped * @param destination location to put the unzipped file * @throws IOException throws when fail to create the directory structure when unzipping a file. */ public static void unzip(File archiveFile, File destination) throws IOException { try (FileInputStream fis = new FileInputStream(archiveFile); ZipInputStream zis = new ZipInputStream( new BufferedInputStream(fis))) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { int count; byte data[] = new byte[BUFFER]; File file = new File(destination, entry.getName()); if (entry.getName().endsWith("/")) { if (!file.exists() && !file.mkdirs()) { throw new IOException( "Failed to create directories at " + file.getAbsolutePath()); } continue; } if (file.getParentFile() != null && !file.getParentFile().exists()) { if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { throw new IOException( "Failed to create directories at " + file.getAbsolutePath()); } } try (FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream dest = new BufferedOutputStream( fos, BUFFER)) { while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } } } } } }