List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getInputStream
public InputStream getInputStream(ZipArchiveEntry ze) throws IOException, ZipException
From source file:org.beangle.ems.util.ZipUtils.java
/** * <p>//from w ww .j a va 2s.c om * unzip. * </p> * * @param zipFile a {@link java.io.File} object. * @param destination a {@link java.lang.String} object. * @param encoding a {@link java.lang.String} object. * @return a {@link java.util.List} object. */ public static List<String> unzip(final File zipFile, final String destination, String encoding) { List<String> fileNames = CollectUtils.newArrayList(); String dest = destination; if (!destination.endsWith(File.separator)) { dest = destination + File.separator; } ZipFile file; try { file = null; if (null == encoding) file = new ZipFile(zipFile); else file = new ZipFile(zipFile, encoding); Enumeration<ZipArchiveEntry> en = file.getEntries(); ZipArchiveEntry ze = null; while (en.hasMoreElements()) { ze = en.nextElement(); File f = new File(dest, ze.getName()); if (ze.isDirectory()) { f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); InputStream is = file.getInputStream(ze); OutputStream os = new FileOutputStream(f); IOs.copy(is, os); is.close(); os.close(); fileNames.add(f.getAbsolutePath()); } } file.close(); } catch (IOException e) { e.printStackTrace(); } return fileNames; }
From source file:org.codehaus.mojo.unix.maven.zip.ZipPackageTest.java
private void assertFile(ZipFile file, ZipArchiveEntry entry, String name, int size, LocalDateTime time, String content, UnixFileMode mode) throws IOException { InputStream in = file.getInputStream(entry); assertFalse(name + " should be file", entry.isDirectory()); assertEquals(name + ", name", name, entry.getName()); assertEquals(name + ", timestamp", time, new LocalDateTime(entry.getTime())); // wtf: http://vimalathithen.blogspot.no/2006/06/using-zipentrygetsize.html // assertEquals( name + ", size", size, entry.getSize() ); byte[] bytes = new byte[1000]; assertEquals(size, in.read(bytes, 0, bytes.length)); assertEquals(content, new String(bytes, 0, size, charset)); assertEquals(ZipArchiveEntry.PLATFORM_UNIX, entry.getPlatform()); assertEquals(name + ", mode", mode.toString(), UnixFileMode.fromInt(entry.getUnixMode()).toString()); }
From source file:org.codehaus.plexus.archiver.jar.IndexTest.java
public void testCreateArchiveWithIndexedJars() throws Exception { /* create a dummy jar */ JarArchiver archiver1 = (JarArchiver) lookup(Archiver.ROLE, "jar"); archiver1.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt"); archiver1.setDestFile(getTestFile("target/output/archive1.jar")); archiver1.createArchive();//from w w w .j ava 2 s. c o m /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first jar. */ Manifest m = new Manifest(); Manifest.Attribute classpathAttr = new Manifest.Attribute("Class-Path", "archive1.jar"); m.addConfiguredAttribute(classpathAttr); JarArchiver archiver2 = (JarArchiver) lookup(Archiver.ROLE, "jar"); archiver2.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt"); archiver2.setIndex(true); archiver2.addConfiguredIndexJars(archiver1.getDestFile()); archiver2.setDestFile(getTestFile("target/output/archive2.jar")); archiver2.addConfiguredManifest(m); archiver2.createArchive(); // read the index file back and check it looks like it ought to org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile( archiver2.getDestFile()); ZipArchiveEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST"); assertNotNull(indexEntry); InputStream bis = bufferedInputStream(zf.getInputStream(indexEntry)); byte buf[] = new byte[1024]; int i = bis.read(buf); String res = new String(buf, 0, i); assertEquals("JarIndex-Version: 1.0\n\narchive2.jar\ntwo.txt\n\narchive1.jar\none.txt\n\n", res.replaceAll("\r\n", "\n")); }
From source file:org.codehaus.plexus.archiver.jar.IndexTest.java
/** * this is pretty much a duplicate of testCreateArchiveWithIndexedJars(), but adds some extra * tests for files in META-INF// w ww .j av a 2 s .c o m */ public void testCreateArchiveWithIndexedJarsAndMetaInf() throws Exception { /* create a dummy jar */ JarArchiver archiver1 = (JarArchiver) lookup(Archiver.ROLE, "jar"); archiver1.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt"); // add a file in the META-INF directory, as this previously didn't make it into the index archiver1.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "META-INF/foo"); archiver1.setDestFile(getTestFile("target/output/archive1.jar")); archiver1.createArchive(); /* create another dummy jar, with an index but nothing else in META-INF. Also checks non-leaf files. */ JarArchiver archiver3 = (JarArchiver) lookup(Archiver.ROLE, "jar"); archiver3.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "org/apache/maven/one.txt"); archiver3.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "META-INF/INDEX.LIST"); archiver3.setDestFile(getTestFile("target/output/archive3.jar")); archiver3.createArchive(); /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first two jars. */ Manifest m = new Manifest(); Manifest.Attribute classpathAttr = new Manifest.Attribute("Class-Path", "archive1.jar archive3.jar"); m.addConfiguredAttribute(classpathAttr); JarArchiver archiver2 = (JarArchiver) lookup(Archiver.ROLE, "jar"); archiver2.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt"); archiver2.setIndex(true); archiver2.addConfiguredIndexJars(archiver1.getDestFile()); archiver2.addConfiguredIndexJars(archiver3.getDestFile()); archiver2.setDestFile(getTestFile("target/output/archive2.jar")); archiver2.addConfiguredManifest(m); archiver2.createArchive(); // read the index file back and check it looks like it ought to org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile( archiver2.getDestFile()); ZipArchiveEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST"); assertNotNull(indexEntry); InputStream bis = bufferedInputStream(zf.getInputStream(indexEntry)); byte buf[] = new byte[1024]; int i = bis.read(buf); String res = new String(buf, 0, i); //System.out.println(res); assertEquals("JarIndex-Version: 1.0\n\n" + "archive2.jar\ntwo.txt\n\n" + "archive1.jar\nMETA-INF\none.txt\n\n" + "archive3.jar\norg\norg/apache\norg/apache/maven\n\n", res.replaceAll("\r\n", "\n")); }
From source file:org.codehaus.plexus.archiver.zip.AbstractZipUnArchiver.java
protected void execute() throws ArchiverException { getLogger().debug("Expanding: " + getSourceFile() + " into " + getDestDirectory()); org.apache.commons.compress.archivers.zip.ZipFile zf = null; try {// w ww .ja va 2s . c om zf = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding); final Enumeration e = zf.getEntries(); while (e.hasMoreElements()) { final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement(); final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zf, ze); if (isSelected(ze.getName(), fileInfo)) { InputStream in = zf.getInputStream(ze); extractFileIfIncluded(getSourceFile(), getDestDirectory(), in, ze.getName(), new Date(ze.getTime()), ze.isDirectory(), ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zf, ze)); IOUtil.close(in); } } getLogger().debug("expand complete"); } catch (final IOException ioe) { throw new ArchiverException("Error while expanding " + getSourceFile().getAbsolutePath(), ioe); } finally { IOUtils.closeQuietly(zf); } }
From source file:org.codehaus.plexus.archiver.zip.AbstractZipUnArchiver.java
protected void execute(final String path, final File outputDirectory) throws ArchiverException { org.apache.commons.compress.archivers.zip.ZipFile zipFile = null; try {//from w w w. j a va 2 s . c o m zipFile = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding); final Enumeration e = zipFile.getEntries(); while (e.hasMoreElements()) { final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement(); final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zipFile, ze); if (!isSelected(ze.getName(), fileInfo)) { continue; } if (ze.getName().startsWith(path)) { final InputStream inputStream = zipFile.getInputStream(ze); extractFileIfIncluded(getSourceFile(), outputDirectory, inputStream, ze.getName(), new Date(ze.getTime()), ze.isDirectory(), ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zipFile, ze)); IOUtil.close(inputStream); } } } catch (final IOException ioe) { throw new ArchiverException("Error while expanding " + getSourceFile().getAbsolutePath(), ioe); } finally { IOUtils.closeQuietly(zipFile); } }
From source file:org.cytobank.acs.core.ACS.java
/** * Writes out a file contained within this <code>ACS</code> instance to the * specified <code>OutputStream</code>. * /*ww w . ja va 2s.c o m*/ * @param filePath * the <code>String</code> path representing a file within this * <code>ACS</code> instance. * @param outputStream * to write the requested file to * @throws IOException * If an input or output exception occurred */ public void extractFile(String filePath, OutputStream outputStream) throws IOException { ZipFile zipFile = new ZipFile(acsFile); if (filePath.startsWith("/")) filePath = filePath.substring(1); InputStream inputStream = null; ZipArchiveEntry entry = zipFile.getEntry(filePath); try { if (entry == null) throw new IOException("Could not find zip file entry " + filePath); inputStream = zipFile.getInputStream(entry); IOUtils.copy(inputStream, outputStream); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException ignore) { } try { zipFile.close(); } catch (IOException ignore) { } } }
From source file:org.everit.osgi.dev.maven.util.FileManager.java
public final void unpackZipFile(final File file, final File destinationDirectory) throws IOException { destinationDirectory.mkdirs();//from w w w .jav a 2s .com ZipFile zipFile = new ZipFile(file); try { Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); String name = entry.getName(); File destFile = new File(destinationDirectory, name); if (entry.isDirectory()) { destFile.mkdirs(); } else { File parentFolder = destFile.getParentFile(); parentFolder.mkdirs(); InputStream inputStream = zipFile.getInputStream(entry); overCopyFile(inputStream, destFile); FileManager.setPermissionsOnFile(destFile, entry); } } } finally { zipFile.close(); } }
From source file:org.fabrician.maven.plugins.CompressUtils.java
public static void copyZipToArchiveOutputStream(File zipSrc, FilenamePatternFilter filter, ArchiveOutputStream out, String alternateBaseDir) throws IOException { ZipFile zip = new ZipFile(zipSrc); for (Enumeration<ZipArchiveEntry> zipEnum = zip.getEntries(); zipEnum.hasMoreElements();) { ZipArchiveEntry source = zipEnum.nextElement(); if (filter != null && !filter.accept(source.getName())) { System.out.println("Excluding " + source.getName()); continue; }/*from w w w. j av a 2s. c o m*/ InputStream in = null; try { in = zip.getInputStream(source); out.putArchiveEntry(createArchiveEntry(source, out, alternateBaseDir)); IOUtils.copy(in, out); out.closeArchiveEntry(); } finally { close(in); } } }
From source file:org.interreg.docexplore.util.ZipUtils.java
public static void unzip(File zipfile, File directory, float[] progress, float progressOffset, float progressAmount, boolean overwrite) throws IOException { org.apache.commons.compress.archivers.zip.ZipFile zfile = new org.apache.commons.compress.archivers.zip.ZipFile( zipfile);//from w w w .j a va2 s .co m int nEntries = 0; Enumeration<ZipArchiveEntry> entries = zfile.getEntries(); while (entries.hasMoreElements()) if (!entries.nextElement().isDirectory()) nEntries++; int cnt = 0; entries = zfile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); File file = new File(directory, entry.getName()); if (entry.isDirectory()) file.mkdirs(); else { if (!file.exists() || overwrite) { file.getParentFile().mkdirs(); InputStream in = zfile.getInputStream(entry); try { copy(in, file); } finally { in.close(); } } cnt++; if (progress != null) progress[0] = progressOffset + cnt * progressAmount / nEntries; } } zfile.close(); }