Java examples for File Path IO:Zip File
Extracts a ZIP archive into the target directory
//package com.java2s; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Path; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { protected static final int bufferSize = 1024; /**//from w w w . j a v a 2 s .com * Extracts a ZIP archive into the target directory * * @param archive Path to the archive to extract * @param targetDirectory Directory to which the files will be extracted * @throws Exception */ public static synchronized void extract(Path archive, Path targetDirectory) throws Exception { System.out.println("Unzipping " + archive.getFileName().toString() + "..."); byte[] buffer = new byte[bufferSize]; // extracts the source code archive to the target directory try (ZipInputStream zin = new ZipInputStream(new FileInputStream( archive.toFile()))) { ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { Path extractedFile = targetDirectory.resolve(ze.getName()); try (FileOutputStream fout = new FileOutputStream( extractedFile.toFile())) { int len; while ((len = zin.read(buffer)) > 0) { fout.write(buffer, 0, len); } zin.closeEntry(); } catch (IOException ioe_inner) { throw new Exception( "Error while extracting a file from the archive: " + extractedFile); } } } catch (IOException ioe_outter) { throw new Exception("Error while extracting " + archive); } System.out.println("Unzipping " + archive.getFileName().toString() + " done."); } }