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.apache.hadoop.hive.common.CompressionUtils.java

/**
 * Archive all the files in the inputFiles into outputFile
 *
 * @param inputFiles//from www .  j av a 2s  .  com
 * @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.nutch.tools.CommonCrawlDataDumper.java

private void constructNewStream(File outputDir) throws IOException {
    String archiveName = new SimpleDateFormat("yyyyMMddhhmm'.tar.gz'").format(new Date());
    LOG.info("Creating a new gzip archive: " + archiveName);
    fileOutput = new FileOutputStream(new File(outputDir + File.separator + archiveName));
    bufOutput = new BufferedOutputStream(fileOutput);
    gzipOutput = new GzipCompressorOutputStream(bufOutput);
    tarOutput = new TarArchiveOutputStream(gzipOutput);
    tarOutput.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}

From source file:org.apache.openaz.xacml.admin.components.PolicyWorkspace.java

@Override
public InputStream getStream() {
    ///*  w w w.ja  va2  s .c om*/
    // Grab our working repository
    //
    final Path repoPath = ((XacmlAdminUI) getUI()).getUserGitPath();
    Path workspacePath = ((XacmlAdminUI) getUI()).getUserWorkspace();
    final Path tarFile = Paths.get(workspacePath.toString(), "Repository.tgz");

    try (OutputStream os = Files.newOutputStream(tarFile)) {
        try (GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(os)) {
            try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzOut)) {

                tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

                Files.walkFileTree(repoPath, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                            throws IOException {
                        if (dir.getFileName().toString().startsWith(".git")) {
                            return FileVisitResult.SKIP_SUBTREE;
                        }
                        Path relative = repoPath.relativize(dir);
                        if (relative.toString().isEmpty()) {
                            return super.preVisitDirectory(dir, attrs);
                        }
                        TarArchiveEntry entry = new TarArchiveEntry(relative.toFile());
                        tarOut.putArchiveEntry(entry);
                        tarOut.closeArchiveEntry();
                        return super.preVisitDirectory(dir, attrs);
                    }

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        if (file.getFileName().toString().endsWith(".xml") == false) {
                            return super.visitFile(file, attrs);
                        }
                        Path relative = repoPath.relativize(file);
                        TarArchiveEntry entry = new TarArchiveEntry(relative.toFile());
                        entry.setSize(Files.size(file));
                        tarOut.putArchiveEntry(entry);
                        try {
                            IOUtils.copy(Files.newInputStream(file), tarOut);
                        } catch (IOException e) {
                            logger.error(e);
                        }
                        tarOut.closeArchiveEntry();
                        return super.visitFile(file, attrs);
                    }

                });
                tarOut.finish();
            }
        }
    } catch (IOException e) {
        logger.error(e);
    }
    try {
        return Files.newInputStream(tarFile);
    } catch (IOException e) {
        logger.error(e);
    }
    return null;
}

From source file:org.apache.openejb.maven.plugin.BuildTomEEMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    super.execute();

    if (formats == null) {
        formats = Collections.emptyMap();
    }/*w  w  w.j  av a  2 s.c o  m*/

    String prefix = catalinaBase.getParentFile().getAbsolutePath();
    if (!prefix.endsWith(File.separator)) {
        prefix += File.separator;
    }
    if (skipArchiveRootFolder) {
        prefix += catalinaBase.getName() + File.separator;
    }

    if (zip || formats.containsKey("zip")) {
        getLog().info("Zipping Custom TomEE Distribution");

        final String zip = formats.get("zip");
        final File output = zip != null ? new File(zip) : zipFile;
        try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(output))) {
            for (final String entry : catalinaBase.list()) {
                zip(zos, new File(catalinaBase, entry), prefix);
            }
        } catch (final IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }

        attach("zip", output);
    }
    if (formats != null) {
        formats.remove("zip"); //handled previously for compatibility

        for (final Map.Entry<String, String> format : formats.entrySet()) {
            final String key = format.getKey();
            getLog().info(key + "-ing Custom TomEE Distribution");

            if ("tar.gz".equals(key)) {
                final String out = format.getValue();
                final File output = out != null ? new File(out)
                        : new File(base.getParentFile(), base.getName() + "." + key);
                Files.mkdirs(output.getParentFile());

                try (final TarArchiveOutputStream tarGz = new TarArchiveOutputStream(
                        new GZIPOutputStream(new FileOutputStream(output)))) {
                    tarGz.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
                    for (final String entry : catalinaBase.list()) {
                        tarGz(tarGz, new File(catalinaBase, entry), prefix);
                    }
                } catch (final IOException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }

                attach(key, output);
            } else {
                throw new MojoExecutionException(key + " format not supported");
            }
        }
    }
}

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  ww  .  ja v  a  2s .  co m
        tarOutputStream.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFile(tarOutputStream, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}

From source file:org.artifactory.util.ArchiveUtils.java

public static ArchiveOutputStream createArchiveOutputStream(OutputStream outputStream, ArchiveType archiveType)
        throws IOException {
    ArchiveOutputStream result = null;/*from w  w  w .  j a  va2 s . c  o m*/
    switch (archiveType) {
    case ZIP:
        result = new ZipArchiveOutputStream(outputStream);
        break;
    case TAR:
        result = new TarArchiveOutputStream(outputStream);
        ((TarArchiveOutputStream) result).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        break;
    case TARGZ:
        result = new TarArchiveOutputStream(new GzipCompressorOutputStream(outputStream));
        ((TarArchiveOutputStream) result).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        break;
    case TGZ:
        result = new TarArchiveOutputStream(new GzipCompressorOutputStream(outputStream));
        ((TarArchiveOutputStream) result).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        break;
    }

    if (result == null) {
        throw new IllegalArgumentException("Unsupported archive type: '" + archiveType + "'");
    }

    return result;
}

From source file:org.codehaus.plexus.archiver.tar.TarArchiver.java

protected void execute() throws ArchiverException, IOException {
    if (!checkForced()) {
        return;// ww  w . j  a  v  a2s .com
    }

    ResourceIterator iter = getResources();
    if (!iter.hasNext()) {
        throw new ArchiverException("You must set at least one file.");
    }

    File tarFile = getDestFile();

    if (tarFile == null) {
        throw new ArchiverException("You must set the destination tar file.");
    }
    if (tarFile.exists() && !tarFile.isFile()) {
        throw new ArchiverException(tarFile + " isn't a file.");
    }
    if (tarFile.exists() && !tarFile.canWrite()) {
        throw new ArchiverException(tarFile + " is read-only.");
    }

    getLogger().info("Building tar: " + tarFile.getAbsolutePath());

    final OutputStream os = new FileOutputStream(tarFile);
    tOut = new TarArchiveOutputStream(compress(compression, os), "UTF8");
    if (longFileMode.isTruncateMode()) {
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
    } else if (longFileMode.isPosixMode() || longFileMode.isPosixWarnMode()) {
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        // Todo: Patch 2.5.1   for this fix. Also make closeable fix on 2.5.1
        tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    } else if (longFileMode.isFailMode() || longFileMode.isOmitMode()) {
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
    } else {
        // warn or GNU
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    }

    longWarningGiven = false;
    try {
        while (iter.hasNext()) {
            ArchiveEntry entry = iter.next();
            // Check if we don't add tar file in itself
            if (ResourceUtils.isSame(entry.getResource(), tarFile)) {
                throw new ArchiverException("A tar file cannot include itself.");
            }
            String fileName = entry.getName();
            String name = StringUtils.replace(fileName, File.separatorChar, '/');

            tarFile(entry, tOut, name);
        }
    } finally {
        IOUtil.close(tOut);
    }
}

From source file:org.codehaus.plexus.archiver.tar.TarRoundTripTest.java

/**
 * test round-tripping long (GNU) entries
 *//*from  w w  w  . ja va2s.  co  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.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java

private File archiveBag() throws PackageToolException {
    File archivedFile = new File(packageLocationDir, bagBaseDir.getName() + "." + archivingFormat);
    try {//from   w w w .  ja v a  2s. c  o  m
        FileOutputStream fos = new FileOutputStream(archivedFile);
        ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream(archivingFormat,
                new BufferedOutputStream(fos));
        if (aos instanceof TarArchiveOutputStream) {
            ((TarArchiveOutputStream) aos).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        }
        // Get to putting all the files in the compressed output file
        for (File f : bagBaseDir.listFiles()) {
            //To support the cancelling of package creation we check here to see if the thread has been interrupted.
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            addFilesToArchive(aos, f);
        }
        aos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_FILE_NOT_FOUND_EXCEPTION, e,
                "Exception occurred when serializing the bag.");
    } catch (IOException e) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_IO_EXCEPTION, e,
                "Exception occurred when serializing the bag.");
    } catch (ArchiveException e) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_ASSEMBLER_ARCHIVE_EXP, e,
                "Archiving format \"" + archivingFormat + "\" is not supported.");
    }

    return archivedFile;
}

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. //from   w ww .j a v  a2 s .  co 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;
}