Here you can find the source of unzip(File zip)
Parameter | Description |
---|---|
zip | a parameter |
public static void unzip(File zip)
//package com.java2s; /*/*ww w .j ava 2s .co m*/ * Copyright (c) 2009 The Regents of the University of California. * All rights reserved. * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the above * copyright notice and the following two paragraphs appear in all copies * of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, * ENHANCEMENTS, OR MODIFICATIONS. * */ 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 { /** * unzip a file * * @param zip */ public static void unzip(File zip) { try { String destinationDirName = zip.getName().substring(0, zip.getName().length() - ".zip".length()); File destinationDir = new File(zip.getParentFile(), destinationDirName); unzipHelper(zip, destinationDir); } catch (IOException e) { e.printStackTrace(); } } /** * unzip a file to a dir * * @param zip * @param destinationDir */ public static void unzip(File zip, File destinationDir) { try { unzipHelper(zip, destinationDir); } catch (IOException e) { e.printStackTrace(); } } /** * unzip a file to a dir * * @param zip * @param destinationDir * @throws IOException */ private static void unzipHelper(File zip, File destinationDir) throws IOException { System.out.println("Unzipping " + zip.getAbsolutePath()); destinationDir.mkdirs(); ZipInputStream zis = new ZipInputStream(new BufferedInputStream( new FileInputStream(zip))); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { File outputFile = new File(destinationDir, entry.getName()); if (entry.isDirectory()) { outputFile.mkdirs(); continue; } int count = 0; final int BUFFER = 2048; byte[] data = new byte[2048]; FileOutputStream fos = new FileOutputStream(outputFile); BufferedOutputStream destinationStream = new BufferedOutputStream( fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { destinationStream.write(data, 0, count); } destinationStream.flush(); destinationStream.close(); } zis.close(); System.out.println("Done unzipping"); } }