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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the underlying OutputStream.

Usage

From source file:algorithm.TarPackaging.java

@Override
public File encapsulate(File carrier, List<File> payloadFiles) throws IOException {
    String outputName = getOutputFileName(carrier);
    int dotIndex = outputName.lastIndexOf('.');
    if (dotIndex > 0) {
        if (compression()) {
            if (gzip()) {
                outputName = outputName.substring(0, dotIndex) + ".tgz";
            } else if (bzip()) {
                outputName = outputName.substring(0, dotIndex) + ".tbz2";
            }/*from w  ww. ja v a  2  s  .co  m*/
        } else {
            outputName = outputName.substring(0, dotIndex) + ".tar";
        }
    }
    File tarFile = new File(outputName);
    FileOutputStream outputStream = new FileOutputStream(tarFile);
    if (compression()) {
        if (gzip()) {
            TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(
                    new GzipCompressorOutputStream(new BufferedOutputStream(outputStream)));
            archiveFile(carrier, tarOutputStream);
            for (File payload : payloadFiles) {
                archiveFile(payload, tarOutputStream);
            }
            tarOutputStream.close();
        } else if (bzip()) {
            TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(
                    new BZip2CompressorOutputStream(new BufferedOutputStream(outputStream)));
            archiveFile(carrier, tarOutputStream);
            for (File payload : payloadFiles) {
                archiveFile(payload, tarOutputStream);
            }
            tarOutputStream.close();
        }
    } else {
        TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(
                new BufferedOutputStream(outputStream));
        archiveFile(carrier, tarOutputStream);
        for (File payload : payloadFiles) {
            archiveFile(payload, tarOutputStream);
        }
        tarOutputStream.close();
    }
    outputStream.close();
    return tarFile;
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

/**
  * @param basePath/*from  ww w .j  a va2 s  . co m*/
  * @param tarPath
  * @param filePaths
  * @throws java.io.IOException
  */
public static void tar(String basePath, String tarPath, Map<String, String> filePaths) throws IOException {
    BufferedInputStream origin = null;
    TarArchiveOutputStream out = null;
    FileOutputStream dest = null;
    try {
        dest = new FileOutputStream(tarPath);
        out = new TarArchiveOutputStream(new BufferedOutputStream(dest));
        byte data[] = new byte[BUFFER];
        for (Map.Entry<String, String> entryFile : filePaths.entrySet()) {
            String filename = entryFile.getKey();
            String filePath = entryFile.getValue();
            System.out.println("Adding: " + filename + " => " + filePath);
            FileInputStream fi = new FileInputStream(basePath + filePath);
            origin = new BufferedInputStream(fi, BUFFER);
            TarArchiveEntry entry = new TarArchiveEntry(filename);
            out.putArchiveEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (origin != null)
            origin.close();
        if (out != null)
            out.close();
        if (dest != null)
            dest.close();
    }
}

From source file:com.espringtran.compressor4j.processor.TarProcessor.java

/**
 * Compress data//w  w w. java  2 s  .  com
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TarArchiveOutputStream aos = new TarArchiveOutputStream(baos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.espringtran.compressor4j.processor.TarBz2Processor.java

/**
 * Compress data/*from w  w w. j ava  2s  . c o m*/
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(baos);
    TarArchiveOutputStream aos = new TarArchiveOutputStream(cos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.espringtran.compressor4j.processor.TarGzProcessor.java

/**
 * Compress data//from   ww w  .j  ava  2s.c om
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GzipCompressorOutputStream cos = new GzipCompressorOutputStream(baos);
    TarArchiveOutputStream aos = new TarArchiveOutputStream(cos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.espringtran.compressor4j.processor.XzProcessor.java

/**
 * Compress data//from w w  w.  ja  va2 s  .com
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XZCompressorOutputStream cos = new XZCompressorOutputStream(baos);
    TarArchiveOutputStream aos = new TarArchiveOutputStream(cos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.francetelecom.clara.cloud.mvn.consumer.maven.MavenDeployer.java

private File populateTgzArchive(File archive, List<FileRef> fileset) throws IOException {
    archive.getParentFile().mkdirs();/*from  w w  w . jav  a  2 s.  c  o m*/
    CompressorOutputStream zip = new GzipCompressorOutputStream(new FileOutputStream(archive));
    TarArchiveOutputStream tar = new TarArchiveOutputStream(zip);
    for (FileRef fileRef : fileset) {
        TarArchiveEntry entry = new TarArchiveEntry(new File(fileRef.getRelativeLocation()));
        byte[] bytes = fileRef.getContent().getBytes();
        entry.setSize(bytes.length);
        tar.putArchiveEntry(entry);
        tar.write(bytes);
        tar.closeArchiveEntry();
    }
    tar.close();
    return archive;
}

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   w  ww.  ja va 2 s.  c  o m
 * @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:edu.mit.lib.bagit.Filler.java

private void deflate(OutputStream out, String format) throws IOException {
    switch (format) {
    case "zip":
        ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(out));
        fillZip(base, base.getName(), zout);
        zout.close();// w w w .  j ava  2  s .com
        break;
    case "tgz":
        TarArchiveOutputStream tout = new TarArchiveOutputStream(
                new BufferedOutputStream(new GzipCompressorOutputStream(out)));
        fillArchive(base, base.getName(), tout);
        tout.close();
        break;
    default:
        throw new IOException("Unsupported package format: " + format);
    }
}

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

public InputStream readArchiveProfileFrom(Path profilePath) throws IOException {

    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());
        tarArchive.putArchiveEntry(tarEntry);
        IOUtils.copy(Files.newInputStream(path), tarArchive);
        tarArchive.closeArchiveEntry();/*from   w w  w .java 2 s .  c o  m*/
    }

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

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