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:com.fizzed.stork.deploy.Archive.java

static private ArchiveOutputStream newArchiveOutputStream(Path file, String format) throws IOException {
    switch (format) {
    case "tar.gz":
        TarArchiveOutputStream tgzout = new TarArchiveOutputStream(
                new GzipCompressorOutputStream(Files.newOutputStream(file)));
        tgzout.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
        tgzout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        return tgzout;
    case "zip":
        return new ZipArchiveOutputStream(Files.newOutputStream(file));
    default:/*from  w  w  w . j av  a 2s  . c  om*/
        throw new IOException("Unsupported archive file type (we support .tar.gz and .zip)");
    }
}

From source file:com.linuxbox.enkive.workspace.searchFolder.SearchFolder.java

/**
 * Writes a tar.gz file to the provided outputstream
 * /* ww  w.  j a va2 s  .c om*/
 * @param outputStream
 * @throws IOException
 */
public void exportSearchFolder(OutputStream outputStream) throws IOException {
    BufferedOutputStream out = new BufferedOutputStream(outputStream);
    GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out);
    TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);

    File mboxFile = File.createTempFile("mbox-export", ".mbox");
    BufferedWriter mboxWriter = new BufferedWriter(new FileWriter(mboxFile));
    // Write mbox to tempfile?
    for (String messageId : getMessageIds()) {
        try {
            Message message = retrieverService.retrieve(messageId);

            mboxWriter.write("From " + message.getDateStr() + "\r\n");
            BufferedReader reader = new BufferedReader(new StringReader(message.getReconstitutedEmail()));
            String tmpLine;
            while ((tmpLine = reader.readLine()) != null) {
                if (tmpLine.startsWith("From "))
                    mboxWriter.write(">" + tmpLine);
                else
                    mboxWriter.write(tmpLine);
                mboxWriter.write("\r\n");
            }
        } catch (CannotRetrieveException e) {
            // Add errors to report
            // if (LOGGER.isErrorEnabled())
            // LOGGER.error("Could not retrieve message with id"
            // + messageId);
        }
    }
    mboxWriter.flush();
    mboxWriter.close();
    // Add mbox to tarfile
    TarArchiveEntry mboxEntry = new TarArchiveEntry(mboxFile, "filename.mbox");
    tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tOut.putArchiveEntry(mboxEntry);
    IOUtils.copy(new FileInputStream(mboxFile), tOut);
    tOut.flush();
    tOut.closeArchiveEntry();
    mboxWriter.close();
    mboxFile.delete();
    // Create report in tempfile?

    // Add report to tarfile

    // Close out stream
    tOut.finish();
    outputStream.flush();
    tOut.close();
    outputStream.close();

}

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

private static TarArchiveOutputStream buildTarStream(OutputStream outputStream) throws IOException {
    OutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
    bufferedOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);

    TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(bufferedOutputStream);
    tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    return tarArchiveOutputStream;
}

From source file:hudson.gridmaven.gridlayer.HadoopInstance.java

private void addFileToTar(TarArchiveOutputStream tOut, String path, String base, String root)
        throws IOException {
    if (!root.equals(path)) {
        File f = new File(path);
        String entryName = base + f.getName();
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        tOut.putArchiveEntry(tarEntry);//from  w ww .  j av a 2 s .  c  o m

        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) {
                    addFileToTar(tOut, child.getAbsolutePath(), entryName + "/", root);
                }
            }
        }
    } else {
        File f = new File(path);
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToTar(tOut, child.getAbsolutePath(), "", root);
            }
        }
    }
}

From source file:ezbake.deployer.utilities.ArtifactHelpers.java

/**
 * Append to the given ArchiveInputStream writing to the given outputstream, the given entries to add.
 * This will duplicate the InputStream to the Output.
 *
 * @param inputStream - archive input to append to
 * @param output      - what to copy the modified archive to
 * @param filesToAdd  - what entries to append.
 *///from  w w  w.j a  va  2 s.  c o m
private static void appendFilesInTarArchive(ArchiveInputStream inputStream, OutputStream output,
        Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    try {
        HashMap<String, ArtifactDataEntry> newFiles = new HashMap<>();
        for (ArtifactDataEntry entry : filesToAdd) {
            newFiles.put(entry.getEntry().getName(), entry);
        }
        GZIPOutputStream gzs = new GZIPOutputStream(output);
        TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs);
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = inputStream.getNextEntry()) != null) {
            //If we're passing in the same file, don't copy into the new archive
            if (!newFiles.containsKey(nextEntry.getName())) {
                aos.putArchiveEntry(nextEntry);
                IOUtils.copy(inputStream, aos);
                aos.closeArchiveEntry();
            }
        }

        for (ArtifactDataEntry entry : filesToAdd) {
            aos.putArchiveEntry(entry.getEntry());
            IOUtils.write(entry.getData(), aos);
            aos.closeArchiveEntry();
        }
        aos.finish();
        gzs.finish();
    } catch (ArchiveException | IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}

From source file:gzipper.algorithms.Gzip.java

@Override
public void run() {
    /*check whether archive with given name already exists; 
    if so, add index to file name an re-check*/
    if (_createArchive) {
        try {//from w ww .ja  v  a 2 s . co m
            File file = new File(_path + _archiveName + ".tar.gz");
            while (file.exists()) {
                ++_nameIndex;
                _archiveName = _archiveName.substring(0, 7) + _nameIndex;
                file = new File(_path + _archiveName + ".tar.gz");
            }
            _tos = new TarArchiveOutputStream(new GZIPOutputStream(
                    new BufferedOutputStream(new FileOutputStream(_path + _archiveName + ".tar.gz"))));
            _tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
            _tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
        } catch (IOException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, "Error creating output stream", ex);
            System.exit(1);
        }
    }
    while (_runFlag) {
        try {
            long startTime = System.nanoTime();

            if (_selectedFiles != null) {
                if (_createArchive != false) {
                    compress(_selectedFiles, "");
                } else {
                    extract(_path, _archiveName);
                }
            } else {
                throw new GZipperException("File selection must not be null");
            }
            _elapsedTime = System.nanoTime() - startTime;

        } catch (IOException | GZipperException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, "Error compressing archive", ex);
        } finally {
            stop(); //stop thread after successful operation
        }
    }
}

From source file:com.st.maven.debian.DebianPackageMojo.java

private void fillDataTar(Config config, ArFileOutputStream output) throws MojoExecutionException {
    TarArchiveOutputStream tar = null;/*  ww w.  j  a  va  2 s . co  m*/
    try {
        tar = new TarArchiveOutputStream(new GZIPOutputStream(new ArWrapper(output)));
        tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        if (Boolean.TRUE.equals(javaServiceWrapper)) {
            byte[] daemonData = processTemplate(freemarkerConfig, config, "daemon.ftl");
            TarArchiveEntry initScript = new TarArchiveEntry("etc/init.d/" + project.getArtifactId());
            initScript.setSize(daemonData.length);
            initScript.setMode(040755);
            tar.putArchiveEntry(initScript);
            tar.write(daemonData);
            tar.closeArchiveEntry();
        }
        String packageBaseDir = "home/" + unixUserId + "/" + project.getArtifactId() + "/";
        if (fileSets != null && !fileSets.isEmpty()) {
            writeDirectory(tar, packageBaseDir);

            Collections.sort(fileSets, MappingPathComparator.INSTANCE);
            for (Fileset curPath : fileSets) {
                curPath.setTarget(packageBaseDir + curPath.getTarget());
                addRecursively(config, tar, curPath);
            }
        }

    } catch (Exception e) {
        throw new MojoExecutionException("unable to create data tar", e);
    } finally {
        IOUtils.closeQuietly(tar);
    }
}

From source file:freenet.client.async.ContainerInserter.java

/**
** OutputStream os will be close()d if this method returns successfully.
*//*from  w ww . jav a  2s .c o m*/
private String createTarBucket(OutputStream os) throws IOException {
    if (logMINOR)
        Logger.minor(this, "Create a TAR Bucket");

    TarArchiveOutputStream tarOS = new TarArchiveOutputStream(os);
    try {
        tarOS.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        TarArchiveEntry ze;

        for (ContainerElement ph : containerItems) {
            if (logMINOR)
                Logger.minor(this, "Putting into tar: " + ph + " data length " + ph.data.size() + " name "
                        + ph.targetInArchive);
            ze = new TarArchiveEntry(ph.targetInArchive);
            ze.setModTime(0);
            long size = ph.data.size();
            ze.setSize(size);
            tarOS.putArchiveEntry(ze);
            BucketTools.copyTo(ph.data, tarOS, size);
            tarOS.closeArchiveEntry();
        }
    } finally {
        tarOS.close();
    }

    return ARCHIVE_TYPE.TAR.mimeTypes[0];
}

From source file:com.dotcms.publisher.myTest.PushPublisher.java

/**
 * Compress (tar.gz) the input files to the output file
 *
 * @param files The files to compress/*from  ww  w.j  a v  a2 s  . com*/
 * @param output The resulting output file (should end in .tar.gz)
 * @param bundleRoot
 * @throws IOException
 */
private void compressFiles(Collection<File> files, File output, String bundleRoot) throws IOException {
    Logger.info(this.getClass(), "Compressing " + files.size() + " to " + output.getAbsoluteFile());
    // 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 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, ".", bundleRoot);
    }

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

From source file:gdt.data.entity.ArchiveHandler.java

/**
 * Compress the database into the tar archive file. 
 * @param  entigrator entigrator instance,
 * @param locator$ container of arguments 
 * in the string form. /*w w  w. j  av a  2 s. co m*/
 * @return true if success false otherwise.
 */
public boolean compressDatabaseToTar(Entigrator entigrator, String locator$) {
    try {
        System.out.println("ArchiveHandler:compressDatabaseToTar:locator=" + locator$);
        Properties locator = Locator.toProperties(locator$);
        archiveType$ = locator.getProperty(ARCHIVE_TYPE);
        archiveFile$ = locator.getProperty(ARCHIVE_FILE);
        String tarfile$ = archiveFile$;
        File tarfile = new File(tarfile$);
        if (!tarfile.exists())
            tarfile.createNewFile();
        TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory()
                .createArchiveOutputStream("tar", new FileOutputStream(tarfile$));
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        String entihome$ = entigrator.getEntihome();
        append(entigrator, entihome$, entihome$, aos);
        aos.close();
        return true;
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).severe(e.toString());
        return false;
    }
}