List of usage examples for java.util.zip ZipInputStream closeEntry
public void closeEntry() throws IOException
From source file:org.bonitasoft.platform.setup.PlatformSetupTestUtils.java
private static void extractZipEntries(final ZipInputStream zipInputstream, final File outputFolder) throws IOException { ZipEntry zipEntry;/*from w w w . j ava 2s. c o m*/ while ((zipEntry = zipInputstream.getNextEntry()) != null) { try { // For each entry, a file is created in the output directory "folder" final File outputFile = new File(outputFolder.getAbsolutePath(), zipEntry.getName()); // If the entry is a directory, it creates in the output folder, and we go to the next entry (continue). if (zipEntry.isDirectory()) { outputFile.mkdirs(); continue; } writeZipInputToFile(zipInputstream, outputFile); } finally { zipInputstream.closeEntry(); } } }
From source file:org.wso2.carbon.cluster.utils.FileUtils.java
public static boolean unzipFile(String zipFilePath, String destDirectory) { boolean isExtracted = true; ZipInputStream zipIn = null; try {// w w w.j a v a 2s.com File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); while (entry != null) { textArea.append("INFO :: Extracting :: " + entry.getName() + "\n"); String filePath = destDirectory + File.separator + entry.getName(); if (!entry.isDirectory()) { extractFile(zipIn, filePath); } else { File dir = new File(filePath); dir.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } textArea.append("========================================================================" + "\n"); textArea.append("INFO :: Successfully Extracted :: " + "\n"); textArea.append("========================================================================" + "\n"); } catch (IOException e) { isExtracted = false; textArea.append("========================================================================" + "\n"); textArea.append("ERROR :: " + e.toString() + "\n"); textArea.append("========================================================================" + "\n"); } finally { if (zipIn != null) { try { zipIn.close(); } catch (IOException ex) { } } } return isExtracted; }
From source file:com.jlgranda.fede.ejb.url.reader.FacturaElectronicaURLReader.java
/** * Obtiene la lista de objetos factura para el sujeto en fede * * @param urls URLs hacia los archivo XML o ZIP a leer * @return una lista de instancias FacturaReader *///from w w w . ja v a2s . c o m public static FacturaReader getFacturaElectronica(String url) throws Exception { FacturaReader facturaReader = null; if (url.endsWith(".xml")) { String xml = FacturaElectronicaURLReader.read(url); facturaReader = new FacturaReader(FacturaUtil.read(xml), xml, url); } else if (url.endsWith(".zip")) { URL url_ = new URL(url); InputStream is = url_.openStream(); ZipInputStream zis = new ZipInputStream(is); try { ZipEntry entry = null; String tmp = null; ByteArrayOutputStream fout = null; while ((entry = zis.getNextEntry()) != null) { if (entry.getName().toLowerCase().endsWith(".xml")) { fout = new ByteArrayOutputStream(); for (int c = zis.read(); c != -1; c = zis.read()) { fout.write(c); } tmp = new String(fout.toByteArray(), Charset.defaultCharset()); facturaReader = new FacturaReader(FacturaUtil.read(tmp), tmp, url); fout.close(); } zis.closeEntry(); } zis.close(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(zis); } } return facturaReader; }
From source file:gabi.FileUploadServlet.java
private static void unzip(String zipFilePath, String destDir) { File dir = new File(destDir); // create output directory if it doesn't exist if (!dir.exists()) dir.mkdirs();//from w w w . j ava2s .co m FileInputStream fis; //buffer for read and write data to file byte[] buffer = new byte[1024]; try { fis = new FileInputStream(zipFilePath); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(destDir + File.separator + fileName); System.out.println("Unzipping to " + newFile.getAbsolutePath()); //create directories for sub directories in zip new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); //close this ZipEntry zis.closeEntry(); ze = zis.getNextEntry(); } //close last ZipEntry zis.closeEntry(); zis.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void unzip(InputStream is, String dir) throws IOException { File dest = new File(dir); if (!dest.exists()) { dest.mkdirs();/*from www. j a v a2s . c o m*/ } if (!dest.isDirectory()) throw new IOException("Invalid Unzip destination " + dest); if (null == is) { throw new IOException("InputStream is null"); } ZipInputStream zip = new ZipInputStream(is); ZipEntry ze; while ((ze = zip.getNextEntry()) != null) { final String path = dest.getAbsolutePath() + File.separator + ze.getName(); String zeName = ze.getName(); char cTail = zeName.charAt(zeName.length() - 1); if (cTail == File.separatorChar) { File file = new File(path); if (!file.exists()) { if (!file.mkdirs()) { throw new IOException("Unable to create folder " + file); } } continue; } FileOutputStream fout = new FileOutputStream(path); byte[] bytes = new byte[1024]; int c; while ((c = zip.read(bytes)) != -1) { fout.write(bytes, 0, c); } zip.closeEntry(); fout.close(); } }
From source file:Main.java
public static void unZip(String zipFile, String outputFolder) throws IOException { byte[] buffer = new byte[1024]; //create output directory is not exists File folder = new File(outputFolder); if (!folder.exists()) { folder.mkdir();//from w ww .j a va 2 s . c o m } //get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); //create all non exists folders //else you will hit FileNotFoundException for compressed folder if (ze.isDirectory()) newFile.mkdirs(); else { newFile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); }
From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java
public static File unzip(byte[] zipData, File directory) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(zipData); ZipInputStream zis = new ZipInputStream(bais); ZipEntry entry = zis.getNextEntry(); File root = null;//from w w w.ja va 2 s . c o m while (entry != null) { if (entry.isDirectory()) { File f = new File(directory, entry.getName()); f.mkdir(); if (root == null) { root = f; } } else { BufferedOutputStream out; out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())), BUFFER_SIZE); // ZipInputStream can only give us one byte at a time... for (int data = zis.read(); data != -1; data = zis.read()) { out.write(data); } out.close(); } zis.closeEntry(); entry = zis.getNextEntry(); } zis.close(); return root; }
From source file:org.n52.oxf.util.IOHelper.java
public static void decompressAll(File zipFile, File targetDirectory) throws IOException, ZipException { if (!targetDirectory.isDirectory()) throw new IOException("2nd Parameter targetDirectory is not a valid diectory!"); byte[] buf = new byte[4096]; ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile)); while (true) { // Nchsten Eintrag lesen ZipEntry entry = in.getNextEntry(); if (entry == null) { break; }/* www .j av a2s . c o m*/ // Ausgabedatei erzeugen FileOutputStream out = new FileOutputStream( targetDirectory.getAbsolutePath() + "/" + new File(entry.getName()).getName()); // Lese-Vorgang int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.close(); // Eintrag schlieen in.closeEntry(); } in.close(); }
From source file:com.dv.util.DataViewerZipUtil.java
/** * * @param zipInput// w w w.j ava 2 s. co m * @param dest * @throws IOException */ private static void doUnzipFile(ZipInputStream zipInput, File dest) throws IOException { if (!dest.exists()) { FileUtils.forceMkdir(dest); } for (ZipEntry entry; (entry = zipInput.getNextEntry()) != null;) { String entryName = entry.getName(); File file = new File(dest, entry.getName()); if (entryName.endsWith(File.separator)) { FileUtils.forceMkdir(file); } else { OutputStream out = FileUtils.openOutputStream(file); try { IOUtils.copy(zipInput, out); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(out); } } zipInput.closeEntry(); } }
From source file:com.amalto.core.jobox.util.JoboxUtil.java
public static void extract(String zipPathFile, String destinationPath) throws Exception { FileInputStream fins = new FileInputStream(zipPathFile); ZipInputStream zipInputStream = new ZipInputStream(fins); try {/* ww w . ja va 2s. com*/ ZipEntry ze; byte ch[] = new byte[256]; while ((ze = zipInputStream.getNextEntry()) != null) { File zipFile = new File(destinationPath + ze.getName()); File zipFilePath = new File(zipFile.getParentFile().getPath()); if (ze.isDirectory()) { if (!zipFile.exists()) { if (!zipFile.mkdirs()) { LOGGER.error("Create folder failed for '" + zipFile.getAbsolutePath() + "'."); //$NON-NLS-1$ //$NON-NLS-2$ } } zipInputStream.closeEntry(); } else { if (!zipFilePath.exists()) { if (!zipFilePath.mkdirs()) { LOGGER.error("Create folder failed for '" + zipFilePath.getAbsolutePath() + "'."); //$NON-NLS-1$ //$NON-NLS-2$ } } FileOutputStream fileOutputStream = new FileOutputStream(zipFile); try { int i; while ((i = zipInputStream.read(ch)) != -1) { fileOutputStream.write(ch, 0, i); } zipInputStream.closeEntry(); } finally { fileOutputStream.close(); } } } } finally { IOUtils.closeQuietly(fins); IOUtils.closeQuietly(zipInputStream); } }