List of usage examples for org.apache.commons.compress.archivers ArchiveEntry getName
public String getName();
From source file:com.asakusafw.shafu.core.util.IoUtils.java
private static void createDirectory(File base, ArchiveEntry entry) throws IOException { File file = new File(base, entry.getName()); if (file.mkdirs() == false && file.isDirectory() == false) { throw new IOException(MessageFormat.format(Messages.IoUtils_errorFailedToCreateDirectory, base)); }//from ww w. j a v a2 s.co m }
From source file:com.asakusafw.shafu.core.util.IoUtils.java
private static File createFile(File base, ArchiveEntry entry, InputStream contents) throws IOException { File file = new File(base, entry.getName()); File parent = file.getParentFile(); parent.mkdirs();/*from w ww . j a va 2 s.c om*/ OutputStream output = new FileOutputStream(file); try { IOUtils.copy(contents, output); } finally { output.close(); } return file; }
From source file:ezbake.deployer.utilities.ArtifactHelpers.java
/** * Append to the given ArchiveInputStream writing to the given outputstream, the given entries to add. * This will duplicate the InputStream to the Output. * * @param inputStream - archive input to append to * @param output - what to copy the modified archive to * @param filesToAdd - what entries to append. *//*w w w .ja va 2 s. com*/ private static void appendFilesInTarArchive(ArchiveInputStream inputStream, OutputStream output, Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException { ArchiveStreamFactory asf = new ArchiveStreamFactory(); try { HashMap<String, ArtifactDataEntry> newFiles = new HashMap<>(); for (ArtifactDataEntry entry : filesToAdd) { newFiles.put(entry.getEntry().getName(), entry); } GZIPOutputStream gzs = new GZIPOutputStream(output); TarArchiveOutputStream aos = (TarArchiveOutputStream) asf .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs); aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = inputStream.getNextEntry()) != null) { //If we're passing in the same file, don't copy into the new archive if (!newFiles.containsKey(nextEntry.getName())) { aos.putArchiveEntry(nextEntry); IOUtils.copy(inputStream, aos); aos.closeArchiveEntry(); } } for (ArtifactDataEntry entry : filesToAdd) { aos.putArchiveEntry(entry.getEntry()); IOUtils.write(entry.getData(), aos); aos.closeArchiveEntry(); } aos.finish(); gzs.finish(); } catch (ArchiveException | IOException e) { log.error(e.getMessage(), e); throw new DeploymentException(e.getMessage()); } }
From source file:io.github.retz.executor.FileManager.java
private static boolean needsDecompression(File file, String dir) throws IOException, ArchiveException { // To use autodetect feature of ArchiveStreamFactory, input stream must be wrapped // with BufferedInputStream. From commons-compress mailing list. LOG.debug("loading file .... {} as {}", file, file.getPath()); boolean needsDecompression = false; try (ArchiveInputStream ain = createAIS(file)) { while (ain != null) { ArchiveEntry entry = ain.getNextEntry(); if (entry == null) break; // LOG.debug("name: {} size:{} dir: {}", entry.getName(), entry.getSize(), entry.isDirectory()); File f = new File(FilenameUtils.concat(dir, entry.getName())); // LOG.debug("{} exists: {}, size:{}, dir:{}", f, f.exists(), f.length(), f.isDirectory()); if (f.isDirectory()) { continue; } else if (!f.exists() || entry.getSize() != f.length()) { LOG.info("File {} differs: seems not decompressed before", f); needsDecompression = true; break; }/*w ww. j a va 2 s . c om*/ } } catch (ArchiveException e) { needsDecompression = true; } return needsDecompression; }
From source file:com.silverpeas.util.ZipManager.java
/** * Extract the content of an archive into a directory. * * @param source the archive.//w ww . jav a 2s. c o m * @param dest the destination directory. * @throws UtilException */ /*public static void extract(File source, File dest) throws UtilException { if (source == null) { throw new UtilException("Expand.execute()", SilverpeasException.ERROR, "util.EXE_SOURCE_FILE_ATTRIBUTE_MUST_BE_SPECIFIED"); } if (dest == null) { throw new UtilException("Expand.execute()", SilverpeasException.ERROR, "util.EXE_DESTINATION_FILE_ATTRIBUTE_MUST_BE_SPECIFIED"); } ZipFile zf = null; try { zf = new ZipFile(source); @SuppressWarnings("unchecked") Enumeration<ZipArchiveEntry> entries = (Enumeration<ZipArchiveEntry>) zf.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry ze = entries.nextElement(); File currentFile = new File(dest, ze.getName()); try { currentFile.getParentFile().mkdirs(); if (ze.isDirectory()) { currentFile.mkdirs(); } else { currentFile.getParentFile().mkdirs(); InputStream zis = zf.getInputStream(ze); FileOutputStream fos = new FileOutputStream(currentFile); IOUtils.copy(zis, fos); IOUtils.closeQuietly(zis); IOUtils.closeQuietly(fos); } } catch (FileNotFoundException ex) { SilverTrace.warn("util", "ZipManager.extractFile()", "root.EX_FILE_NOT_FOUND", "file = " + currentFile.getPath(), ex); } } } catch (IOException ioe) { SilverTrace.warn("util", "ZipManager.extractFile()", "util.EXE_ERROR_WHILE_EXTRACTING_FILE", "sourceFile = " + source.getPath(), ioe); } finally { if (zf != null) { ZipFile.closeQuietly(zf); } } } */ public static void extract(File source, File dest) throws UtilException { if (source == null) { throw new UtilException("Expand.execute()", SilverpeasException.ERROR, "util.EXE_SOURCE_FILE_ATTRIBUTE_MUST_BE_SPECIFIED"); } if (dest == null) { throw new UtilException("Expand.execute()", SilverpeasException.ERROR, "util.EXE_DESTINATION_FILE_ATTRIBUTE_MUST_BE_SPECIFIED"); } InputStream in = null; ArchiveInputStream archiveStream = null; try { in = new BufferedInputStream(new FileInputStream(source)); try { archiveStream = new ArchiveStreamFactory().createArchiveInputStream(in); } catch (ArchiveException aex) { if (FilenameUtils.getExtension(source.getName()).toLowerCase().endsWith("gz")) { archiveStream = new TarArchiveInputStream(new GzipCompressorInputStream(in)); } else { archiveStream = new TarArchiveInputStream(new BZip2CompressorInputStream(in)); } } ArchiveEntry archiveEntry; while ((archiveEntry = archiveStream.getNextEntry()) != null) { File currentFile = new File(dest, archiveEntry.getName()); try { currentFile.getParentFile().mkdirs(); if (archiveEntry.isDirectory()) { currentFile.mkdirs(); } else { currentFile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(currentFile); IOUtils.copy(archiveStream, fos); IOUtils.closeQuietly(fos); } } catch (FileNotFoundException ex) { SilverTrace.warn("util", "ZipManager.extractFile()", "root.EX_FILE_NOT_FOUND", "file = " + currentFile.getPath(), ex); } } } catch (IOException ioe) { SilverTrace.warn("util", "ZipManager.extractFile()", "util.EXE_ERROR_WHILE_EXTRACTING_FILE", "sourceFile = " + source.getPath(), ioe); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(archiveStream); } }
From source file:com.igormaznitsa.mvngolang.utils.UnpackUtils.java
public static int unpackFileToFolder(@Nonnull final Log logger, @Nullable final String folder, @Nonnull final File archiveFile, @Nonnull final File destinationFolder, final boolean makeAllExecutable) throws IOException { final String normalizedName = archiveFile.getName().toLowerCase(Locale.ENGLISH); final ArchEntryGetter entryGetter; boolean modeZipFile = false; final ZipFile theZipFile; final ArchiveInputStream archInputStream; if (normalizedName.endsWith(".zip")) { logger.debug("Detected ZIP archive"); modeZipFile = true;/*from w w w .j a v a 2 s .c o m*/ theZipFile = new ZipFile(archiveFile); archInputStream = null; entryGetter = new ArchEntryGetter() { private final Enumeration<ZipArchiveEntry> iterator = theZipFile.getEntries(); @Override @Nullable public ArchiveEntry getNextEntry() throws IOException { ArchiveEntry result = null; if (this.iterator.hasMoreElements()) { result = this.iterator.nextElement(); } return result; } }; } else { theZipFile = null; final InputStream in = new BufferedInputStream(new FileInputStream(archiveFile)); try { if (normalizedName.endsWith(".tar.gz")) { logger.debug("Detected TAR.GZ archive"); archInputStream = new TarArchiveInputStream(new GZIPInputStream(in)); entryGetter = new ArchEntryGetter() { @Override @Nullable public ArchiveEntry getNextEntry() throws IOException { return ((TarArchiveInputStream) archInputStream).getNextTarEntry(); } }; } else { logger.debug("Detected OTHER archive"); archInputStream = ARCHIVE_STREAM_FACTORY.createArchiveInputStream(in); logger.debug("Created archive stream : " + archInputStream.getClass().getName()); entryGetter = new ArchEntryGetter() { @Override @Nullable public ArchiveEntry getNextEntry() throws IOException { return archInputStream.getNextEntry(); } }; } } catch (ArchiveException ex) { IOUtils.closeQuietly(in); throw new IOException("Can't recognize or read archive file : " + archiveFile, ex); } catch (CantReadArchiveEntryException ex) { IOUtils.closeQuietly(in); throw new IOException("Can't read entry from archive file : " + archiveFile, ex); } } try { final String normalizedFolder = folder == null ? null : FilenameUtils.normalize(folder, true) + '/'; int unpackedFilesCounter = 0; while (true) { final ArchiveEntry entry = entryGetter.getNextEntry(); if (entry == null) { break; } final String normalizedPath = FilenameUtils.normalize(entry.getName(), true); logger.debug("Detected archive entry : " + normalizedPath); if (normalizedFolder == null || normalizedPath.startsWith(normalizedFolder)) { final File targetFile = new File(destinationFolder, normalizedFolder == null ? normalizedPath : normalizedPath.substring(normalizedFolder.length())); if (entry.isDirectory()) { logger.debug("Folder : " + normalizedPath); if (!targetFile.exists() && !targetFile.mkdirs()) { throw new IOException("Can't create folder " + targetFile); } } else { final File parent = targetFile.getParentFile(); if (parent != null && !parent.isDirectory() && !parent.mkdirs()) { throw new IOException("Can't create folder : " + parent); } final FileOutputStream fos = new FileOutputStream(targetFile); try { if (modeZipFile) { logger.debug("Unpacking ZIP entry : " + normalizedPath); final InputStream zipEntryInStream = theZipFile .getInputStream((ZipArchiveEntry) entry); try { if (IOUtils.copy(zipEntryInStream, fos) != entry.getSize()) { throw new IOException( "Can't unpack file, illegal unpacked length : " + entry.getName()); } } finally { IOUtils.closeQuietly(zipEntryInStream); } } else { logger.debug("Unpacking archive entry : " + normalizedPath); if (!archInputStream.canReadEntryData(entry)) { throw new IOException("Can't read archive entry data : " + normalizedPath); } if (IOUtils.copy(archInputStream, fos) != entry.getSize()) { throw new IOException( "Can't unpack file, illegal unpacked length : " + entry.getName()); } } } finally { fos.close(); } if (makeAllExecutable) { try { targetFile.setExecutable(true, true); } catch (SecurityException ex) { throw new IOException("Can't make file executable : " + targetFile, ex); } } unpackedFilesCounter++; } } else { logger.debug("Archive entry " + normalizedPath + " ignored"); } } return unpackedFilesCounter; } finally { IOUtils.closeQuietly(theZipFile); IOUtils.closeQuietly(archInputStream); } }
From source file:com.geewhiz.pacify.utils.ArchiveUtils.java
private static Boolean archiveContainsFile(File archive, String archiveType, String fileToLookFor) { ArchiveInputStream ais = null;//from ww w. ja v a2 s . c om try { ArchiveStreamFactory factory = new ArchiveStreamFactory(); ais = factory.createArchiveInputStream(archiveType, new FileInputStream(archive)); ArchiveEntry entry; while ((entry = ais.getNextEntry()) != null) { if (fileToLookFor.equals(entry.getName())) { return Boolean.TRUE; } } return Boolean.FALSE; } catch (ArchiveException e) { throw new RuntimeException(e); } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(ais); } }
From source file:backup.integration.MiniClusterTestBase.java
private static File extract(File output, File parcelLocation) throws Exception { try (TarArchiveInputStream tarInput = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(parcelLocation)))) { ArchiveEntry entry; while ((entry = (ArchiveEntry) tarInput.getNextEntry()) != null) { LOG.info("Extracting: {}", entry.getName()); File f = new File(output, entry.getName()); if (entry.isDirectory()) { f.mkdirs();/*from w w w . j a v a 2 s. c o m*/ } else { f.getParentFile().mkdirs(); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(f))) { IOUtils.copy(tarInput, out); } } } } return output; }
From source file:com.geewhiz.pacify.utils.ArchiveUtils.java
private static Map<String, File> extractFiles(File archive, String archiveType, String searchFor, Boolean isRegExp) {/* www . j a v a 2s. c o m*/ Map<String, File> result = new HashMap<String, File>(); ArchiveInputStream ais = null; try { ArchiveStreamFactory factory = new ArchiveStreamFactory(); ais = factory.createArchiveInputStream(archiveType, new FileInputStream(archive)); ArchiveEntry entry; while ((entry = ais.getNextEntry()) != null) { if (isRegExp) { if (!matches(entry.getName(), searchFor)) { continue; } } else if (!searchFor.equals(entry.getName())) { continue; } File physicalFile = FileUtils.createEmptyFileWithSamePermissions(archive, archive.getName() + "!" + Paths.get(entry.getName()).getFileName().toString() + "_"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(physicalFile)); byte[] content = new byte[2048]; int len; while ((len = ais.read(content)) != -1) { bos.write(content, 0, len); } bos.close(); content = null; result.put(entry.getName(), physicalFile); } } catch (ArchiveException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(ais); } return result; }
From source file:com.mirth.connect.util.ArchiveUtils.java
/** * Extracts an archive using generic stream factories provided by commons-compress. *//*from w ww . j av a 2 s . c o m*/ private static void extractGenericArchive(File archiveFile, File destinationFolder) throws CompressException { try { InputStream inputStream = new BufferedInputStream(FileUtils.openInputStream(archiveFile)); try { inputStream = new CompressorStreamFactory().createCompressorInputStream(inputStream); } catch (CompressorException e) { // a compressor was not recognized in the stream, in this case we leave the inputStream as-is } ArchiveInputStream archiveInputStream = new ArchiveStreamFactory() .createArchiveInputStream(inputStream); ArchiveEntry entry; int inputOffset = 0; byte[] buffer = new byte[BUFFER_SIZE]; try { while (null != (entry = archiveInputStream.getNextEntry())) { File outputFile = new File( destinationFolder.getAbsolutePath() + IOUtils.DIR_SEPARATOR + entry.getName()); if (entry.isDirectory()) { FileUtils.forceMkdir(outputFile); } else { FileOutputStream outputStream = null; try { outputStream = FileUtils.openOutputStream(outputFile); int bytesRead; int outputOffset = 0; while ((bytesRead = archiveInputStream.read(buffer, inputOffset, BUFFER_SIZE)) > 0) { outputStream.write(buffer, outputOffset, bytesRead); inputOffset += bytesRead; outputOffset += bytesRead; } } finally { IOUtils.closeQuietly(outputStream); } } } } finally { IOUtils.closeQuietly(archiveInputStream); } } catch (Exception e) { throw new CompressException(e); } }