List of usage examples for java.util.zip ZipInputStream getNextEntry
public ZipEntry getNextEntry() throws IOException
From source file:io.druid.java.util.common.CompressionUtils.java
/** * Decompress an input stream from a file, based on the filename. *///w ww .j av a 2s. co m public static InputStream decompress(final InputStream in, final String fileName) throws IOException { if (fileName.endsWith(GZ_SUFFIX)) { return gzipInputStream(in); } else if (fileName.endsWith(BZ2_SUFFIX)) { return new BZip2CompressorInputStream(in, true); } else if (fileName.endsWith(XZ_SUFFIX)) { return new XZCompressorInputStream(in, true); } else if (fileName.endsWith(ZIP_SUFFIX)) { // This reads the first file in the archive. final ZipInputStream zipIn = new ZipInputStream(in, StandardCharsets.UTF_8); try { final ZipEntry nextEntry = zipIn.getNextEntry(); if (nextEntry == null) { zipIn.close(); // No files in the archive - return an empty stream. return new ByteArrayInputStream(new byte[0]); } return zipIn; } catch (IOException e) { try { zipIn.close(); } catch (IOException e2) { e.addSuppressed(e2); } throw e; } } else { return in; } }
From source file:Main.java
public static boolean unzipFile(InputStream fis, String destDir) { final byte[] buffer = new byte[4096]; ZipInputStream zis = null; Log.e("Unzip", "destDir = " + destDir); try {/*from w ww . j a v a 2 s . co m*/ // make sure the directory is existent File dstFile = new File(destDir); if (!dstFile.exists()) { dstFile.mkdirs(); } else { int fileLenght = dstFile.listFiles().length; if (fileLenght >= 2) { return true; } } zis = new ZipInputStream(fis); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { String fileName = entry.getName(); if (entry.isDirectory()) { new File(destDir, fileName).mkdirs(); } else { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File(destDir, fileName))); int lenRead; while ((lenRead = zis.read(buffer)) != -1) { bos.write(buffer, 0, lenRead); } bos.close(); } zis.closeEntry(); } return true; } catch (IOException e) { e.printStackTrace(); } finally { if (zis != null) { try { zis.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
From source file:edu.indiana.d2i.htrc.dataapi.DataAPIWrapper.java
private static void openZipStream(InputStream inputStream, Map<String, Map<String, String>> volPageContents, boolean isConcat) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry zipEntry = null;/* w ww. ja v a 2s .c o m*/ String currentVolID = null; if (!isConcat) { while ((zipEntry = zipInputStream.getNextEntry()) != null) { String name = zipEntry.getName(); /** * check whether encounter ERROR.err */ if (ERROR_FNAME.equals(name)) { log.error("Encountered ERROR.err file in the zip stream"); // log the content of ERROR.err file log.error("*************** Start of Content of ERROR.err ***************"); StringBuilder sb = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(zipInputStream)); String line = null; while ((line = reader.readLine()) != null) sb.append(line + "\n"); log.error(sb.toString()); log.error("*************** End of Content of ERROR.err ***************"); continue; } if (zipEntry.isDirectory()) { // get volume name currentVolID = name.split("/")[0]; volPageContents.put(currentVolID, new HashMap<String, String>()); if (log.isDebugEnabled()) log.debug("Encounter volueme directory : " + currentVolID); } else { String[] names = name.split("/"); // each page is a separate entry // assert names.length == 2; // assert names[0].equals(currentVolID); // get page(s) content StringBuilder sb = new StringBuilder(); BufferedReader reader = new BufferedReader( new InputStreamReader(zipInputStream, Charset.forName("UTF-8"))); String line = null; while ((line = reader.readLine()) != null) sb.append(line + "\n"); Map<String, String> contents = volPageContents.get(currentVolID); int idx = names[1].indexOf(".txt"); // assert idx != -1; contents.put(names[1].substring(0, idx), sb.toString()); } } } else { while ((zipEntry = zipInputStream.getNextEntry()) != null) { assert !zipEntry.isDirectory(); String name = zipEntry.getName(); /** * check whether encounter ERROR.err */ if (ERROR_FNAME.equals(name)) { log.error("Encountered ERROR.err file in the zip stream"); // log the content of ERROR.err file log.error("*************** Start of Content of ERROR.err ***************"); StringBuilder sb = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(zipInputStream)); String line = null; while ((line = reader.readLine()) != null) sb.append(line + "\n"); log.error(sb.toString()); log.error("*************** End of Content of ERROR.err ***************"); continue; } int idx = name.indexOf(".txt"); // assert idx != -1; String cleanedVolId = name.substring(0, idx); if (log.isDebugEnabled()) log.debug("Encounter volueme whole entry : " + cleanedVolId); StringBuilder sb = new StringBuilder(); BufferedReader reader = new BufferedReader( new InputStreamReader(zipInputStream, Charset.forName("UTF-8"))); String line = null; while ((line = reader.readLine()) != null) sb.append(line + "\n"); HashMap<String, String> concatContent = new HashMap<String, String>(); concatContent.put(DataAPIWrapper.WHOLE_CONTENT, sb.toString()); volPageContents.put(cleanedVolId, concatContent); } } }
From source file:com.ikon.util.ReportUtils.java
/** * Get report parameters/* ww w. jav a 2 s . c o m*/ */ public static List<FormElement> getReportParameters(long rpId) throws ParseException, DatabaseException, IOException { log.debug("getReportParameters({})", rpId); List<FormElement> params = null; ByteArrayInputStream bais = null; ZipInputStream zis = null; try { Report rp = ReportDAO.findByPk(rpId); bais = new ByteArrayInputStream(SecureStore.b64Decode(rp.getFileContent())); zis = new ZipInputStream(bais); ZipEntry zi = null; while ((zi = zis.getNextEntry()) != null) { if ("params.xml".equals(zi.getName())) { params = FormUtils.parseReportParameters(zis); break; } } if (params == null) { params = new ArrayList<FormElement>(); log.warn("Report '{}' has no params.xml file", rpId); } // Activity log // UserActivity.log(session.getUserID(), "GET_REPORT_PARAMETERS", rpId+"", null); } catch (IOException e) { throw e; } finally { IOUtils.closeQuietly(zis); IOUtils.closeQuietly(bais); } log.debug("getReportParameters: {}", params); return params; }
From source file:com.openkm.util.ReportUtils.java
/** * Get report parameters/*from w ww. ja v a 2 s .c o m*/ */ public static List<FormElement> getReportParameters(long rpId) throws ParseException, DatabaseException, IOException { log.debug("getReportParameters({})", rpId); long begin = System.currentTimeMillis(); List<FormElement> params = null; ByteArrayInputStream bais = null; ZipInputStream zis = null; try { Report rp = ReportDAO.findByPk(rpId); bais = new ByteArrayInputStream(SecureStore.b64Decode(rp.getFileContent())); zis = new ZipInputStream(bais); ZipEntry zi = null; while ((zi = zis.getNextEntry()) != null) { if ("params.xml".equals(zi.getName())) { params = FormUtils.parseReportParameters(zis); break; } } if (params == null) { params = new ArrayList<FormElement>(); log.warn("Report '{}' has no params.xml file", rpId); } // Activity log // UserActivity.log(session.getUserID(), "GET_REPORT_PARAMETERS", rpId+"", null); } catch (IOException e) { throw e; } finally { IOUtils.closeQuietly(zis); IOUtils.closeQuietly(bais); } log.trace("getReportParameters.Time: {}", System.currentTimeMillis() - begin); log.debug("getReportParameters: {}", params); return params; }
From source file:com.glaf.core.util.ZipUtils.java
public static byte[] getBytes(ZipInputStream zipInputStream, String name) { byte[] bytes = null; try {// w w w.j a va2 s . com ZipEntry zipEntry = zipInputStream.getNextEntry(); while (zipEntry != null) { String entryName = zipEntry.getName(); if (entryName.equalsIgnoreCase(name)) { bytes = FileUtils.getBytes(zipInputStream); if (bytes != null) { break; } } zipEntry = zipInputStream.getNextEntry(); } return bytes; } catch (Exception ex) { throw new RuntimeException(ex); } }
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". * /*from w w w . j a va 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:com.matze5800.paupdater.Functions.java
public static void addFilesToExistingZip(File zipFile, File[] files) throws IOException { File tempFile = new File(Environment.getExternalStorageDirectory() + "/pa_updater", "temp_kernel.zip"); tempFile.delete();//from w w w .j a va2 s . co m boolean renameOk = zipFile.renameTo(tempFile); if (!renameOk) { throw new RuntimeException( "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath()); } byte[] buf = new byte[1024]; ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); boolean notInFiles = true; for (File f : files) { if (f.getName().equals(name)) { notInFiles = false; break; } } if (notInFiles) { out.putNextEntry(new ZipEntry(name)); int len; while ((len = zin.read(buf)) > 0) { out.write(buf, 0, len); } } entry = zin.getNextEntry(); } zin.close(); for (int i = 0; i < files.length; i++) { InputStream in = new FileInputStream(files[i]); out.putNextEntry(new ZipEntry(files[i].getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); tempFile.delete(); }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
/** * @param zipFile//from www . j a v a 2 s . co m * @param files * @throws java.io.IOException */ public static void addFilesToZip(File zipFile, Map<String, File> files) throws IOException { // get a temp file File tempFile = File.createTempFile(zipFile.getName(), null); // delete it, otherwise you cannot rename your existing zip to it. FileUtils.deleteQuietly(tempFile); boolean renameOk = zipFile.renameTo(tempFile); if (!renameOk) { throw new RuntimeException( "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath()); } ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); boolean notInFiles = true; for (Map.Entry<String, File> e : files.entrySet()) { if (e.getKey().equals(name)) { notInFiles = false; break; } } if (notInFiles) { out.putNextEntry(new ZipEntry(name)); IOUtils.copy(zin, out); } entry = zin.getNextEntry(); } // Close the streams zin.close(); // Compress the files for (Map.Entry<String, File> e : files.entrySet()) { InputStream in = new FileInputStream(e.getValue()); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(e.getKey())); // Transfer bytes from the file to the ZIP file IOUtils.copy(in, out); // Complete the entry out.closeEntry(); IOUtils.closeQuietly(in); } // Complete the ZIP file IOUtils.closeQuietly(out); FileUtils.deleteQuietly(tempFile); }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
/** * @param zipFile// ww w .j a va 2s . c om * @param files * @throws java.io.IOException */ public static void addStreamsToZip(File zipFile, Map<String, InputStream> files) throws IOException { // get a temp file File tempFile = File.createTempFile(zipFile.getName(), null); // delete it, otherwise you cannot rename your existing zip to it. FileUtils.deleteQuietly(tempFile); boolean renameOk = zipFile.renameTo(tempFile); if (!renameOk) { throw new RuntimeException( "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath()); } ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); boolean notInFiles = true; for (Map.Entry<String, InputStream> e : files.entrySet()) { if (e.getKey().equals(name)) { notInFiles = false; break; } } if (notInFiles) { out.putNextEntry(new ZipEntry(name)); IOUtils.copy(zin, out); } entry = zin.getNextEntry(); } // Close the streams zin.close(); // Compress the files for (Map.Entry<String, InputStream> e : files.entrySet()) { InputStream in = e.getValue(); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(e.getKey())); // Transfer bytes from the file to the ZIP file IOUtils.copy(in, out); // Complete the entry out.closeEntry(); IOUtils.closeQuietly(in); } // Complete the ZIP file IOUtils.closeQuietly(out); FileUtils.deleteQuietly(tempFile); }