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

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

Introduction

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

Prototype

int LONGFILE_GNU

To view the source code for org.apache.commons.compress.archivers.tar TarArchiveOutputStream LONGFILE_GNU.

Click Source Link

Document

GNU tar extensions are used to store long file names in the archive.

Usage

From source file:org.eclipse.packagedrone.utils.deb.build.DebianPackageWriter.java

public DebianPackageWriter(final OutputStream stream, final BinaryPackageControlFile packageControlFile)
        throws IOException {
    this.packageControlFile = packageControlFile;
    BinaryPackageControlFile.validate(packageControlFile);

    this.ar = new ArArchiveOutputStream(stream);

    this.ar.putArchiveEntry(new ArArchiveEntry("debian-binary", this.binaryHeader.length));
    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);
}

From source file:org.eclipse.packagedrone.utils.deb.build.DebianPackageWriter.java

private void buildAndAddControlFile() throws IOException, FileNotFoundException {
    final File controlFile = File.createTempFile("control", null);
    try {//from  w  w w.  ja  v  a 2 s. c  o m
        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();
    }
}

From source file:org.eclipse.scada.utils.pkg.deb.DebianPackageWriter.java

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");
    }//from  w ww. j  a  v a 2  s .  com
    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);
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java

public void createArchive() throws IOException {
    validate();//w ww  .  j a  v  a2  s  . c om
    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.j  ava  2s  .  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//  w  ww.j  a  v a  2 s  . c o  m
 * @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 ww w  .j  av  a2s.  co  m
 * @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();
}

From source file:org.hyperledger.fabric.sdkintegration.Util.java

/**
 * Generate a targz inputstream from source folder.
 *
 * @param src        Source location//  w w w .  ja v a 2  s.  com
 * @param pathPrefix prefix to add to the all files found.
 * @return return inputstream.
 * @throws IOException
 */
public static InputStream generateTarGzInputStream(File src, String pathPrefix) throws IOException {
    File sourceDirectory = src;

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);

    String sourcePath = sourceDirectory.getAbsolutePath();

    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(new BufferedOutputStream(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);

            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 new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.icgc.dcc.download.client.io.ArchiveOutputStream.java

private static TarArchiveOutputStream createTarOutputStream(OutputStream out) {
    val tarOut = new TarArchiveOutputStream(new BufferedOutputStream(out));
    tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tarOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);

    return tarOut;
}

From source file:org.jclouds.docker.compute.features.internal.Archives.java

public static File tar(File baseDir, String archivePath) throws IOException {
    // Check that the directory is a directory, and get its contents
    checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir);
    File[] files = baseDir.listFiles();
    File tarFile = new File(archivePath);

    String token = getLast(Splitter.on("/").split(archivePath.substring(0, archivePath.lastIndexOf("/"))));

    byte[] buf = new byte[1024];
    int len;/*from  ww w .  ja v a 2 s.c  o  m*/
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    for (File file : files) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(file);
        tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString())));
        tos.putArchiveEntry(tarEntry);
        if (!file.isDirectory()) {
            FileInputStream fin = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fin);
            while ((len = in.read(buf)) != -1) {
                tos.write(buf, 0, len);
            }
            in.close();
        }
        tos.closeArchiveEntry();
    }
    tos.close();
    return tarFile;
}