List of usage examples for java.util.zip ZipInputStream closeEntry
public void closeEntry() throws IOException
From source file:org.deeplearning4j.util.ArchiveUtils.java
/** * Extracts files to the specified destination * @param file the file to extract to//from w w w.j a v a2 s . c om * @param dest the destination directory * @throws IOException */ public static void unzipFileTo(String file, String dest) throws IOException { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; if (file.endsWith(".zip")) { //getFromOrigin the zip file content ZipInputStream zis = new ZipInputStream(fin); //getFromOrigin the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(dest + File.separator + fileName); log.info("file unzip : " + newFile.getAbsoluteFile()); //createComplex all non exists folders //else you will hit FileNotFoundException for compressed folder new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(data)) > 0) { fos.write(data, 0, len); } fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) { BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".gz")) { GZIPInputStream is2 = new GZIPInputStream(fin); File extracted = new File(target.getParent(), target.getName().replace(".gz", "")); if (extracted.exists()) extracted.delete(); extracted.createNewFile(); OutputStream fos = FileUtils.openOutputStream(extracted); IOUtils.copyLarge(is2, fos); is2.close(); fos.flush(); fos.close(); } target.delete(); }
From source file:it.geosolutions.tools.compress.file.Extractor.java
/** * Unzips the files from a zipfile into a directory. All of the files will be put in a single * direcotry. If the zipfile contains a hierarchycal structure, it will be ignored. * //from ww w .j av a 2 s . c om * @param zipFile * The zipfile to be examined * @param destDir * The direcotry where the extracted files will be stored. * @return The list of the extracted files, or null if an error occurred. * @throws IllegalArgumentException * if the destination dir is not writeable. * @deprecated use Extractor.unZip instead which support complex zip structure */ public static List<File> unzipFlat(final File zipFile, final File destDir) { // if (!destDir.isDirectory()) // throw new IllegalArgumentException("Not a directory '" + destDir.getAbsolutePath() // + "'"); if (!destDir.canWrite()) throw new IllegalArgumentException("Unwritable directory '" + destDir.getAbsolutePath() + "'"); try { List<File> ret = new ArrayList<File>(); ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(zipFile)); for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream .getNextEntry()) { String entryName = zipentry.getName(); if (zipentry.isDirectory()) continue; File outFile = new File(destDir, entryName); ret.add(outFile); FileOutputStream fileoutputstream = new FileOutputStream(outFile); org.apache.commons.io.IOUtils.copy(zipinputstream, fileoutputstream); fileoutputstream.close(); zipinputstream.closeEntry(); } zipinputstream.close(); return ret; } catch (Exception e) { LOGGER.warn("Error unzipping file '" + zipFile.getAbsolutePath() + "'", e); return null; } }
From source file:org.n52.movingcode.runtime.codepackage.ZippedPackage.java
/** * Static private method to extract the description from a package * /*from w w w . ja v a 2 s. c om*/ * @param archive * {@link ZippedPackage} * @return {@link PackageDescriptionDocument} */ private static PackageDescriptionDocument extractDescription(ZippedPackage archive) { // zipFile and zip url MUST not be null at the same time assert (!((archive.zipFile == null) && (archive.zipURL == null))); String archiveName = null; try { ZipInputStream zis = null; if (archive.zipFile != null) { zis = new ZipInputStream(new FileInputStream(archive.zipFile)); archiveName = archive.zipFile.getAbsolutePath(); } else if (archive.zipURL != null) { zis = new ZipInputStream(archive.zipURL.openConnection().getInputStream()); archiveName = archive.zipURL.toString(); } ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (entry.getName().equalsIgnoreCase(Constants.PACKAGE_DESCRIPTION_XML)) { PackageDescriptionDocument doc = PackageDescriptionDocument.Factory.parse(zis); zis.close(); return doc; } zis.closeEntry(); } } catch (ZipException e) { logger.error("Error! Could read from archive: " + archiveName); } catch (IOException e) { logger.error("Error! Could not open archive: " + archiveName); } catch (XmlException e) { logger.error("Error! Could not parse package description from archive: " + archiveName); } return null; }
From source file:org.openecomp.sdc.common.util.ZipUtil.java
public static byte[] unzip(byte[] zipped) { ZipInputStream zipinputstream = null; ByteArrayOutputStream outputStream = null; try {//from ww w. j ava 2 s . c o m byte[] buf = new byte[1024]; zipinputstream = new ZipInputStream(new ByteArrayInputStream(zipped)); ZipEntry zipentry = zipinputstream.getNextEntry(); outputStream = new ByteArrayOutputStream(); int n; while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { outputStream.write(buf, 0, n); } return outputStream.toByteArray(); } catch (Exception e) { throw new IllegalStateException("Can't unzip input stream", e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { log.debug("Failed to close output stream", e); } } if (zipinputstream != null) { try { zipinputstream.closeEntry(); zipinputstream.close(); } catch (IOException e) { log.debug("Failed to close zip input stream", e); } } } }
From source file:org.openecomp.sdc.common.util.ZipUtil.java
public static Map<String, byte[]> readZip(ZipInputStream zis) { Map<String, byte[]> fileNameToByteArray = new HashMap<String, byte[]>(); byte[] buffer = new byte[1024]; try {/*from w w w . j av a 2 s .c o m*/ // get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); if (false == ze.isDirectory()) { ByteArrayOutputStream os = new ByteArrayOutputStream(); try { int len; while ((len = zis.read(buffer)) > 0) { os.write(buffer, 0, len); } fileNameToByteArray.put(fileName, os.toByteArray()); } finally { if (os != null) { os.close(); } } } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException ex) { ex.printStackTrace(); return null; } finally { if (zis != null) { try { zis.closeEntry(); zis.close(); } catch (IOException e) { // TODO: add log } } } return fileNameToByteArray; }
From source file:com.t3.persistence.FileUtil.java
public static void unzip(ZipInputStream in, File destDir) throws IOException { if (in == null) throw new IOException("input stream cannot be null"); // Prepare destination destDir.mkdirs();/*from w w w . ja v a2s . com*/ String absDestDir = destDir.getAbsolutePath() + File.separator; // Pull out the files ZipEntry entry = null; while ((entry = in.getNextEntry()) != null) { if (entry.isDirectory()) continue; // Prepare file destination String path = absDestDir + entry.getName(); File file = new File(path); file.getParentFile().mkdirs(); try (OutputStream out = new FileOutputStream(file)) { IOUtils.copy(in, out); } in.closeEntry(); } }
From source file:com.bukanir.android.utils.Utils.java
public static String unzipSubtitle(String zip, String path) { InputStream is;/*from w w w. ja v a 2 s.c om*/ ZipInputStream zis; try { String filename = null; is = new FileInputStream(zip); zis = new ZipInputStream(new BufferedInputStream(is)); ZipEntry ze; byte[] buffer = new byte[1024]; int count; while ((ze = zis.getNextEntry()) != null) { filename = ze.getName(); if (ze.isDirectory()) { File fmd = new File(path + "/" + filename); fmd.mkdirs(); continue; } if (filename.endsWith(".srt") || filename.endsWith(".sub")) { FileOutputStream fout = new FileOutputStream(path + "/" + filename); while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); zis.closeEntry(); break; } zis.closeEntry(); } zis.close(); File z = new File(zip); z.delete(); return path + "/" + filename; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java
public static InputStream getShapefileFromCompressed(InputStream is, ShpFileType type) { InputStream shapefile = null; ZipInputStream zis = new ZipInputStream(is); ZipEntry ze;// ww w . j av a2 s . c om try { while ((ze = zis.getNextEntry()) != null) { if (!ze.isDirectory()) { String fileName = ze.getName().toLowerCase(); String baseShapefileName = type.toBase(fileName); if (baseShapefileName != null) { shapefile = zis; break; } } } } catch (IOException e) { try { zis.closeEntry(); } catch (IOException e2) { // TODO Auto-generated catch block e.printStackTrace(); } try { zis.close(); } catch (IOException e2) { // TODO Auto-generated catch block e.printStackTrace(); } } return shapefile; }
From source file:org.sakaiproject.portal.charon.test.PortalTestFileUtils.java
/** * unpack a segment from a zip//from ww w.java 2 s. co m * * @param addsi * @param packetStream * @param version */ public static void unpack(InputStream source, File destination) throws IOException { ZipInputStream zin = new ZipInputStream(source); ZipEntry zipEntry = null; FileOutputStream fout = null; try { byte[] buffer = new byte[4096]; while ((zipEntry = zin.getNextEntry()) != null) { long ts = zipEntry.getTime(); // the zip entry needs to be a full path from the // searchIndexDirectory... hence this is correct File f = new File(destination, zipEntry.getName()); if (log.isDebugEnabled()) log.debug(" Unpack " + f.getAbsolutePath()); f.getParentFile().mkdirs(); fout = new FileOutputStream(f); int len; while ((len = zin.read(buffer)) > 0) { fout.write(buffer, 0, len); } zin.closeEntry(); fout.close(); f.setLastModified(ts); } } finally { try { fout.close(); } catch (Exception ex) { } } }
From source file:org.openremote.modeler.utils.ZipUtils.java
/** * Unzip a zip./* ww w . ja va 2 s .c om*/ * * @param inputStream the input stream * @param targetDir the target dir * * @return true, if success */ public static boolean unzip(InputStream inputStream, String targetDir) { ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry zipEntry; FileOutputStream fileOutputStream = null; try { while ((zipEntry = zipInputStream.getNextEntry()) != null) { if (!zipEntry.isDirectory()) { targetDir = targetDir.endsWith("/") || targetDir.endsWith("\\") ? targetDir : targetDir + "/"; File zippedFile = new File(targetDir + zipEntry.getName()); FileUtilsExt.deleteQuietly(zippedFile); fileOutputStream = new FileOutputStream(zippedFile); int b; while ((b = zipInputStream.read()) != -1) { fileOutputStream.write(b); } fileOutputStream.close(); } } } catch (IOException e) { LOGGER.error("Can't unzip to " + targetDir, e); return false; } finally { try { zipInputStream.closeEntry(); if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { LOGGER.error("Error while closing stream.", e); } } return true; }