1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.io.*; import java.util.zip.ZipInputStream; import java.util.zip.ZipEntry; public class UnzipDemo { public static void main(String[] args) { String zipname = "data.zip"; try { FileInputStream fis = new FileInputStream(zipname); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; // Read each entry from the ZipInputStream until no more entry found while ((entry = zis.getNextEntry()) != null) { System.out.println("Unzipping: " + entry.getName()); int size; byte[] buffer = new byte[2048]; FileOutputStream fos = new FileOutputStream(entry.getName()); BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length); while ((size = zis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, size); } bos.flush(); bos.close(); } zis.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } |