org.apache.commons.compress.archivers.tar.TarArchiveOutputStream Java Examples

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: nifi   Author: apache   File: FlowFilePackagerV1.java    License: Apache License 2.0 6 votes vote down vote up
private void writeAttributesEntry(final Map<String, String> attributes, final TarArchiveOutputStream tout) throws IOException {
    final StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE properties\n  SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n");
    sb.append("<properties>");
    for (final Map.Entry<String, String> entry : attributes.entrySet()) {
        final String escapedKey = StringEscapeUtils.escapeXml11(entry.getKey());
        final String escapedValue = StringEscapeUtils.escapeXml11(entry.getValue());
        sb.append("\n  <entry key=\"").append(escapedKey).append("\">").append(escapedValue).append("</entry>");
    }
    sb.append("</properties>");

    final byte[] metaBytes = sb.toString().getBytes(StandardCharsets.UTF_8);
    final TarArchiveEntry attribEntry = new TarArchiveEntry(FILENAME_ATTRIBUTES);
    attribEntry.setMode(tarPermissions);
    attribEntry.setSize(metaBytes.length);
    tout.putArchiveEntry(attribEntry);
    tout.write(metaBytes);
    tout.closeArchiveEntry();
}
 
Example #2
Source Project: xodus   Author: JetBrains   File: CompressBackupUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Compresses the content of source and stores newly created archive in dest.
 * In case source is a directory, it will be compressed recursively.
 *
 * @param source file or folder to be archived. Should exist on method call.
 * @param dest   path to the archive to be created. Should not exist on method call.
 * @throws IOException           in case of any issues with underlying store.
 * @throws FileNotFoundException in case source does not exist.
 */
public static void tar(@NotNull File source, @NotNull File dest) throws IOException {
    if (!source.exists()) {
        throw new IllegalArgumentException("No source file or folder exists: " + source.getAbsolutePath());
    }
    if (dest.exists()) {
        throw new IllegalArgumentException("Destination refers to existing file or folder: " + dest.getAbsolutePath());
    }

    try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(new GZIPOutputStream(
            new BufferedOutputStream(new FileOutputStream(dest)), 0x1000))) {
        doTar("", source, tarOut);
    } catch (IOException e) {
        IOUtil.deleteFile(dest); // operation filed, let's remove the destination archive
        throw e;
    }
}
 
Example #3
Source Project: localization_nifi   Author: wangrenlei   File: FlowFilePackagerV1.java    License: Apache License 2.0 6 votes vote down vote up
private void writeAttributesEntry(final Map<String, String> attributes, final TarArchiveOutputStream tout) throws IOException {
    final StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE properties\n  SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n");
    sb.append("<properties>");
    for (final Map.Entry<String, String> entry : attributes.entrySet()) {
        final String escapedKey = StringEscapeUtils.escapeXml11(entry.getKey());
        final String escapedValue = StringEscapeUtils.escapeXml11(entry.getValue());
        sb.append("\n  <entry key=\"").append(escapedKey).append("\">").append(escapedValue).append("</entry>");
    }
    sb.append("</properties>");

    final byte[] metaBytes = sb.toString().getBytes(StandardCharsets.UTF_8);
    final TarArchiveEntry attribEntry = new TarArchiveEntry(FILENAME_ATTRIBUTES);
    attribEntry.setMode(tarPermissions);
    attribEntry.setSize(metaBytes.length);
    tout.putArchiveEntry(attribEntry);
    tout.write(metaBytes);
    tout.closeArchiveEntry();
}
 
Example #4
Source Project: localization_nifi   Author: wangrenlei   File: FlowFilePackagerV1.java    License: Apache License 2.0 6 votes vote down vote up
private void writeContentEntry(final TarArchiveOutputStream tarOut, final InputStream inStream, final long fileSize) throws IOException {
    final TarArchiveEntry entry = new TarArchiveEntry(FILENAME_CONTENT);
    entry.setMode(tarPermissions);
    entry.setSize(fileSize);
    tarOut.putArchiveEntry(entry);
    final byte[] buffer = new byte[512 << 10];//512KB
    int bytesRead = 0;
    while ((bytesRead = inStream.read(buffer)) != -1) { //still more data to read
        if (bytesRead > 0) {
            tarOut.write(buffer, 0, bytesRead);
        }
    }

    copy(inStream, tarOut);
    tarOut.closeArchiveEntry();
}
 
Example #5
Source Project: buck   Author: facebook   File: DefaultJavaLibraryIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * writeTarZst writes a .tar.zst file to 'file'.
 *
 * <p>For each key:value in archiveContents, a file named 'key' with contents 'value' will be
 * created in the archive. File names ending with "/" are considered directories.
 */
private void writeTarZst(Path file, Map<String, byte[]> archiveContents) throws IOException {
  try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(file));
      OutputStream z = new ZstdCompressorOutputStream(o);
      TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
    archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    for (Entry<String, byte[]> mapEntry : archiveContents.entrySet()) {
      String fileName = mapEntry.getKey();
      byte[] fileContents = mapEntry.getValue();
      boolean isRegularFile = !fileName.endsWith("/");

      TarArchiveEntry e = new TarArchiveEntry(fileName);
      if (isRegularFile) {
        e.setSize(fileContents.length);
        archive.putArchiveEntry(e);
        archive.write(fileContents);
      } else {
        archive.putArchiveEntry(e);
      }
      archive.closeArchiveEntry();
    }
    archive.finish();
  }
}
 
Example #6
Source Project: docker-java   Author: docker-java   File: CompressArchiveUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static File archiveTARFiles(File base, Iterable<File> files, String archiveNameWithOutExtension)
        throws IOException {
    File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar");
    tarFile.deleteOnExit();
    try (TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(
            new FileOutputStream(tarFile))))) {
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        for (File file : files) {
            // relativize with method using Path otherwise method with File resolves the symlinks
            // and this is not want we want. If the file is a symlink, the relativized path should
            // keep the symlink name and not the target it points to.
            addFileToTar(tos, file.toPath(), relativize(base.toPath(), file.toPath()));
        }
    }

    return tarFile;
}
 
Example #7
Source Project: neoscada   Author: eclipse   File: DebianPackageWriter.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public DebianPackageWriter ( final OutputStream stream, final GenericControlFile packageControlFile, final TimestampProvider timestampProvider ) throws IOException
{
    this.packageControlFile = packageControlFile;
    this.timestampProvider = timestampProvider;
    if ( getTimestampProvider () == null )
    {
        throw new IllegalArgumentException ( "'timestampProvider' must not be null" );
    }
    BinaryPackageControlFile.validate ( packageControlFile );

    this.ar = new ArArchiveOutputStream ( stream );

    this.ar.putArchiveEntry ( new ArArchiveEntry ( "debian-binary", this.binaryHeader.length, 0, 0, AR_ARCHIVE_DEFAULT_MODE, getTimestampProvider ().getModTime () / 1000 ) );
    this.ar.write ( this.binaryHeader );
    this.ar.closeArchiveEntry ();

    this.dataTemp = File.createTempFile ( "data", null );

    this.dataStream = new TarArchiveOutputStream ( new GZIPOutputStream ( new FileOutputStream ( this.dataTemp ) ) );
    this.dataStream.setLongFileMode ( TarArchiveOutputStream.LONGFILE_GNU );
}
 
Example #8
Source Project: neoscada   Author: eclipse   File: DebianPackageWriter.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private void buildAndAddControlFile () throws IOException, FileNotFoundException
{
    final File controlFile = File.createTempFile ( "control", null );
    try
    {
        try ( GZIPOutputStream gout = new GZIPOutputStream ( new FileOutputStream ( controlFile ) );
              TarArchiveOutputStream tout = new TarArchiveOutputStream ( gout ) )
        {
            tout.setLongFileMode ( TarArchiveOutputStream.LONGFILE_GNU );

            addControlContent ( tout, "control", createControlContent (), -1 );
            addControlContent ( tout, "md5sums", createChecksumContent (), -1 );
            addControlContent ( tout, "conffiles", createConfFilesContent (), -1 );
            addControlContent ( tout, "preinst", this.preinstScript, EntryInformation.DEFAULT_FILE_EXEC.getMode () );
            addControlContent ( tout, "prerm", this.prermScript, EntryInformation.DEFAULT_FILE_EXEC.getMode () );
            addControlContent ( tout, "postinst", this.postinstScript, EntryInformation.DEFAULT_FILE_EXEC.getMode () );
            addControlContent ( tout, "postrm", this.postrmScript, EntryInformation.DEFAULT_FILE_EXEC.getMode () );
        }
        addArFile ( controlFile, "control.tar.gz" );
    }
    finally
    {
        controlFile.delete ();
    }
}
 
Example #9
Source Project: syndesis   Author: syndesisio   File: ProjectGenerator.java    License: Apache License 2.0 6 votes vote down vote up
private void addExtensions(TarArchiveOutputStream tos, Integration integration) throws IOException {
    final Set<String> extensions = resourceManager.collectDependencies(integration).stream()
        .filter(d-> d.isExtension() || d.isExtensionTag())
        .map(Dependency::getId)
        .collect(Collectors.toCollection(TreeSet::new));

    if (!extensions.isEmpty()) {
        addTarEntry(tos, "src/main/resources/loader.properties", generateExtensionLoader(extensions));

        for (String extensionId : extensions) {
            try (InputStream is =
                     resourceManager.loadExtensionBLOB(extensionId).orElseThrow(
                         () -> new IllegalStateException("No extension blob for extension with id:" + extensionId)
                     )) {
                addTarEntry(
                    tos,
                    "extensions/" + Names.sanitize(extensionId) + ".jar",
                    IOUtils.toByteArray(is)
                );
            }
        }
    }
}
 
Example #10
Source Project: twister2   Author: DSC-SPIDAL   File: TarGzipPacker.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * create TarGzipPacker object
 */
public static TarGzipPacker createTarGzipPacker(String targetDir, Config config) {
  // this should be received from config
  String archiveFilename = SchedulerContext.jobPackageFileName(config);
  Path archiveFile = Paths.get(targetDir + "/" + archiveFilename);

  try {
    // construct output stream
    OutputStream outStream = Files.newOutputStream(archiveFile);
    GzipCompressorOutputStream gzipOutputStream = new GzipCompressorOutputStream(outStream);
    TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(gzipOutputStream);

    return new TarGzipPacker(archiveFile, tarOutputStream);
  } catch (IOException ioe) {
    LOG.log(Level.SEVERE, "Archive file can not be created: " + archiveFile, ioe);
    return null;
  }
}
 
Example #11
Source Project: argument-reasoning-comprehension-task   Author: UKPLab   File: CompressedXmiWriter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(UimaContext aContext)
        throws ResourceInitializationException
{
    super.initialize(aContext);

    // some param check
    if (!outputFile.getName().endsWith(".tar.gz")) {
        throw new ResourceInitializationException(
                new IllegalArgumentException("Output file must have .tar.gz extension"));
    }

    typeSystemWritten = false;

    try {
        outputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(
                new BufferedOutputStream(new FileOutputStream(outputFile))));
    }
    catch (IOException ex) {
        throw new ResourceInitializationException(ex);
    }
}
 
Example #12
Source Project: docker-maven-plugin   Author: fabric8io   File: ImageArchiveUtilTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void readInvalidJsonInArchive() throws IOException {
    byte[] archiveBytes;

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(baos)) {
        final byte[] entryData = ("}" + UUID.randomUUID().toString() + "{").getBytes();
        TarArchiveEntry tarEntry = new TarArchiveEntry("not-the-" + ImageArchiveUtil.MANIFEST_JSON);
        tarEntry.setSize(entryData.length);
        tarOutput.putArchiveEntry(tarEntry);
        tarOutput.write(entryData);
        tarOutput.closeArchiveEntry();
        tarOutput.finish();
        archiveBytes = baos.toByteArray();
    }

    ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(new ByteArrayInputStream(archiveBytes));
    Assert.assertNull(manifest);
}
 
Example #13
Source Project: incubator-gobblin   Author: apache   File: StreamUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that recursively adds a directory to a given
 * {@link TarArchiveOutputStream}.
 */
private static void dirToTarArchiveOutputStreamRecursive(FileStatus dirFileStatus, FileSystem fs,
    Optional<Path> destDir, TarArchiveOutputStream tarArchiveOutputStream) throws IOException {

  Preconditions.checkState(fs.isDirectory(dirFileStatus.getPath()));

  Path dir = destDir.isPresent() ? new Path(destDir.get(), dirFileStatus.getPath().getName())
      : new Path(dirFileStatus.getPath().getName());
  dirToTarArchiveOutputStream(dir, tarArchiveOutputStream);

  for (FileStatus childFileStatus : fs.listStatus(dirFileStatus.getPath())) {
    Path childFile = new Path(dir, childFileStatus.getPath().getName());

    if (fs.isDirectory(childFileStatus.getPath())) {
      dirToTarArchiveOutputStreamRecursive(childFileStatus, fs, Optional.of(childFile), tarArchiveOutputStream);
    } else {
      try (FSDataInputStream fsDataInputStream = fs.open(childFileStatus.getPath())) {
        fileToTarArchiveOutputStream(childFileStatus, fsDataInputStream, childFile, tarArchiveOutputStream);
      }
    }
  }
}
 
Example #14
Source Project: Xndroid   Author: XndroidDev   File: TarUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 目录归档
 *
 * @param dir
 * @param taos
 *            TarArchiveOutputStream
 * @param basePath
 * @throws Exception
 */
private static void archiveDir(File dir, TarArchiveOutputStream taos,
                               String basePath) throws Exception {

    File[] files = dir.listFiles();

    if (files.length < 1) {
        TarArchiveEntry entry = new TarArchiveEntry(basePath
                + dir.getName() + PATH);

        taos.putArchiveEntry(entry);
        taos.closeArchiveEntry();
    }

    for (File file : files) {

        // 递归归档
        archive(file, taos, basePath + dir.getName() + PATH);

    }
}
 
Example #15
Source Project: gatk   Author: broadinstitute   File: IOUtils.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void addToTar(TarArchiveOutputStream out, File file, String dir) throws IOException {
    String entry = dir + File.separator + file.getName();
    if (file.isFile()){
        out.putArchiveEntry(new TarArchiveEntry(file, entry));
        try (FileInputStream in = new FileInputStream(file)){
            org.apache.commons.compress.utils.IOUtils.copy(in, out);
        }
        out.closeArchiveEntry();
    } else if (file.isDirectory()) {
        File[] children = file.listFiles();
        if (children != null){
            for (File child : children){
                addToTar(out, child, entry);
            }
        }
    } else {
        System.out.println(file.getName() + " is not supported");
    }
}
 
Example #16
Source Project: docker-java   Author: kpelykh   File: CompressArchiveUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static File archiveTARFiles(File base, Iterable<File> files, String archiveNameWithOutExtension) throws IOException {
    File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar");
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
    try {
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        for (File file : files) {
            TarArchiveEntry tarEntry = new TarArchiveEntry(file);
            tarEntry.setName(relativize(base, file));

            tos.putArchiveEntry(tarEntry);

            if (!file.isDirectory()) {
                FileUtils.copyFile(file, tos);
            }
            tos.closeArchiveEntry();
        }
    } finally {
        tos.close();
    }

    return tarFile;
}
 
Example #17
Source Project: CloverETL-Engine   Author: CloverETL   File: TarDirectoryStreamTest.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected byte[] prepareData() throws IOException {
	try (
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		TarArchiveOutputStream tos = new TarArchiveOutputStream(os);
	) {
		byte[] bytes = "hello world".getBytes();
		TarArchiveEntry entry = new TarArchiveEntry("file.txt");
		entry.setSize(bytes.length);
		tos.putArchiveEntry(entry);
		tos.write(bytes);
		tos.closeArchiveEntry();
		tos.close();
		return os.toByteArray();
	}	
}
 
Example #18
Source Project: xodus   Author: JetBrains   File: EnvironmentTestsBase.java    License: Apache License 2.0 6 votes vote down vote up
public static void archiveDB(final String location, final String target) {
    try {
        System.out.println("Dumping " + location + " to " + target);
        final File root = new File(location);
        final File targetFile = new File(target);
        TarArchiveOutputStream tarGz = new TarArchiveOutputStream(new GZIPOutputStream(
                new BufferedOutputStream(new FileOutputStream(targetFile)), 0x1000));
        for (final File file : IOUtil.listFiles(root)) {
            final long fileSize = file.length();
            if (file.isFile() && fileSize != 0) {
                CompressBackupUtil.archiveFile(tarGz, new BackupStrategy.FileDescriptor(file, ""), fileSize);
            }
        }
        tarGz.close();
    } catch (IOException ioe) {
        System.out.println("Can't create backup");
    }
}
 
Example #19
Source Project: tracecompass   Author: tracecompass   File: TestDirectoryStructureUtil.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private static void addToArchive(TarArchiveOutputStream taos, File dir, File root) throws IOException {
    byte[] buffer = new byte[1024];
    int length;
    int index = root.getAbsolutePath().length();
    for (File file : dir.listFiles()) {
        String name = file.getAbsolutePath().substring(index);
        if (file.isDirectory()) {
            if (file.listFiles().length != 0) {
                addToArchive(taos, file, root);
            } else {
                taos.putArchiveEntry(new TarArchiveEntry(name + File.separator));
                taos.closeArchiveEntry();
            }
        } else {
            try (FileInputStream fis = new FileInputStream(file)) {
                TarArchiveEntry entry = new TarArchiveEntry(name);
                entry.setSize(file.length());
                taos.putArchiveEntry(entry);
                while ((length = fis.read(buffer)) > 0) {
                    taos.write(buffer, 0, length);
                }
                taos.closeArchiveEntry();
            }
        }
    }
}
 
Example #20
Source Project: packagedrone   Author: eclipse   File: DebianPackageWriter.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public DebianPackageWriter ( final OutputStream stream, final BinaryPackageControlFile packageControlFile, final Supplier<Instant> timestampSupplier ) throws IOException
{
    Objects.requireNonNull ( timestampSupplier );

    this.timestampSupplier = timestampSupplier;
    this.packageControlFile = packageControlFile;
    BinaryPackageControlFile.validate ( packageControlFile );

    this.ar = new ArArchiveOutputStream ( stream );

    this.ar.putArchiveEntry ( new ArArchiveEntry ( "debian-binary", this.binaryHeader.length, 0, 0, AR_ARCHIVE_DEFAULT_MODE, timestampSupplier.get ().getEpochSecond () ) );
    this.ar.write ( this.binaryHeader );
    this.ar.closeArchiveEntry ();

    this.dataTemp = File.createTempFile ( "data", null );

    this.dataStream = new TarArchiveOutputStream ( new GZIPOutputStream ( new FileOutputStream ( this.dataTemp ) ) );
    this.dataStream.setLongFileMode ( TarArchiveOutputStream.LONGFILE_GNU );
}
 
Example #21
Source Project: nifi   Author: apache   File: FlowFilePackagerV1.java    License: Apache License 2.0 6 votes vote down vote up
private void writeContentEntry(final TarArchiveOutputStream tarOut, final InputStream inStream, final long fileSize) throws IOException {
    final TarArchiveEntry entry = new TarArchiveEntry(FILENAME_CONTENT);
    entry.setMode(tarPermissions);
    entry.setSize(fileSize);
    tarOut.putArchiveEntry(entry);
    final byte[] buffer = new byte[512 << 10];//512KB
    int bytesRead = 0;
    while ((bytesRead = inStream.read(buffer)) != -1) { //still more data to read
        if (bytesRead > 0) {
            tarOut.write(buffer, 0, bytesRead);
        }
    }

    copy(inStream, tarOut);
    tarOut.closeArchiveEntry();
}
 
Example #22
Source Project: Export   Author: hy-wux   File: ExpProc.java    License: Apache License 2.0 6 votes vote down vote up
private void addArchive(File file, TarArchiveOutputStream aos,
		String basepath) throws Exception {
	if (file.exists()) {
		TarArchiveEntry entry = new TarArchiveEntry(basepath + "/"
				+ file.getName());
		entry.setSize(file.length());
		aos.putArchiveEntry(entry);
		BufferedInputStream bis = new BufferedInputStream(
				new FileInputStream(file));
		int count;
		byte data[] = new byte[1024];
		while ((count = bis.read(data, 0, data.length)) != -1) {
			aos.write(data, 0, count);
		}
		bis.close();
		aos.closeArchiveEntry();
	}
}
 
Example #23
Source Project: writelatex-git-bridge   Author: winstonli   File: Tar.java    License: MIT License 6 votes vote down vote up
private static void addTarFile(
        TarArchiveOutputStream tout,
        Path base,
        File file
) throws IOException {
    Preconditions.checkArgument(
            file.isFile(),
            "given file" +
            " is not file: %s", file);
    checkFileSize(file.length());
    String name = base.relativize(
            Paths.get(file.getAbsolutePath())
    ).toString();
    ArchiveEntry entry = tout.createArchiveEntry(file, name);
    tout.putArchiveEntry(entry);
    try (InputStream in = new FileInputStream(file)) {
        IOUtils.copy(in, tout);
    }
    tout.closeArchiveEntry();
}
 
Example #24
Source Project: aws-codepipeline-plugin-for-jenkins   Author: awslabs   File: CompressionTools.java    License: Apache License 2.0 6 votes vote down vote up
public static void compressTarFile(
        final File temporaryTarFile,
        final Path pathToCompress,
        final BuildListener listener)
        throws IOException {
    try (final TarArchiveOutputStream tarArchiveOutputStream =
                 new TarArchiveOutputStream(
                 new BufferedOutputStream(
                 new FileOutputStream(temporaryTarFile)))) {

        compressArchive(
                pathToCompress,
                tarArchiveOutputStream,
                new ArchiveEntryFactory(CompressionType.Tar),
                CompressionType.Tar,
                listener);
    }
}
 
Example #25
Source Project: incubator-pinot   Author: apache   File: TarGzCompressionUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a tar entry for the path specified with a name built from the base
 * passed in and the file/directory name. If the path is a directory, a
 * recursive call is made such that the full directory is added to the tar.
 *
 * @param tOut
 *          The tar file's output stream
 * @param path
 *          The filesystem path of the file/directory being added
 * @param base
 *          The base prefix to for the name of the tar file entry
 *
 * @throws IOException
 *           If anything goes wrong
 */
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base)
    throws IOException {
  File f = new File(path);
  String entryName = base + f.getName();
  TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

  tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
  tOut.putArchiveEntry(tarEntry);

  if (f.isFile()) {
    IOUtils.copy(new FileInputStream(f), tOut);

    tOut.closeArchiveEntry();
  } else {
    tOut.closeArchiveEntry();

    File[] children = f.listFiles();

    if (children != null) {
      for (File child : children) {
        addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
      }
    }
  }
}
 
Example #26
Source Project: jphp   Author: jphp-group   File: CompressExtension.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onRegister(CompileScope scope) {
    // register classes ...
    registerWrapperClass(scope, ArchiveEntry.class, PArchiveEntry.class);
    registerWrapperClass(scope, TarArchiveEntry.class, PTarArchiveEntry.class);
    registerWrapperClass(scope, ZipArchiveEntry.class, PZipArchiveEntry.class);

    registerWrapperClass(scope, ArchiveInputStream.class, PArchiveInput.class);
    registerWrapperClass(scope, TarArchiveInputStream.class, PTarArchiveInput.class);
    registerWrapperClass(scope, ZipArchiveInputStream.class, PZipArchiveInput.class);

    registerWrapperClass(scope, ArchiveOutputStream.class, PArchiveOutput.class);
    registerWrapperClass(scope, TarArchiveOutputStream.class, PTarArchiveOutput.class);
    registerWrapperClass(scope, ZipArchiveOutputStream.class, PZipArchiveOutput.class);

    registerClass(scope, PGzipOutputStream.class);
    registerClass(scope, PGzipInputStream.class);
    registerClass(scope, PBzip2OutputStream.class);
    registerClass(scope, PBZip2InputStream.class);
    registerClass(scope, PLz4OutputStream.class);
    registerClass(scope, PLz4InputStream.class);

    registerClass(scope, PArchive.class);
    registerClass(scope, PTarArchive.class);
    registerClass(scope, PZipArchive.class);
}
 
Example #27
Source Project: che   Author: eclipse   File: TarUtils.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private static void addFileEntry(
    TarArchiveOutputStream tarOut, String entryName, File file, long modTime) throws IOException {
  final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
  if (modTime >= 0) {
    tarEntry.setModTime(modTime);
  }
  tarOut.putArchiveEntry(tarEntry);
  try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
    final byte[] buf = new byte[BUF_SIZE];
    int r;
    while ((r = in.read(buf)) != -1) {
      tarOut.write(buf, 0, r);
    }
  }
  tarOut.closeArchiveEntry();
}
 
Example #28
Source Project: writelatex-git-bridge   Author: winstonli   File: Tar.java    License: MIT License 6 votes vote down vote up
private static void addTarDir(
        TarArchiveOutputStream tout,
        Path base,
        File dir
) throws IOException {
    Preconditions.checkArgument(dir.isDirectory());
    String name = base.relativize(
            Paths.get(dir.getAbsolutePath())
    ).toString();
    ArchiveEntry entry = tout.createArchiveEntry(dir, name);
    tout.putArchiveEntry(entry);
    tout.closeArchiveEntry();
    for (File f : dir.listFiles()) {
        addTarEntry(tout, base, f);
    }
}
 
Example #29
Source Project: flow   Author: vaadin   File: DefaultArchiveExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expected = ArchiveExtractionException.class)
public void extractTarAsZip_ArchiveExtractionExceptionIsThrown()
        throws IOException, ArchiveExtractionException{
    File archiveFile = new File(baseDir, "archive.zip");
    archiveFile.createNewFile();
    Path tempArchive = archiveFile.toPath();

    try (OutputStream fo = Files.newOutputStream(
            tempArchive); OutputStream gzo = new GzipCompressorOutputStream(
            fo); ArchiveOutputStream o = new TarArchiveOutputStream(gzo)) {
        o.putArchiveEntry(
                o.createArchiveEntry(new File(ROOT_FILE), ROOT_FILE));
        o.closeArchiveEntry();
    }

    new DefaultArchiveExtractor().extract(archiveFile, targetDir);

}
 
Example #30
Source Project: digdag   Author: treasure-data   File: Archiver.java    License: Apache License 2.0 5 votes vote down vote up
List<String> createArchive(Path projectPath, Path output, boolean copyOutgoingSymlinks)
        throws IOException
{
    out.println("Creating " + output + "...");

    ProjectArchive project = projectLoader.load(projectPath, WorkflowResourceMatcher.defaultMatcher(), cf.create());

    ImmutableList.Builder<String> workflowResources = ImmutableList.builder();

    try (TarArchiveOutputStream tar = new TarArchiveOutputStream(new GzipCompressorOutputStream(Files.newOutputStream(output)))) {
        // default mode for file names longer than 100 bytes is throwing an exception (LONGFILE_ERROR)
        tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

        project.listFiles((resourceName, absPath) -> {
            TarArchiveEntry e = buildFileOrSymlinkEntryOrNull(projectPath, absPath, resourceName, copyOutgoingSymlinks);
            if (e != null) {
                tar.putArchiveEntry(e);
                if (!e.isSymbolicLink()) {
                    try (InputStream in = Files.newInputStream(absPath)) {
                        ByteStreams.copy(in, tar);
                    }
                }
                tar.closeArchiveEntry();

                if (WorkflowResourceMatcher.defaultMatcher().matches(resourceName, absPath)) {
                    workflowResources.add(resourceName);
                }

                // If symbolic link entry is created, don't copy files recursively
                return !e.isSymbolicLink();
            }
            return true;
        });
    }

    return workflowResources.build();
}