Here you can find the source of unzip(File zip, File extractTo)
Parameter | Description |
---|---|
zip | a parameter |
extractTo | a parameter |
public static void unzip(File zip, File extractTo)
//package com.java2s; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.io.InputStream; public class Main { /**/* w ww .j a va2 s . c om*/ * Desenzippa un fitxer .zip en un directori * * @param zip * @param extractTo */ public static void unzip(File zip, File extractTo) { try { ZipFile archive = new ZipFile(zip); Enumeration<?> e = archive.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); File file = new File(extractTo, entry.getName()); if (entry.isDirectory() && !file.exists()) { file.mkdirs(); } else if (!entry.isDirectory()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } InputStream in = archive.getInputStream(entry); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); byte[] buffer = new byte[8192]; int read; while (-1 != (read = in.read(buffer))) { out.write(buffer, 0, read); } in.close(); out.close(); } } archive.close(); } catch (IOException e) { System.err.println("Error desenzippant el fitxer: " + zip.getName() + " al directori: " + extractTo + ". Error: " + e.getMessage()); } } }