List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getName
public String getName()
From source file:org.codehaus.plexus.archiver.zip.ConcurrentJarCreator.java
/** * Adds an archive entry to this archive. * <p/>//from ww w.j av a2 s . c om * This method is expected to be called from a single client thread * * @param zipArchiveEntry The entry to add. Compression method * @param source The source input stream supplier * @throws java.io.IOException */ public void addArchiveEntry(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier source) throws IOException { final int method = zipArchiveEntry.getMethod(); if (method == -1) throw new IllegalArgumentException("Method must be set on the supplied zipArchiveEntry"); if (zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink()) { final ByteArrayInputStream payload = new ByteArrayInputStream(new byte[] {}); directories.addArchiveEntry( createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload))); payload.close(); } else if ("META-INF".equals(zipArchiveEntry.getName())) { InputStream payload = source.get(); if (zipArchiveEntry.isDirectory()) zipArchiveEntry.setMethod(ZipEntry.STORED); metaInfDir.addArchiveEntry( createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload))); payload.close(); } else if ("META-INF/MANIFEST.MF".equals(zipArchiveEntry.getName())) { InputStream payload = source.get(); if (zipArchiveEntry.isDirectory()) zipArchiveEntry.setMethod(ZipEntry.STORED); manifest.addArchiveEntry( createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload))); payload.close(); } else { parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, source); } }
From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java
private void createArchive(ZipArchiver archiver) throws ArchiverException, IOException { archiver.createArchive();//from w w w . j a v a 2 s.co m ZipFile zf = new ZipFile(archiver.getDestFile()); Enumeration e = zf.getEntries(); while (e.hasMoreElements()) { ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement(); if (ze.isDirectory()) { if (ze.getName().startsWith("worldwritable")) { fileModeAssert(0777, UnixStat.PERM_MASK & ze.getUnixMode()); } else if (ze.getName().startsWith("groupwritable")) { fileModeAssert(0070, UnixStat.PERM_MASK & ze.getUnixMode()); } else { fileModeAssert(0500, UnixStat.PERM_MASK & ze.getUnixMode()); } } else { if (ze.getName().equals("one.txt")) { fileModeAssert(0640, UnixStat.PERM_MASK & ze.getUnixMode()); } else if (ze.getName().equals("two.txt")) { fileModeAssert(0664, UnixStat.PERM_MASK & ze.getUnixMode()); } else if (ze.isUnixSymlink()) { // assertEquals( ze.getName(), 0500, UnixStat.PERM_MASK & ze.getUnixMode() ); } else { fileModeAssert(0400, UnixStat.PERM_MASK & ze.getUnixMode()); } } } }
From source file:org.codelibs.fess.crawler.extractor.impl.ZipExtractor.java
@Override public ExtractData getText(final InputStream in, final Map<String, String> params) { if (in == null) { throw new CrawlerSystemException("The inputstream is null."); }// w ww .ja va 2 s . com final MimeTypeHelper mimeTypeHelper = crawlerContainer.getComponent("mimeTypeHelper"); if (mimeTypeHelper == null) { throw new CrawlerSystemException("MimeTypeHelper is unavailable."); } final ExtractorFactory extractorFactory = crawlerContainer.getComponent("extractorFactory"); if (extractorFactory == null) { throw new CrawlerSystemException("ExtractorFactory is unavailable."); } final UnsafeStringBuilder buf = new UnsafeStringBuilder(1000); ArchiveInputStream ais = null; try { ais = archiveStreamFactory.createArchiveInputStream(in); ZipArchiveEntry entry = null; long contentSize = 0; while ((entry = (ZipArchiveEntry) ais.getNextEntry()) != null) { contentSize += entry.getSize(); if (maxContentSize != -1 && contentSize > maxContentSize) { throw new MaxLengthExceededException( "Extracted size is " + contentSize + " > " + maxContentSize); } final String filename = entry.getName(); final String mimeType = mimeTypeHelper.getContentType(null, filename); if (mimeType != null) { final Extractor extractor = extractorFactory.getExtractor(mimeType); if (extractor != null) { try { final Map<String, String> map = new HashMap<String, String>(); map.put(TikaMetadataKeys.RESOURCE_NAME_KEY, filename); buf.append(extractor.getText(new IgnoreCloseInputStream(ais), map).getContent()); buf.append('\n'); } catch (final Exception e) { if (logger.isDebugEnabled()) { logger.debug("Exception in an internal extractor.", e); } } } } } } catch (final MaxLengthExceededException e) { throw e; } catch (final Exception e) { if (buf.length() == 0) { throw new ExtractException("Could not extract a content.", e); } } finally { IOUtils.closeQuietly(ais); } return new ExtractData(buf.toUnsafeString().trim()); }
From source file:org.codelibs.robot.extractor.impl.ZipExtractor.java
@Override public ExtractData getText(final InputStream in, final Map<String, String> params) { if (in == null) { throw new RobotSystemException("The inputstream is null."); }/* ww w.j a va 2s.c om*/ final MimeTypeHelper mimeTypeHelper = robotContainer.getComponent("mimeTypeHelper"); if (mimeTypeHelper == null) { throw new RobotSystemException("MimeTypeHelper is unavailable."); } final ExtractorFactory extractorFactory = robotContainer.getComponent("extractorFactory"); if (extractorFactory == null) { throw new RobotSystemException("ExtractorFactory is unavailable."); } final StringBuilder buf = new StringBuilder(1000); ArchiveInputStream ais = null; try { ais = archiveStreamFactory.createArchiveInputStream(in); ZipArchiveEntry entry = null; while ((entry = (ZipArchiveEntry) ais.getNextEntry()) != null) { final String filename = entry.getName(); final String mimeType = mimeTypeHelper.getContentType(null, filename); if (mimeType != null) { final Extractor extractor = extractorFactory.getExtractor(mimeType); if (extractor != null) { try { final Map<String, String> map = new HashMap<String, String>(); map.put(TikaMetadataKeys.RESOURCE_NAME_KEY, filename); buf.append(extractor.getText(new IgnoreCloseInputStream(ais), map).getContent()); buf.append('\n'); } catch (final Exception e) { if (logger.isDebugEnabled()) { logger.debug("Exception in an internal extractor.", e); } } } } } } catch (final Exception e) { if (buf.length() == 0) { throw new ExtractException("Could not extract a content.", e); } } finally { IOUtils.closeQuietly(ais); } return new ExtractData(buf.toString()); }
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. j a v a 2 s . c o m 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.eclipse.cbi.maven.plugins.macsigner.SignMojo.java
/** * Decompresses zip files.// w w w. ja v a 2 s.co m * @param zipFile The zip file to decompress. * @throws IOException * @throws MojoExecutionException */ private static void unZip(File zipFile, File output_dir) throws IOException, MojoExecutionException { ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(zipFile)); ZipArchiveEntry ze; String name, parent; try { ze = zis.getNextZipEntry(); // check for at least one zip entry if (ze == null) { throw new MojoExecutionException("Could not decompress " + zipFile); } while (ze != null) { name = ze.getName(); //make directories if (ze.isDirectory()) { mkdirs(output_dir, name); } else { parent = getParentDirAbsolutePath(name); mkdirs(output_dir, parent); File outFile = new File(output_dir, name); outFile.createNewFile(); // check for match in executable list if (executableFiles.contains(name)) { Files.setPosixFilePermissions(outFile.toPath(), PosixFilePermissions.fromString("rwxr-x---")); } FileOutputStream fos = new FileOutputStream(outFile); copyInputStreamToOutputStream(zis, fos); fos.close(); } ze = zis.getNextZipEntry(); } } finally { zis.close(); } }
From source file:org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ZipLeveledStructureProvider.java
/** * Creates a new file zip entry with the specified name. * @param entry the entry to create the file for *//*from w ww .j a v a 2s. c o m*/ protected void createFile(ZipArchiveEntry entry) { IPath pathname = new Path(entry.getName()); ZipArchiveEntry parent; if (pathname.segmentCount() == 1) { parent = root; } else { parent = directoryEntryCache.get(pathname.removeLastSegments(1)); } @Nullable List<ZipArchiveEntry> childList = children.get(parent); NonNullUtils.checkNotNull(childList).add(entry); }
From source file:org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ZipLeveledStructureProvider.java
/** * Initializes this object's children table based on the contents of the * specified source file.// ww w. j a v a 2 s .c o m */ protected void initialize() { children = new HashMap<>(1000); children.put(root, new ArrayList<>()); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); IPath path = new Path(entry.getName()).addTrailingSeparator(); if (entry.isDirectory()) { createContainer(path); } else { // Ensure the container structure for all levels above this is initialized // Once we hit a higher-level container that's already added we need go no further int pathSegmentCount = path.segmentCount(); if (pathSegmentCount > 1) { createContainer(path.uptoSegment(pathSegmentCount - 1)); } createFile(entry); } } }
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 ww .jav a2 s. c om*/ 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; }/* www .ja v a2 s . 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); } } }