List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream
public ZipArchiveInputStream(InputStream inputStream, String encoding, boolean useUnicodeExtraFields)
From source file:com.zimbra.cs.util.ZipUtil.java
/** * Traditional java.util.zip processing either assumes archives use UTF-8 for filenames or requires that * you know up front what charset is used for filenames. * This class uses the more versatile org.apache.commons.compress.archivers.zip package combined with * language information to make a best guess at what the filenames might be. * *///from ww w. ja v a 2 s. c o m public static List<String> getZipEntryNames(InputStream inputStream, Locale locale) throws IOException { List<String> zipEntryNames = Lists.newArrayList(); /* * From http://commons.apache.org/proper/commons-compress/zip.html * Traditionally the ZIP archive format uses CodePage 437 as encoding for file name, which is not sufficient for * many international character sets. Over time different archivers have chosen different ways to work around * the limitation - the java.util.zip packages simply uses UTF-8 as its encoding for example. * * For our purposes, CP437 has the advantage that all byte sequences are valid, so it works well as a final * fallback charset to assume for the name. */ try (ZipArchiveInputStream zis = new ZipArchiveInputStream(inputStream, cp437charset.name(), false /* useUnicodeExtraFields - we do our own handling of this */)) { ZipArchiveEntry ze; while ((ze = zis.getNextZipEntry()) != null) { if (ze.isDirectory()) { continue; } String entryName = bestGuessAtEntryName(ze, locale); zipEntryNames.add(entryName); } } return zipEntryNames; }
From source file:com.zimbra.cs.util.ZipUtil.java
/** * * @param inputStream archive input stream * @param locale - best guess as to locale for the filenames in the archive * @param seqNo - the order of the item to return (excluding directory entries) * @return/*w w w. j av a 2 s . c o m*/ * @throws IOException */ public static ZipNameAndSize getZipEntryNameAndSize(InputStream inputStream, Locale locale, int seqNo) throws IOException { ZipArchiveInputStream zis = new ZipArchiveInputStream(inputStream, cp437charset.name(), false /* useUnicodeExtraFields - we do our own handling of this */); ZipArchiveEntry ze; int idx = 0; while ((ze = zis.getNextZipEntry()) != null) { if (ze.isDirectory()) { continue; } if (idx++ == seqNo) { String entryName = bestGuessAtEntryName(ze, locale); return new ZipNameAndSize(entryName, ze.getSize(), zis); } } zis.close(); throw new IOException("file " + seqNo + " not in archive"); }
From source file:org.apache.ant.compress.util.ZipStreamFactory.java
/** * @param stream the stream to read from, should be buffered * @param encoding the encoding of the entry names *//*ww w. j a va 2s . c o m*/ public ArchiveInputStream getArchiveStream(InputStream stream, String encoding) throws IOException { return new ZipArchiveInputStream(stream, encoding, true); }
From source file:org.dbflute.helper.io.compress.DfZipArchiver.java
/** * Extract the archive file to the directory. * @param baseDir The base directory to compress. (NotNull) * @param filter The file filter, which doesn't need to accept the base directory. (NotNull) *//*from w w w .ja v a2 s . c om*/ public void extract(File baseDir, FileFilter filter) { if (baseDir == null) { String msg = "The argument 'baseDir' should not be null."; throw new IllegalArgumentException(msg); } if (baseDir.exists() && !baseDir.isDirectory()) { String msg = "The baseDir should be directory but not: " + baseDir; throw new IllegalArgumentException(msg); } baseDir.mkdirs(); final String baseDirPath = resolvePath(baseDir); InputStream ins = null; ZipArchiveInputStream archive = null; try { ins = new FileInputStream(_zipFile); archive = new ZipArchiveInputStream(ins, "UTF-8", true); ZipArchiveEntry entry; while ((entry = archive.getNextZipEntry()) != null) { final String entryName = resolveFileSeparator(entry.getName()); // just in case final File file = new File(baseDirPath + "/" + entryName); if (!filter.accept(file)) { continue; } if (entry.isDirectory()) { file.mkdirs(); } else { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } OutputStream out = null; try { out = new FileOutputStream(file); IOUtils.copy(archive, out); out.close(); } catch (IOException e) { String msg = "Failed to IO-copy the file: " + file.getPath(); throw new IllegalStateException(msg, e); } finally { if (out != null) { try { out.close(); } catch (IOException ignored) { } } } } } } catch (IOException e) { String msg = "Failed to extract the files from " + _zipFile.getPath(); throw new IllegalArgumentException(msg, e); } finally { if (archive != null) { try { archive.close(); } catch (IOException ignored) { } } if (ins != null) { try { ins.close(); } catch (IOException ignored) { } } } }
From source file:org.ngrinder.common.util.CompressionUtil.java
/** * Unzip the given input stream into destination directory with the given character set. * // www .ja v a 2 s. c om * @param is * input stream * @param destDir * destination directory * @param charsetName * character set name */ public static void unzip(InputStream is, File destDir, String charsetName) { ZipArchiveInputStream zis = null; try { ZipArchiveEntry entry; String name; File target; int nWritten = 0; BufferedOutputStream bos; byte[] buf = new byte[1024 * 8]; zis = new ZipArchiveInputStream(is, charsetName, false); while ((entry = zis.getNextZipEntry()) != null) { name = entry.getName(); target = new File(destDir, name); if (entry.isDirectory()) { target.mkdirs(); /* does it always work? */ } else { target.createNewFile(); bos = new BufferedOutputStream(new FileOutputStream(target)); while ((nWritten = zis.read(buf)) >= 0) { bos.write(buf, 0, nWritten); } bos.close(); } } } catch (Exception e) { throw new NGrinderRuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(zis); } }
From source file:org.ngrinder.script.util.CompressionUtil.java
public void unzip(InputStream is, File destDir, String charsetName) throws IOException { ZipArchiveInputStream zis;/* w w w .j ava 2s.c om*/ ZipArchiveEntry entry; String name; File target; int nWritten = 0; BufferedOutputStream bos; byte[] buf = new byte[1024 * 8]; zis = new ZipArchiveInputStream(is, charsetName, false); while ((entry = zis.getNextZipEntry()) != null) { name = entry.getName(); target = new File(destDir, name); if (entry.isDirectory()) { target.mkdirs(); /* does it always work? */ } else { target.createNewFile(); bos = new BufferedOutputStream(new FileOutputStream(target)); while ((nWritten = zis.read(buf)) >= 0) { bos.write(buf, 0, nWritten); } bos.close(); } } zis.close(); }
From source file:org.sourcepit.tools.shared.resources.internal.harness.SharedResourcesUtils.java
private static void importArchive(ClassLoader classLoader, String archivePath, String archiveEntry, String dirName, String encoding, File targetDir, boolean keepArchivePaths, IFilteredCopier copier, IFilterStrategy strategy) throws FileNotFoundException, IOException { final InputStream in = classLoader.getResourceAsStream(archivePath); if (in == null) { throw new FileNotFoundException(archivePath); }//from w w w . j a va 2s . c o m final String _dirName = !keepArchivePaths ? "" : dirName; final File outDir = new File(targetDir, _dirName); if (!outDir.exists()) { outDir.mkdirs(); } final ZipArchiveInputStream zipIn = new ZipArchiveInputStream(in, encoding, true); try { importArchive(zipIn, archiveEntry, outDir, keepArchivePaths, encoding, copier, strategy); } finally { IOUtils.closeQuietly(zipIn); } }
From source file:org.squashtest.tm.service.internal.archive.ZipReader.java
private void doSetStream(InputStream stream) { zipStream = new ZipArchiveInputStream(stream, encoding, false); }
From source file:org.xwiki.filter.xar.internal.input.WikiReader.java
public void read(InputStream stream, Object filter, XARInputFilter proxyFilter) throws IOException { ZipArchiveInputStream zis = new ZipArchiveInputStream(stream, "UTF-8", false); for (ZipArchiveEntry entry = zis.getNextZipEntry(); entry != null; entry = zis.getNextZipEntry()) { if (entry.isDirectory() || entry.getName().startsWith("META-INF")) { // The entry is either a directory or is something inside of the META-INF dir. // (we use that directory to put meta data such as LICENSE/NOTICE files.) continue; } else if (entry.getName().equals(XarModel.PATH_PACKAGE)) { // The entry is the manifest (package.xml). Read this differently. try { this.xarPackage.readDescriptor(zis); } catch (Exception e) { if (this.properties.isVerbose()) { this.logger.warn(LOG_DESCRIPTOR_FAILREAD, "Failed to read XAR descriptor from entry [{}]: {}", entry.getName(), ExceptionUtils.getRootCauseMessage(e)); }/*www . ja v a 2 s . c o m*/ } } else { try { this.documentReader.read(zis, filter, proxyFilter); } catch (SkipEntityException skip) { if (this.properties.isVerbose()) { this.logger.info(LOG_DOCUMENT_SKIPPED, "Skipped document [{}]", skip.getEntityReference()); } } catch (Exception e) { if (this.properties.isVerbose()) { this.logger.warn(LOG_DOCUMENT_FAILREAD, "Failed to read XAR XML document from entry [{}]: {}", entry.getName(), ExceptionUtils.getRootCauseMessage(e), e); } } } } }
From source file:org.xwiki.wikistream.xar.internal.input.WikiReader.java
public void read(InputStream stream, Object filter, XARInputFilter proxyFilter) throws XMLStreamException, IOException, WikiStreamException { ZipArchiveInputStream zis = new ZipArchiveInputStream(stream, "UTF-8", false); for (ZipArchiveEntry entry = zis.getNextZipEntry(); entry != null; entry = zis.getNextZipEntry()) { if (entry.isDirectory() || entry.getName().startsWith("META-INF")) { // The entry is either a directory or is something inside of the META-INF dir. // (we use that directory to put meta data such as LICENSE/NOTICE files.) continue; } else if (entry.getName().equals(XarModel.PATH_PACKAGE)) { // The entry is the manifest (package.xml). Read this differently. try { this.xarPackage.readDescriptor(zis); } catch (Exception e) { if (this.properties.isVerbose()) { this.logger.warn(LOG_DESCRIPTOR_FAILREAD, "Failed to read XAR descriptor from entry [{}]: {}", entry.getName(), ExceptionUtils.getRootCauseMessage(e)); }//from w w w . java2s.com } } else { try { this.documentReader.read(zis, filter, proxyFilter); } catch (SkipEntityException skip) { if (this.properties.isVerbose()) { this.logger.info(LOG_DOCUMENT_SKIPPED, "Skipped document [{}]", skip.getEntityReference()); } } catch (Exception e) { if (this.properties.isVerbose()) { this.logger.warn(LOG_DOCUMENT_FAILREAD, "Failed to read XAR XML document from entry [{}]: {}", entry.getName(), ExceptionUtils.getRootCauseMessage(e)); } } } } }