Example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream.

Prototype

public TarArchiveOutputStream(OutputStream os) 

Source Link

Document

Constructor for TarInputStream.

Usage

From source file:com.mobilesorcery.sdk.builder.linux.deb.BuilderUtil.java

/**
 * Adds a directory structure to a gzipped tar.
 *
 * @param o The output file that the tar+gz will be written to
 * @param i The directory to add/*  w  w w.  ja  va  2  s. co  m*/
 *
 * @throws IOException If there's an I/O error
 */
public void tarGZipDirectory(File o, File i) throws IOException {
    FileOutputStream fos = new FileOutputStream(o);
    GzipCompressorOutputStream gzos = new GzipCompressorOutputStream(fos);
    TarArchiveOutputStream tos = new TarArchiveOutputStream(gzos);

    // Recursivly add to tar
    doAddFileToTar(tos, "./", i);

    // Close
    tos.close();
    gzos.close();
    fos.close();
}

From source file:com.salsaberries.narchiver.Writer.java

/**
 *
 * @param files/*from  w  ww .  ja  va  2s . com*/
 * @param output
 * @throws IOException
 */
public static void compressFiles(Collection<File> files, File output) throws IOException {
    // Wrap the output file stream in streams that will tar and gzip everything
    try (FileOutputStream fos = new FileOutputStream(output);
            // Wrap the output file stream in streams that will tar and gzip everything
            TarArchiveOutputStream taos = new TarArchiveOutputStream(
                    new GZIPOutputStream(new BufferedOutputStream(fos)))) {

        // Enable support for long file names
        taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        // Put all the files in a compressed output file
        for (File f : files) {
            addFilesToCompression(taos, f, ".");
        }
    }
}

From source file:com.github.trask.comet.loadtest.aws.ChefBootstrap.java

private static void prepareCookbook(String cookbooksDir, List<String> cookbooks, String destFilename)
        throws IOException, FileNotFoundException {

    TarArchiveOutputStream cookbooksTarOut = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(destFilename)));
    for (String cookbook : cookbooks) {
        File cookbookDir = new File(cookbooksDir, cookbook);
        if (!cookbookDir.exists()) {
            throw new IllegalStateException("missing cookbook " + cookbookDir);
        }/*from   w  ww  .j av a 2  s  .  co  m*/
        addToTarWithBase(cookbookDir, cookbooksTarOut, "cookbooks/");
    }
    cookbooksTarOut.close();
}

From source file:divconq.ctp.stream.TarStream.java

@Override
public ReturnOption handle(FileDescriptor file, ByteBuf data) {
    if (file == FileDescriptor.FINAL) {
        if (this.tstream == null)
            return this.downstream.handle(file, data);

        this.finalflag = true;
    }//from w  ww .  j av  a  2s.c  om

    // I don't think tar cares about folder entries at this stage - tar is for file content only
    // folder scanning is upstream in the FileSourceStream and partners
    // TODO try with ending / to file name
    if (file.isFolder())
        return ReturnOption.CONTINUE;

    // init if not set for this round of processing 
    if (this.tstream == null) {
        this.bstream = new CyclingByteBufferOutputStream();
        this.tstream = new TarArchiveOutputStream(this.bstream);
        this.tstream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    }

    ByteBuf in = data;
    ByteBuf out = null;

    // always allow for a header (512) and/or footer (1024) in addition to content
    int sizeEstimate = (in != null) ? in.readableBytes() + 2048 : 2048;
    out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate);

    this.bstream.installBuffer(out);

    // TODO if there is no output available to send and not EOF then just request more,
    // no need to send a message that is empty and not EOF

    FileDescriptor blk = new FileDescriptor();

    if (StringUtil.isNotEmpty(this.lastpath)) {
        blk.setPath(this.lastpath);
    } else {
        if (file.hasPath())
            this.lastpath = "/"
                    + (StringUtil.isNotEmpty(this.nameHint) ? this.nameHint : file.path().getFileName())
                    + ".tar";
        else if (StringUtil.isNotEmpty(this.nameHint))
            this.lastpath = "/" + this.nameHint + ".tar";
        else
            this.lastpath = "/" + FileUtil.randomFilename() + ".tar";

        blk.setPath(this.lastpath);
    }

    blk.setModTime(System.currentTimeMillis());

    if (!this.archiveopenflag && !this.finalflag) {
        TarArchiveEntry tentry = new TarArchiveEntry(file.getPath().toString().substring(1), true);
        tentry.setSize(file.getSize());
        tentry.setModTime(file.getModTime());

        try {
            this.tstream.putArchiveEntry(tentry);
        } catch (IOException x) {
            if (in != null)
                in.release();

            out.release();
            OperationContext.get().getTaskRun().kill("Problem writing tar entry: " + x);
            return ReturnOption.DONE;
        }

        this.archiveopenflag = true;
    }

    if (in != null)
        try {
            this.tstream.write(in.array(), in.arrayOffset(), in.writerIndex());
        } catch (IOException x) {
            in.release();
            out.release();
            OperationContext.get().getTaskRun().kill("Problem writing tar body: " + x);
            return ReturnOption.DONE;
        }

    if (file.isEof()) {
        try {
            this.tstream.closeArchiveEntry();
        } catch (IOException x) {
            if (in != null)
                in.release();

            out.release();
            OperationContext.get().getTaskRun().kill("Problem closing tar entry: " + x);
            return ReturnOption.DONE;
        }

        this.archiveopenflag = false;
    }

    if (in != null)
        in.release();

    if (file == FileDescriptor.FINAL) {
        blk.setEof(true);

        try {
            this.tstream.close();
        } catch (IOException x) {
            //in.release();
            out.release();
            OperationContext.get().getTaskRun().kill("Problem closing tar stream: " + x);
            return ReturnOption.DONE;
        }

        this.tstream = null;
        this.bstream = null;
    } else
        this.bstream.uninstallBuffer(); // we are done with out forever, don't reference it

    System.out.println("tar sending: " + out.readableBytes());

    ReturnOption v = this.downstream.handle(blk, out);

    if (!this.finalflag)
        return v;

    if (v == ReturnOption.CONTINUE)
        return this.downstream.handle(FileDescriptor.FINAL, null);

    return ReturnOption.DONE;
}

From source file:com.codenvy.commons.lang.TarUtils.java

public static void tarFiles(File tar, long modTime, File... files) throws IOException {
    try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(
            new BufferedOutputStream(new FileOutputStream(tar)))) {
        tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (File f : files) {
            if (f.isDirectory()) {
                addDirectoryEntry(tarOut, f.getName(), f, modTime);
                final String parentPath = f.getParentFile().getAbsolutePath();
                addDirectoryRecursively(tarOut, parentPath, f, modTime, IoUtil.ANY_FILTER);
            } else if (f.isFile()) {
                addFileEntry(tarOut, f.getName(), f, modTime);
            }/*from w  ww . ja  v  a 2 s.c o  m*/
        }
    }
}

From source file:com.shopzilla.hadoop.repl.commands.util.ClusterStateManager.java

public static void compressFiles(Collection<File> files, File output) throws IOException {
    // Create the output stream for the output file
    FileOutputStream fos = new FileOutputStream(output);
    // Wrap the output file stream in streams that will tar and gzip everything
    TarArchiveOutputStream taos = new TarArchiveOutputStream(
            new GZIPOutputStream(new BufferedOutputStream(fos)));
    // TAR has an 8 gig file limit by default, this gets around that
    taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); // to get past the 8 gig limit
    // TAR originally didn't support long file names, so enable the support for it
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    // Get to putting all the files in the compressed output file
    for (File f : files) {
        addFilesToCompression(taos, f, ".");
    }/*from  ww  w. j  a v  a  2  s . c o  m*/

    // Close everything up
    taos.close();
    fos.close();
}

From source file:com.amazonaws.codepipeline.jenkinsplugin.CompressionTools.java

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);
    }//from   w w  w.j a v  a2s .  c  om
}

From source file:com.netflix.spinnaker.halyard.core.registry.v1.GitProfileReader.java

@Override
public InputStream readArchiveProfile(String artifactName, String version, String profileName)
        throws IOException {
    Path profilePath = Paths.get(profilePath(artifactName, version, profileName));

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);

    ArrayList<Path> filePathsToAdd = java.nio.file.Files
            .walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
            .filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));

    for (Path path : filePathsToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
        int permissions = FileModeUtils.getFileMode(Files.getPosixFilePermissions(path));
        permissions = FileModeUtils.setFileBit(permissions);
        tarEntry.setMode(permissions);/*from  w  w w  .  jav a2s .c  om*/
        tarArchive.putArchiveEntry(tarEntry);
        IOUtils.copy(Files.newInputStream(path), tarArchive);
        tarArchive.closeArchiveEntry();
    }

    tarArchive.finish();
    tarArchive.close();

    return new ByteArrayInputStream(os.toByteArray());
}

From source file:jetbrains.exodus.env.EnvironmentTestsBase.java

public static void archiveDB(final String location, final String target) {
    try {/*from  w w w. ja v a 2  s.c  o m*/
        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, "", file, fileSize);
            }
        }
        tarGz.close();
    } catch (IOException ioe) {
        System.out.println("Can't create backup");
    }
}

From source file:com.spotify.docker.client.CompressedDirectory.java

/**
 * This method creates a gzip tarball of the specified directory. File permissions will be
 * retained. The file will be created in a temporary directory using the {@link
 * Files#createTempFile(String, String, FileAttribute[])} method. The returned object is
 * auto-closeable, and upon closing it, the archive file will be deleted.
 *
 * @param directory the directory to compress
 * @return a Path object representing the compressed directory
 * @throws IOException if the compressed directory could not be created.
 *//*from   www.  j  a  v a 2s .c  o m*/
public static CompressedDirectory create(final Path directory) throws IOException {
    final Path file = Files.createTempFile("docker-client-", ".tar.gz");

    final Path dockerIgnorePath = directory.resolve(".dockerignore");
    final ImmutableSet<PathMatcher> ignoreMatchers = parseDockerIgnore(dockerIgnorePath);

    try (final OutputStream fileOut = Files.newOutputStream(file);
            final GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(fileOut);
            final TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut)) {
        tarOut.setLongFileMode(LONGFILE_POSIX);
        tarOut.setBigNumberMode(BIGNUMBER_POSIX);
        Files.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new Visitor(directory, ignoreMatchers, tarOut));

    } catch (Throwable t) {
        // If an error occurs, delete temporary file before rethrowing exception.
        try {
            Files.delete(file);
        } catch (IOException e) {
            // So we don't lose track of the reason the file was deleted... might be important
            t.addSuppressed(e);
        }

        throw t;
    }

    return new CompressedDirectory(file);
}