List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream setLongFileMode
public void setLongFileMode(int longFileMode)
From source file:org.apache.camel.processor.aggregate.tarfile.TarAggregationStrategy.java
private static void addEntryToTar(File source, String entryName, byte[] buffer, int length) throws IOException, ArchiveException { File tmpTar = File.createTempFile(source.getName(), null); tmpTar.delete();//from ww w . j a va 2s . co m if (!source.renameTo(tmpTar)) { throw new IOException("Cannot create temp file: " + source.getName()); } TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new FileInputStream(tmpTar)); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = tin.getNextEntry()) != null) { tos.putArchiveEntry(nextEntry); IOUtils.copy(tin, tos); tos.closeArchiveEntry(); } // Create new entry TarArchiveEntry entry = new TarArchiveEntry(entryName); entry.setSize(length); tos.putArchiveEntry(entry); tos.write(buffer, 0, length); tos.closeArchiveEntry(); IOHelper.close(tin); IOHelper.close(tos); }
From source file:org.apache.hadoop.hive.common.CompressionUtils.java
/** * Archive all the files in the inputFiles into outputFile * * @param inputFiles/*from www. j a v a 2s. c om*/ * @param outputFile * @throws IOException */ public static void tar(String parentDir, String[] inputFiles, String outputFile) throws IOException { FileOutputStream out = null; try { out = new FileOutputStream(new File(parentDir, outputFile)); TarArchiveOutputStream tOut = new TarArchiveOutputStream( new GzipCompressorOutputStream(new BufferedOutputStream(out))); for (int i = 0; i < inputFiles.length; i++) { File f = new File(parentDir, inputFiles[i]); TarArchiveEntry tarEntry = new TarArchiveEntry(f, f.getName()); tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tOut.putArchiveEntry(tarEntry); FileInputStream input = new FileInputStream(f); try { IOUtils.copy(input, tOut); // copy with 8K buffer, not close } finally { input.close(); } tOut.closeArchiveEntry(); } tOut.close(); // finishes inside } finally { // TarArchiveOutputStream seemed not to close files properly in error situation org.apache.hadoop.io.IOUtils.closeStream(out); } }
From source file:org.apache.whirr.util.Tarball.java
private static void addFile(TarArchiveOutputStream tarOutputStream, String path, String base) throws IOException { File file = new File(path); String entryName = base + file.getName(); TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName); tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tarOutputStream.putArchiveEntry(tarEntry); if (file.isFile()) { IOUtils.copy(new FileInputStream(file), tarOutputStream); tarOutputStream.closeArchiveEntry(); } else {// w w w . j ava2 s. com tarOutputStream.closeArchiveEntry(); File[] children = file.listFiles(); if (children != null) { for (File child : children) { addFile(tarOutputStream, child.getAbsolutePath(), entryName + "/"); } } } }
From source file:org.codehaus.plexus.archiver.tar.TarRoundTripTest.java
/** * test round-tripping long (GNU) entries *//*w ww. jav a2 s.c o m*/ public void testLongRoundTripping() throws IOException { TarArchiveEntry original = new TarArchiveEntry(LONG_NAME); assertEquals("over 100 chars", true, LONG_NAME.length() > 100); assertEquals("original name", LONG_NAME, original.getName()); ByteArrayOutputStream buff = new ByteArrayOutputStream(); TarArchiveOutputStream tos = new TarArchiveOutputStream(buff); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tos.putArchiveEntry(original); tos.closeArchiveEntry(); tos.close(); TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(buff.toByteArray())); TarArchiveEntry tripped = tis.getNextTarEntry(); assertEquals("round-tripped name", LONG_NAME, tripped.getName()); assertNull("no more entries", tis.getNextEntry()); tis.close(); }
From source file:org.eclipse.jgit.archive.TarFormat.java
/** * @since 4.0/*from w w w . j av a 2s. c o m*/ */ public ArchiveOutputStream createArchiveOutputStream(OutputStream s, Map<String, Object> o) throws IOException { TarArchiveOutputStream out = new TarArchiveOutputStream(s, "UTF-8"); //$NON-NLS-1$ out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); return applyFormatOptions(out, o); }
From source file:org.eclipse.orion.server.docker.server.DockerFile.java
/** * Get the tar file containing the Dockerfile that can be sent to the Docker * build API to create an image. /* w w w. j a v a 2s . c o m*/ * * @return The tar file. */ public File getTarFile() { try { // get the temporary folder location. String tmpDirName = System.getProperty("java.io.tmpdir"); File tmpDir = new File(tmpDirName); if (!tmpDir.exists() || !tmpDir.isDirectory()) { if (logger.isDebugEnabled()) { logger.error("Cannot find the default temporary-file directory: " + tmpDirName); } return null; } // get a temporary folder name long n = random.nextLong(); n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n); String tmpDirStr = Long.toString(n); tempFolder = new File(tmpDir, tmpDirStr); if (!tempFolder.mkdir()) { if (logger.isDebugEnabled()) { logger.error("Cannot create a temporary directory at " + tempFolder.toString()); } return null; } if (logger.isDebugEnabled()) { logger.debug("Dockerfile: Created a temporary directory at " + tempFolder.toString()); } // create the Dockerfile dockerfile = new File(tempFolder, "Dockerfile"); FileOutputStream fileOutputStream = new FileOutputStream(dockerfile); Charset utf8 = Charset.forName("UTF-8"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, utf8); outputStreamWriter.write(getDockerfileContent()); outputStreamWriter.flush(); outputStreamWriter.close(); fileOutputStream.close(); dockerTarFile = new File(tempFolder, "Dockerfile.tar"); TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream( new FileOutputStream(dockerTarFile)); tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); TarArchiveEntry tarEntry = new TarArchiveEntry(dockerfile); tarEntry.setName(dockerfile.getName()); tarArchiveOutputStream.putArchiveEntry(tarEntry); FileInputStream fileInputStream = new FileInputStream(dockerfile); BufferedInputStream inputStream = new BufferedInputStream(fileInputStream); byte[] buffer = new byte[4096]; int bytes_read; while ((bytes_read = inputStream.read(buffer)) != -1) { tarArchiveOutputStream.write(buffer, 0, bytes_read); } inputStream.close(); tarArchiveOutputStream.closeArchiveEntry(); tarArchiveOutputStream.close(); if (logger.isDebugEnabled()) { logger.debug("Dockerfile: Created a docker tar file at " + dockerTarFile.toString()); } return dockerTarFile; } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); } return null; }
From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java
public void createArchive() throws IOException { validate();/*from www . j a v a 2s . c o m*/ log.info("Building tar: " + destFile); TarArchiveOutputStream tarStream = null; try { destFile.getAbsoluteFile().getParentFile().mkdirs(); GzipCompressorOutputStream gzipStream = new GzipCompressorOutputStream( new BufferedOutputStream(new FileOutputStream(destFile))); tarStream = new TarArchiveOutputStream(gzipStream, "UTF-8"); // allow "long" file paths (> 100 chars) tarStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File sourceDir : sourceDirs) { for (File child : sourceDir.listFiles()) { addToTarRecursively(sourceDir, child, tarStream); } } } finally { if (tarStream != null) { tarStream.close(); } } }
From source file:org.fabrician.maven.plugins.GridlibPackageMojo.java
private void createTar(File distroSourceFile, FilenamePatternFilter filter) throws MojoExecutionException { distroFilename.getParentFile().mkdirs(); FileOutputStream out = null;/*from ww w. jav a 2 s .c om*/ CompressorOutputStream cout = null; TarArchiveOutputStream tout = null; try { out = new FileOutputStream(distroFilename); cout = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, out); tout = new TarArchiveOutputStream(cout); tout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); if (distroSourceFile.isDirectory()) { CompressUtils.copyDirToArchiveOutputStream(distroSourceFile, filter, tout, distroAlternateRootDirectory); } else if (CompressUtils.isZip(distroSourceFile)) { CompressUtils.copyZipToArchiveOutputStream(distroSourceFile, filter, tout, distroAlternateRootDirectory); } else if (CompressUtils.isTargz(distroSourceFile)) { CompressUtils.copyTargzToArchiveOutputStream(distroSourceFile, filter, tout, distroAlternateRootDirectory); } else { throw new MojoExecutionException("Unspported source type: " + distroSource); } if (distroResources != null && !"".equals(distroResources)) { if (filtered) { CompressUtils.copyFilteredDirToArchiveOutputStream(distroResources, getFilterProperties(), tout); } else { CompressUtils.copyDirToArchiveOutputStream(distroResources, tout, distroAlternateRootDirectory); } } } catch (Exception e) { e.printStackTrace(); throw new MojoExecutionException(e.getMessage()); } finally { CompressUtils.close(tout); CompressUtils.close(cout); CompressUtils.close(out); } }
From source file:org.hyperledger.fabric.sdk.helper.SDKUtil.java
/** * Compress the given directory src to target tar.gz file * @param src The source directory//from w w w . jav a 2s . c om * @param target The target tar.gz file * @throws IOException */ public static void generateTarGz(String src, String target) throws IOException { File sourceDirectory = new File(src); File destinationArchive = new File(target); String sourcePath = sourceDirectory.getAbsolutePath(); FileOutputStream destinationOutputStream = new FileOutputStream(destinationArchive); TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream( new GzipCompressorOutputStream(new BufferedOutputStream(destinationOutputStream))); archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try { Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true); childrenFiles.remove(destinationArchive); ArchiveEntry archiveEntry; FileInputStream fileInputStream; for (File childFile : childrenFiles) { String childPath = childFile.getAbsolutePath(); String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length()); relativePath = FilenameUtils.separatorsToUnix(relativePath); archiveEntry = new TarArchiveEntry(childFile, relativePath); fileInputStream = new FileInputStream(childFile); archiveOutputStream.putArchiveEntry(archiveEntry); try { IOUtils.copy(fileInputStream, archiveOutputStream); } finally { IOUtils.closeQuietly(fileInputStream); archiveOutputStream.closeArchiveEntry(); } } } finally { IOUtils.closeQuietly(archiveOutputStream); } }
From source file:org.hyperledger.fabric.sdk.helper.Utils.java
/** * Compress the contents of given directory using Tar and Gzip to an in-memory byte array. * * @param sourceDirectory the source directory. * @param pathPrefix a path to be prepended to every file name in the .tar.gz output, or {@code null} if no prefix is required. * @param chaincodeMetaInf/*from w w w .ja va2 s . c om*/ * @return the compressed directory contents. * @throws IOException */ public static byte[] generateTarGz(File sourceDirectory, String pathPrefix, File chaincodeMetaInf) throws IOException { logger.trace(format("generateTarGz: sourceDirectory: %s, pathPrefix: %s, chaincodeMetaInf: %s", sourceDirectory == null ? "null" : sourceDirectory.getAbsolutePath(), pathPrefix, chaincodeMetaInf == null ? "null" : chaincodeMetaInf.getAbsolutePath())); ByteArrayOutputStream bos = new ByteArrayOutputStream(500000); String sourcePath = sourceDirectory.getAbsolutePath(); TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream( new GzipCompressorOutputStream(bos)); archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try { Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true); ArchiveEntry archiveEntry; FileInputStream fileInputStream; for (File childFile : childrenFiles) { String childPath = childFile.getAbsolutePath(); String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length()); if (pathPrefix != null) { relativePath = Utils.combinePaths(pathPrefix, relativePath); } relativePath = FilenameUtils.separatorsToUnix(relativePath); if (TRACE_ENABED) { logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.", relativePath, childFile.getAbsolutePath())); } archiveEntry = new TarArchiveEntry(childFile, relativePath); fileInputStream = new FileInputStream(childFile); archiveOutputStream.putArchiveEntry(archiveEntry); try { IOUtils.copy(fileInputStream, archiveOutputStream); } finally { IOUtils.closeQuietly(fileInputStream); archiveOutputStream.closeArchiveEntry(); } } if (null != chaincodeMetaInf) { childrenFiles = org.apache.commons.io.FileUtils.listFiles(chaincodeMetaInf, null, true); final URI metabase = chaincodeMetaInf.toURI(); for (File childFile : childrenFiles) { final String relativePath = Paths .get("META-INF", metabase.relativize(childFile.toURI()).getPath()).toString(); if (TRACE_ENABED) { logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.", relativePath, childFile.getAbsolutePath())); } archiveEntry = new TarArchiveEntry(childFile, relativePath); fileInputStream = new FileInputStream(childFile); archiveOutputStream.putArchiveEntry(archiveEntry); try { IOUtils.copy(fileInputStream, archiveOutputStream); } finally { IOUtils.closeQuietly(fileInputStream); archiveOutputStream.closeArchiveEntry(); } } } } finally { IOUtils.closeQuietly(archiveOutputStream); } return bos.toByteArray(); }