Here you can find the source of unzip(File file, String destination, String container)
Parameter | Description |
---|---|
container | all extracted file will be stored in container, which will be created in the destination |
file | a parameter |
destination | where the file will be stored |
public static File unzip(File file, String destination, String container)
//package com.java2s; //License from project: Open Source License import java.io.BufferedOutputStream; 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 { /**//from ww w . jav a2 s .c o m * @param container * all extracted file will be stored in container, which will be * created in the destination * @param file * @param destination * where the file will be stored * @return unzipped file * */ public static File unzip(File file, String destination, String container) { File result = new File(destination + "/" + container); result.mkdir(); destination = destination + "/" + container + "/"; try { ZipFile zipFile = new ZipFile(file); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry ze = entries.nextElement(); if (ze.isDirectory()) (new File(destination + ze.getName())).mkdir(); else { // make sure directories exist in case the client // didn't provide directory entries! File f = new File(destination + ze.getName()); (new File(f.getParent())).mkdirs(); FileOutputStream fos = new FileOutputStream(f); BufferedOutputStream bos = new BufferedOutputStream(fos); InputStream in = zipFile.getInputStream(ze); copystream(in, bos); bos.close(); } } zipFile.close(); } catch (IOException ioex) { ioex.printStackTrace(); } return result; } public static void copystream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); in.close(); // out.close(); } }