List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory
ArchiveStreamFactory
From source file:consulo.cold.runner.execute.target.artifacts.Generator.java
public void buildDistributionInArchive(String distZip, @Nullable String jdkArchivePath, String path, String archiveOutType) throws Exception { myListener.info("Build: " + path); ArchiveStreamFactory factory = new ArchiveStreamFactory(); final File fileZip = new File(myDistPath, distZip); final List<String> executables = Arrays.asList(ourExecutable); try (OutputStream pathStream = createOutputStream(archiveOutType, path)) { ArchiveOutputStream archiveOutputStream = factory.createArchiveOutputStream(archiveOutType, pathStream); if (archiveOutputStream instanceof TarArchiveOutputStream) { ((TarArchiveOutputStream) archiveOutputStream).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); }//from w w w.ja va 2 s.c om // move Consulo to archive, and change permissions try (InputStream is = new FileInputStream(fileZip)) { try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.ZIP, is)) { ArchiveEntry tempEntry = ais.getNextEntry(); while (tempEntry != null) { final ArchiveEntryWrapper newEntry = createEntry(archiveOutType, tempEntry.getName(), tempEntry); newEntry.setMode(extractMode(tempEntry)); newEntry.setTime(tempEntry.getLastModifiedDate().getTime()); if (executables.contains(tempEntry.getName())) { newEntry.setMode(0b111_101_101); } copyEntry(archiveOutputStream, ais, tempEntry, newEntry); tempEntry = ais.getNextEntry(); } } } boolean mac = distZip.contains("mac"); // jdk check if (jdkArchivePath != null) { try (InputStream is = new FileInputStream(jdkArchivePath)) { try (GzipCompressorInputStream gz = new GzipCompressorInputStream(is)) { try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.TAR, gz)) { ArchiveEntry tempEntry = ais.getNextEntry(); while (tempEntry != null) { final String name = tempEntry.getName(); // is our path if (!mac && name.startsWith("jre/")) { final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType, "Consulo/platform/buildSNAPSHOT/" + name, tempEntry); jdkEntry.setMode(extractMode(tempEntry)); jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime()); copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry); } else if (mac && name.startsWith("jdk")) { boolean needAddToArchive = true; for (String prefix : ourMacSkipJdkList) { if (name.startsWith(prefix)) { needAddToArchive = false; } } if (needAddToArchive) { final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType, "Consulo.app/Contents/platform/buildSNAPSHOT/jre/" + name, tempEntry); jdkEntry.setMode(extractMode(tempEntry)); jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime()); copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry); } } tempEntry = ais.getNextEntry(); } } } } } archiveOutputStream.finish(); } }
From source file:com.mirth.connect.util.ArchiveUtils.java
/** * Create an archive file from files/sub-folders in a given source folder. * //from w w w . j a v a 2s.c om * @param sourceFolder * Sub-folders and files inside this folder will be copied into the archive * @param destinationFile * The destination archive file * @param archiver * The archiver format, see * org.apache.commons.compress.archivers.ArchiveStreamFactory * @param compressor * The compressor format, see * org.apache.commons.compress.compressors.CompressorStreamFactory * @throws CompressException */ public static void createArchive(File sourceFolder, File destinationFile, String archiver, String compressor) throws CompressException { if (!sourceFolder.isDirectory() || !sourceFolder.exists()) { throw new CompressException("Invalid source folder: " + sourceFolder.getAbsolutePath()); } logger.debug("Creating archive \"" + destinationFile.getAbsolutePath() + "\" from folder \"" + sourceFolder.getAbsolutePath() + "\""); OutputStream outputStream = null; ArchiveOutputStream archiveOutputStream = null; try { /* * The commons-compress documentation recommends constructing a ZipArchiveOutputStream * with the archive file when using the ZIP archive format. See * http://commons.apache.org/proper/commons-compress/zip.html */ if (archiver.equals(ArchiveStreamFactory.ZIP) && compressor == null) { archiveOutputStream = new ZipArchiveOutputStream(destinationFile); } else { // if not using ZIP format, use the archiver/compressor stream factories to initialize the archive output stream outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile)); if (compressor != null) { outputStream = new CompressorStreamFactory().createCompressorOutputStream(compressor, outputStream); } archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(archiver, outputStream); } createFolderArchive(sourceFolder, archiveOutputStream, sourceFolder.getAbsolutePath() + IOUtils.DIR_SEPARATOR); } catch (Exception e) { throw new CompressException(e); } finally { IOUtils.closeQuietly(archiveOutputStream); IOUtils.closeQuietly(outputStream); logger.debug("Finished creating archive \"" + destinationFile.getAbsolutePath() + "\""); } }
From source file:com.synopsys.integration.util.CommonZipExpander.java
private void expandUnknownFile(File unknownFile, File targetExpansionDirectory) throws IOException, IntegrationException, ArchiveException { String format;/*from w ww. ja va2s . co m*/ // we need to use an InputStream where inputStream.markSupported() == true try (InputStream i = new BufferedInputStream(Files.newInputStream(unknownFile.toPath()))) { format = new ArchiveStreamFactory().detect(i); } beforeExpansion(unknownFile, targetExpansionDirectory); // in the case of zip files, commons-compress creates, but does not close, the ZipFile. To avoid this unclosed resource, we handle it ourselves. try { if (ArchiveStreamFactory.ZIP.equals(format)) { try (ZipFile zipFile = new ZipFile(unknownFile)) { expander.expand(zipFile, targetExpansionDirectory); } } else { expander.expand(unknownFile, targetExpansionDirectory); } } catch (IOException | ArchiveException e) { logger.error("Couldn't extract the archive file - check the file's permissions: " + e.getMessage()); throw e; } afterExpansion(unknownFile, targetExpansionDirectory); }
From source file:com.codemarvels.ant.aptrepotask.AptRepoTask.java
public void execute() { if (repoDir == null) { log("repoDir attribute is empty !", LogLevel.ERR.getLevel()); throw new RuntimeException("Bad attributes for apt-repo task"); }/*from w ww . j av a2 s. c o m*/ log("repo dir: " + repoDir); File repoFolder = new File(repoDir); if (!repoFolder.exists()) { repoFolder.mkdirs(); } File[] files = repoFolder.listFiles(new FileFilter() { public boolean accept(File pathname) { if (pathname.getName().endsWith(FILE_DEB_EXT)) { return true; } return false; } }); Packages packages = new Packages(); for (int i = 0; i < files.length; i++) { File file = files[i]; PackageEntry packageEntry = new PackageEntry(); packageEntry.setSize(file.length()); packageEntry.setSha1(Utils.getDigest("SHA-1", file)); packageEntry.setSha256(Utils.getDigest("SHA-256", file)); packageEntry.setMd5sum(Utils.getDigest("MD5", file)); String fileName = file.getName(); packageEntry.setFilename(fileName); log("found deb: " + fileName); try { ArchiveInputStream control_tgz; ArArchiveEntry entry; TarArchiveEntry control_entry; ArchiveInputStream debStream = new ArchiveStreamFactory().createArchiveInputStream("ar", new FileInputStream(file)); while ((entry = (ArArchiveEntry) debStream.getNextEntry()) != null) { if (entry.getName().equals("control.tar.gz")) { ControlHandler controlHandler = new ControlHandler(); GZIPInputStream gzipInputStream = new GZIPInputStream(debStream); control_tgz = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInputStream); while ((control_entry = (TarArchiveEntry) control_tgz.getNextEntry()) != null) { log("control entry: " + control_entry.getName(), LogLevel.DEBUG.getLevel()); if (control_entry.getName().trim().equals(CONTROL_FILE_NAME)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(control_tgz, outputStream); String content_string = outputStream.toString("UTF-8"); outputStream.close(); controlHandler.setControlContent(content_string); log("control cont: " + outputStream.toString("utf-8"), LogLevel.DEBUG.getLevel()); break; } } control_tgz.close(); if (controlHandler.hasControlContent()) { controlHandler.handle(packageEntry); } else { throw new RuntimeException("no control content found for: " + file.getName()); } break; } } debStream.close(); packages.addPackageEntry(packageEntry); } catch (Exception e) { String msg = FAILED_TO_CREATE_APT_REPO + " " + file.getName(); log(msg, e, LogLevel.ERR.getLevel()); throw new RuntimeException(msg, e); } } try { File packagesFile = new File(repoDir, PACKAGES_GZ); packagesWriter = new BufferedWriter( new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(packagesFile)))); packagesWriter.write(packages.toString()); DefaultHashes hashes = Utils.getDefaultDigests(packagesFile); ReleaseInfo pinfo = new ReleaseInfo(PACKAGES_GZ, packagesFile.length(), hashes); Release release = new Release(); release.addInfo(pinfo); final File releaseFile = new File(repoDir, RELEASE); FileUtils.fileWrite(releaseFile, release.toString()); } catch (IOException e) { throw new RuntimeException("writing files failed", e); } finally { if (packagesWriter != null) { try { packagesWriter.close(); } catch (IOException e) { throw new RuntimeException("writing files failed", e); } } } }
From source file:CASUAL.communicationstools.heimdall.odin.OdinFile.java
/** * Opens an Odin file and verifies MD5sum * * @param odinFile file to be opened and verified * @throws CorruptOdinFileException Odin checks did not pass * @throws FileNotFoundException {@inheritDoc} * @throws IOException {@inheritDoc}/* www.j a v a 2 s. com*/ * @throws NoSuchAlgorithmException {@inheritDoc} * @throws org.apache.commons.compress.archivers.ArchiveException * {@inheritDoc} */ public OdinFile(File odinFile) throws FileNotFoundException, IOException, NoSuchAlgorithmException, CorruptOdinFileException, ArchiveException { this.odinFile = odinFile; this.odinStream = new BufferedInputStream(new FileInputStream(odinFile)); String name = odinFile.getName(); if (name.endsWith("tar")) { actualMd5 = ""; type = 0; } else if (name.endsWith("tar.md5")) { actualMd5 = getActualAndExpectedOdinMd5(); if (!expectedMd5.equals(actualMd5)) { throw new CorruptOdinFileException(odinFile.getCanonicalPath()); } System.out.println("verified file " + odinFile.getCanonicalPath()); type = 1; } else if (name.endsWith("tar.gz.md5")) { actualMd5 = getActualAndExpectedOdinMd5(); if (!expectedMd5.equals(actualMd5)) { throw new CorruptOdinFileException(odinFile.getCanonicalPath()); } System.out.println("verified file " + odinFile.getCanonicalPath()); type = 2; } else {//(name.endsWith("tar.gz")) { actualMd5 = ""; type = 3; } //open a tar.gz stream for tar.gz and tar.md5.gz if (type == 2 || type == 3) { GZIPInputStream gzis = new GZIPInputStream(odinStream); tarStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", gzis); //open a tar stream for .tar and tar.md5 } else { tarStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", odinStream); } }
From source file:mj.ocraptor.extraction.tika.parser.pkg.ZipContainerDetector.java
private static MediaType detectArchiveFormat(byte[] prefix, int length) { try {/*from w w w. j a va 2s .c o m*/ ArchiveStreamFactory factory = new ArchiveStreamFactory(); ArchiveInputStream ais = factory.createArchiveInputStream(new ByteArrayInputStream(prefix, 0, length)); try { if ((ais instanceof TarArchiveInputStream) && !TarArchiveInputStream.matches(prefix, length)) { // ArchiveStreamFactory is too relaxed, see COMPRESS-117 return MediaType.OCTET_STREAM; } else { return PackageParser.getMediaType(ais); } } finally { IOUtils.closeQuietly(ais); } } catch (ArchiveException e) { return MediaType.OCTET_STREAM; } }
From source file:com.audiveris.installer.Expander.java
/** * Untar an input file into an output directory. * * @param inFile the input .tar file/*w w w .ja va2 s .c om*/ * @param outDir the output directory * @throws IOException * @throws FileNotFoundException * @throws ArchiveException * * @return The list of files with the untared content. */ public static List<File> unTar(final File inFile, final File outDir) throws FileNotFoundException, IOException, ArchiveException { logger.debug("Untaring {} to dir {}", inFile, outDir); assert inFile.getName().endsWith(TAR_EXT); final List<File> untaredFiles = new ArrayList<File>(); final InputStream is = new FileInputStream(inFile); final TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); ArchiveEntry entry; while ((entry = tis.getNextEntry()) != null) { final File outFile = new File(outDir, entry.getName()); if (entry.isDirectory()) { logger.debug("Attempting to write output dir {}", outFile); if (!outFile.exists()) { logger.debug("Attempting to create output dir {}", outFile); if (!outFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s", outFile.getAbsolutePath())); } } } else { logger.debug("Creating output file {}", outFile); streamToFile(tis, outFile); } untaredFiles.add(outFile); } tis.close(); return untaredFiles; }
From source file:cn.org.once.cstack.utils.FilesUtils.java
/** * Untar/*from www .j a va 2 s . c om*/ * * @throws IOException * @throws FileNotFoundException * * @return The {@link List} of {@link File}s with the untared content. * @throws ArchiveException */ public static void unTar(final InputStream is, final OutputStream outputFileStream) throws FileNotFoundException, IOException, ArchiveException { try { final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { logger.debug("Entry = " + entry.getName()); IOUtils.copy(debInputStream, outputFileStream); } } finally { is.close(); } }
From source file:com.google.cloud.public_datasets.nexrad2.GcsUntar.java
private static File[] unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException { // from//from w w w. ja v a 2s . co m // http://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java/7556307#7556307 log.info("tar xf " + inputFile + " to " + outputDir); final List<File> untaredFiles = new ArrayList<File>(); final InputStream is = new FileInputStream(inputFile); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); } untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles.toArray(new File[0]); }
From source file:com.netflix.spinnaker.halyard.backup.services.v1.BackupService.java
private void untarHalconfig(String halconfigDir, String halconfigTar) { FileInputStream tarInput = null; TarArchiveInputStream tarArchiveInputStream = null; try {// w w w. j a va2s . c om tarInput = new FileInputStream(new File(halconfigTar)); tarArchiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", tarInput); } catch (IOException | ArchiveException e) { throw new HalException(Problem.Severity.FATAL, "Failed to open backup: " + e.getMessage(), e); } try { ArchiveEntry archiveEntry = tarArchiveInputStream.getNextEntry(); while (archiveEntry != null) { String entryName = archiveEntry.getName(); Path outputPath = Paths.get(halconfigDir, entryName); File outputFile = outputPath.toFile(); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } if (archiveEntry.isDirectory()) { outputFile.mkdir(); } else { Files.copy(tarArchiveInputStream, outputPath, REPLACE_EXISTING); } archiveEntry = tarArchiveInputStream.getNextEntry(); } } catch (IOException e) { throw new HalException(Problem.Severity.FATAL, "Failed to read archive entry: " + e.getMessage(), e); } }