List of usage examples for java.util.zip ZipInputStream closeEntry
public void closeEntry() throws IOException
From source file:org.zaproxy.libs.DownloadTools.java
public static void downloadDriver(String urlStr, String destDir, String destFile) { File dest = new File(destDir + destFile); if (dest.exists()) { System.out.println("Already exists: " + dest.getAbsolutePath()); return;//from w ww . j av a2 s .co m } File parent = dest.getParentFile(); if (!parent.exists() && !parent.mkdirs()) { System.out.println("Failed to create directory : " + dest.getParentFile().getAbsolutePath()); } byte[] buffer = new byte[1024]; if (urlStr.endsWith(".zip")) { try { URL url = new URL(urlStr); ZipInputStream zipIn = new ZipInputStream(url.openStream()); ZipEntry entry; boolean isFound = false; while ((entry = zipIn.getNextEntry()) != null) { if (destFile.equals(entry.getName())) { isFound = true; FileOutputStream out = new FileOutputStream(dest); int read = 0; while ((read = zipIn.read(buffer)) != -1) { out.write(buffer, 0, read); } out.close(); System.out.println("Updated: " + dest.getAbsolutePath()); } else { System.out.println("Found " + entry.getName()); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); if (!isFound) { System.out.println("Failed to find " + destFile); System.exit(1); } } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } else if (urlStr.endsWith(".tar.gz")) { try { URL url = new URL(urlStr); GZIPInputStream gzis = new GZIPInputStream(url.openStream()); File tarFile = new File(dest.getAbsolutePath() + ".tar"); FileOutputStream out = new FileOutputStream(tarFile); int len; while ((len = gzis.read(buffer)) > 0) { out.write(buffer, 0, len); } gzis.close(); out.close(); TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(tarFile)); ArchiveEntry entry; boolean isFound = false; while ((entry = tar.getNextEntry()) != null) { if (destFile.equals(entry.getName())) { out = new FileOutputStream(dest); isFound = true; int read = 0; while ((read = tar.read(buffer)) != -1) { out.write(buffer, 0, read); } out.close(); System.out.println("Updated: " + dest.getAbsolutePath()); } } tar.close(); tarFile.delete(); if (!isFound) { System.out.println("Failed to find " + destFile); System.exit(1); } } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } }
From source file:anotadorderelacoes.model.UtilidadesPacotes.java
/** * Verifica se um dado arquivo um classificador e, se sim, qual . Faz * isso descompactando o arquivo e procurando pelo arquivo "meta.ars". * /* w w w . j a v a 2s . c o m*/ * @param arquivo * * @return "invalido" se no um classificador, "svm" ou "j48" caso * contrrio. */ public static String verificarClassificador(File arquivo) { String classificador = "invalido"; try { ZipInputStream zis = new ZipInputStream(new FileInputStream(arquivo)); ZipEntry ze; byte[] buffer = new byte[4096]; while ((ze = zis.getNextEntry()) != null) { FileOutputStream fos = new FileOutputStream(ze.getName()); int numBytes; while ((numBytes = zis.read(buffer, 0, buffer.length)) != -1) fos.write(buffer, 0, numBytes); fos.close(); zis.closeEntry(); if (ze.getName().equals("meta.ars")) { BufferedReader br = new BufferedReader(new FileReader("meta.ars")); String linha; while ((linha = br.readLine()) != null) { String[] valores = linha.split(":"); if (valores[0].equals("classificador")) classificador = valores[1]; } br.close(); } new File(ze.getName()).delete(); } } catch (FileNotFoundException ex) { Logger.getLogger("ARS logger").log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger("ARS logger").log(Level.SEVERE, null, ex); } catch (NullPointerException ex) { //Logger.getLogger( "ARS logger" ).log( Level.SEVERE, null, ex ); return classificador; } return classificador; }
From source file:pl.psnc.synat.wrdz.common.utility.ZipUtility.java
/** * Unpacks specified input stream into destination directory. * //from w w w . java 2s . c o m * @param src * zip source file * @param dest * destination folder * @throws IOException * in case when something goes wrong in method */ public static void unzip(InputStream src, File dest) throws IOException { if (!dest.exists()) { throw new FileNotFoundException("Destination folder not found!"); } byte[] buffer = new byte[1024]; ZipInputStream zipInput = new ZipInputStream(src); ZipEntry entry = null; while ((entry = zipInput.getNextEntry()) != null) { if (!entry.isDirectory()) { File file = new File(dest, entry.getName()); File parentFile = file.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(file), 1024); int len; while ((len = zipInput.read(buffer)) != -1) { bos.write(buffer, 0, len); } } finally { bos.flush(); bos.close(); } } zipInput.closeEntry(); } }
From source file:Main.java
/** * kudos: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically) * @param zipFile// w ww . java2 s. co m * @param destinationDirectory */ public static void unZip(String zipFile, String destinationDirectory) { try { FileInputStream fin = new FileInputStream(zipFile); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { Log.v("Decompress", "Unzipping " + ze.getName()); String destinationPath = destinationDirectory + File.separator + ze.getName(); if (ze.isDirectory()) { dirChecker(destinationPath); } else { FileOutputStream fout; try { File outputFile = new File(destinationPath); if (!outputFile.getParentFile().exists()) { dirChecker(outputFile.getParentFile().getPath()); } fout = new FileOutputStream(destinationPath); for (int c = zin.read(); c != -1; c = zin.read()) { fout.write(c); } zin.closeEntry(); fout.close(); } catch (Exception e) { // ok for now. Log.v("Decompress", "Error: " + e.getMessage()); } } } zin.close(); } catch (Exception e) { Log.e("Decompress", "unzip", e); } }
From source file:org.sakaiproject.kernel.util.FileUtil.java
public static void unpack(InputStream source, File destination) throws IOException { ZipInputStream zin = new ZipInputStream(source); ZipEntry zipEntry = null;//w w w . j a v a 2 s.co m 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()); LOG.info(" Unpack " + f.getAbsolutePath()); f.getParentFile().mkdirs(); if (!zipEntry.isDirectory()) { 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) { LOG.warn("Exception closing file output stream", ex); } try { zin.close(); } catch (Exception ex) { LOG.warn("Exception closing file input stream", ex); } } }
From source file:prototypes.ws.proxy.soap.commons.io.Files.java
/** * Extracts a zip file specified by the zipFilePath to a directory specified * by destDirectory (will be created if does not exists) * * @param zipFilePath/*from w w w . jav a2 s. c o m*/ * @return */ public static String unzip(String zipFilePath) { try { File folder = createTempDirectory(); if (!folder.exists()) { folder.mkdir(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); // iterates over entries in the zip file while (entry != null) { String filePath = folder.getAbsolutePath() + File.separator + entry.getName(); if (!entry.isDirectory()) { // if the entry is a file, extracts it extractFile(zipIn, filePath); } else { // if the entry is a directory, make the directory File dir = new File(filePath); dir.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); return folder.getAbsolutePath(); } catch (FileNotFoundException ex) { LOGGER.warn(Messages.MSG_ERROR_DETAILS, ex); } catch (IOException ex) { LOGGER.warn(Messages.MSG_ERROR_DETAILS, ex); } return null; }
From source file:org.fusesource.ide.foundation.ui.archetypes.ArchetypeHelper.java
public static Map<String, String> getArchetypeRequiredProperties(URL jarURL) throws IOException, XPathExpressionException, ParserConfigurationException, SAXException { ZipInputStream zis = new ZipInputStream(jarURL.openStream()); try {// ww w . ja v a 2 s . c o m ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null) { try { if (!entry.isDirectory() && "META-INF/maven/archetype-metadata.xml".equals(entry.getName())) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); copy(zis, bos); bos.flush(); return getArchetypeRequiredProperties(bos, true, true); } } finally { zis.closeEntry(); } } } finally { zis.close(); } return Collections.EMPTY_MAP; }
From source file:org.lastmilehealth.collect.android.utilities.ZipUtils.java
/**private static void addFile(File fileObj, ZipOutputStream out) throws IOException { //Log.d("~", "Add file: "+fileObj.getName()); out.putNextEntry(new ZipEntry(fileObj.getName())); out.closeEntry();//www .j a v a 2s .c o m }*/ public static void unzip(String zipFile, String targetLocation) throws Exception { //delete target location to clear previously unzipped data try { org.apache.commons.io.FileUtils.deleteDirectory(new File(targetLocation)); } catch (Exception e) { } createDirIfNotExist(targetLocation); FileInputStream fin = new FileInputStream(zipFile); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { //create dir if required while unzipping if (ze.isDirectory()) { createDirIfNotExist(ze.getName()); } else { String path = targetLocation + "/" + ze.getName(); Log.d(TAG, "unzipping: " + path); if (path.contains("/")) { org.apache.commons.io.FileUtils.forceMkdir(new File(path.substring(0, path.lastIndexOf("/")))); } FileOutputStream fout = new FileOutputStream(path); for (int c = zin.read(); c != -1; c = zin.read()) { fout.write(c); } zin.closeEntry(); fout.close(); } } zin.close(); }
From source file:com.worldline.easycukes.commons.helpers.FileHelper.java
/** * Extracts the content of a zip folder in a specified directory * * @param from a {@link String} representation of the URL containing the zip * file to be unzipped/*from w ww .j ava2s . co m*/ * @param to the path on which the content should be extracted * @throws IOException if anything's going wrong while unzipping the content of the * provided zip folder */ public static void unzip(@NonNull String from, @NonNull String to, boolean isRemote) throws IOException { @Cleanup ZipInputStream zip = null; if (isRemote) zip = new ZipInputStream(new FileInputStream(from)); else zip = new ZipInputStream(FileHelper.class.getResourceAsStream(from)); log.debug("Extracting zip from: " + from + " to: " + to); // Extract without a container directory if exists ZipEntry entry = zip.getNextEntry(); String rootDir = "/"; if (entry != null) if (entry.isDirectory()) rootDir = entry.getName(); else { final String filePath = to + entry.getName(); // if the entry is a file, extracts it try { extractFile(zip, filePath); } catch (final FileNotFoundException fnfe) { log.warn(fnfe.getMessage(), fnfe); } } zip.closeEntry(); entry = zip.getNextEntry(); // iterates over entries in the zip file while (entry != null) { String entryName = entry.getName(); if (entryName.startsWith(rootDir)) entryName = entryName.replaceFirst(rootDir, ""); final String filePath = to + "/" + entryName; if (!entry.isDirectory()) // if the entry is a file, extracts it try { extractFile(zip, filePath); } catch (final FileNotFoundException fnfe) { log.warn(fnfe.getMessage(), fnfe); } else { // if the entry is a directory, make the directory final File dir = new File(filePath); dir.mkdir(); } zip.closeEntry(); entry = zip.getNextEntry(); } // delete the zip file if recovered from URL if (isRemote) new File(from).delete(); }
From source file:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java
/** * Unpacks an existing JAR/Zip archive./* ww w .j a va 2 s .c o m*/ * * @param zipFile The Archive file to unzip. * @param outputFolder The destination folder where the unzipped content shall be saved. * @see <a href="http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/"> * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/</a> */ private static void unZipIt(File zipFile, File outputFolder) { byte[] buffer = new byte[1024]; try { // create output directory is not exists File folder = outputFolder; if (folder.exists()) { FileUtils.deleteDirectory(folder); } folder.mkdir(); // 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, fileName); // create all non exists folders // else you will hit FileNotFoundException for compressed folder new File(newFile.getParent()).mkdirs(); if (!ze.isDirectory()) { 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(); } catch (IOException ex) { ex.printStackTrace(); } }