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:lk.score.androphsy.main.NewCase.java

private void compressFiles(ArrayList<File> list, File outFile) throws IOException {
    FileOutputStream fos = new FileOutputStream(outFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    TarArchiveOutputStream taos = new TarArchiveOutputStream(gos);

    taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    for (File f : list) {
        addFileToCompression(taos, f, "");
    }/*from  w  w  w  . j a  va  2 s.  co m*/
    taos.close();
    fos.close();
    // gos.close();
    // bos.close();
}

From source file:gov.nih.nci.ncicb.tcga.dcc.dam.processors.FilePackager.java

TarArchiveOutputStream makeTarGzOutputStream(final File archiveFile) throws IOException {
    final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(archiveFile)));
    tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tarArchiveOutputStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);

    return tarArchiveOutputStream;
}

From source file:com.facebook.buck.util.unarchive.UntarTest.java

/**
 * cleanDirectoriesExactly asserts that OVERWRITE_AND_CLEAN_DIRECTORIES removes exactly the files
 * in any subdirectory of a directory entry in the tar archive.
 *
 * <p>This behavior supports unarchiving a Buck cache from multiple archives, each containing a
 * part.//  www .j  av a  2  s .  c  o m
 *
 * <p>Explanations:
 * <li>BUCK isn't removed because it's not in a subdirectory of a directory entry in the archive.
 * <li>buck-out/gen/pkg1/rule1.jar isn't removed, even though buck-out/gen/pkg1/rule2.jar is in
 *     the archive, because there's no directory entry for buck-out/gen/pkg1/.
 * <li>buck-out/gen/pkg1/rule2#foo/lib.so, however, is removed because the archive contains the
 *     directory buck-out/gen/pkg1/rule2#foo/
 */
@Test
public void cleanDirectoriesExactly() throws Exception {
    Path archive = filesystem.resolve("archive.tar");
    Path outDir = filesystem.getRootPath();

    List<String> toLeave = ImmutableList.of("BUCK", "buck-out/gen/pkg1/rule1.jar",
            "buck-out/gen/pkg1/rule1#foo/lib.so", "buck-out/gen/pkg2/rule.jar",
            "buck-out/gen/pkg2/rule#foo/lib.so");
    List<String> toDelete = ImmutableList.of("buck-out/gen/pkg1/rule2#foo/lib.so");
    for (String s : concat(toDelete, toLeave)) {
        Path path = filesystem.resolve(s);
        filesystem.createParentDirs(path);
        filesystem.writeContentsToPath("", path);
    }

    // Write test archive.
    try (TarArchiveOutputStream stream = new TarArchiveOutputStream(
            new BufferedOutputStream(filesystem.newFileOutputStream(archive)))) {
        stream.putArchiveEntry(new TarArchiveEntry("buck-out/gen/pkg1/rule2#foo/"));
        stream.putArchiveEntry(new TarArchiveEntry("buck-out/gen/pkg1/rule2.jar"));
        stream.closeArchiveEntry();
    }

    // Untar test archive.
    Untar.tarUnarchiver().extractArchive(archive, filesystem, outDir, Optional.empty(),
            ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES);

    // Assert expected file existence.
    for (String s : toDelete) {
        Assert.assertFalse(String.format("Expected file %s to be deleted, but it wasn't", s),
                filesystem.exists(filesystem.resolve(s)));
    }
    for (String s : toLeave) {
        Assert.assertTrue(String.format("Expected file %s to not be deleted, but it was", s),
                filesystem.exists(filesystem.resolve(s)));
    }
}

From source file:frameworks.Masken.java

public static void CreateTarGZ(String dirPath, String tarGzPath)

{

    FileOutputStream fOut = null;
    try {/*from www.j  a va2s .co m*/
        System.out.println(new File(".").getAbsolutePath());
        //   dirPath = "parent/childDirToCompress/";
        //   tarGzPath = "archive.tar.gz";
        fOut = new FileOutputStream(new File(tarGzPath));
        BufferedOutputStream bOut = new BufferedOutputStream(fOut);
        GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(bOut);
        TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);
        addFileToTarGz(tOut, dirPath, "");
        tOut.finish();
        tOut.close();
        gzOut.close();
        bOut.close();
        fOut.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            fOut.close();
        } catch (IOException ex) {
            Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:frameworks.Masken.java

public static void createTarGzip(String dirPath, String tarGzPath) throws IOException {
    File inputDirectoryPath = new File(dirPath);
    File outputFile = new File(tarGzPath);

    try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            GzipCompressorOutputStream gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
            TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(gzipOutputStream)) {

        tarArchiveOutputStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
        tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        List<File> files = new ArrayList<>(FileUtils.listFiles(inputDirectoryPath,
                new RegexFileFilter("^(.*?)"), DirectoryFileFilter.DIRECTORY));

        for (int i = 0; i < files.size(); i++) {

            File currentFile = files.get(i);
            if (!currentFile.getName().contains(".tgz")) {
                String relativeFilePath = new File(inputDirectoryPath.toURI()).toURI()
                        .relativize(new File(currentFile.getAbsolutePath()).toURI()).getPath();

                TarArchiveEntry tarEntry = new TarArchiveEntry(currentFile, relativeFilePath);
                tarEntry.setSize(currentFile.length());

                tarArchiveOutputStream.putArchiveEntry(tarEntry);
                FileInputStream in = new FileInputStream(currentFile);
                //tarArchiveOutputStream.write(IOUtils.toByteArray(new FileInputStream(currentFile)));
                tarArchiveOutputStream.write(IOUtils.toByteArray(in));

                tarArchiveOutputStream.closeArchiveEntry();
                in.close();/*  w  ww  . ja  va  2  s  . c  o m*/
            }
        }
        tarArchiveOutputStream.close();

    }
}

From source file:gov.noaa.pfel.coastwatch.util.FileVisitorDNLS.java

/** 
 * This makes a .tgz or .tar.gz file.//from   w ww .  jav a  2 s.  co  m
 *
 * @param tResultName is the full result file name, usually 
 *   the name of the dir being archived, and ending in .tgz or .tar.gz.
 */
public static void makeTgz(String tDir, String tFileNameRegex, boolean tRecursive, String tPathRegex,
        String tResultName) throws Exception {
    TarArchiveOutputStream tar = null;
    String outerDir = File2.getDirectory(tDir.substring(0, tDir.length() - 1));
    tar = new TarArchiveOutputStream(
            new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tResultName))));

    // Add data to out and flush stream
    Table filesTable = oneStep(tDir, tFileNameRegex, tRecursive, tPathRegex, false); //tDirectoriesToo
    StringArray directoryPA = (StringArray) filesTable.getColumn(DIRECTORY);
    StringArray namePA = (StringArray) filesTable.getColumn(NAME);
    LongArray lastModifiedPA = (LongArray) filesTable.getColumn(LASTMODIFIED);
    LongArray sizePA = (LongArray) filesTable.getColumn(SIZE);
    byte buffer[] = new byte[32768];
    int nBytes;
    for (int fi = 0; fi < namePA.size(); fi++) {
        String fullName = directoryPA.get(fi) + namePA.get(fi);
        TarArchiveEntry entry = new TarArchiveEntry(new File(fullName.substring(outerDir.length())));
        entry.setSize(sizePA.get(fi));
        entry.setModTime(lastModifiedPA.get(fi));
        tar.putArchiveEntry(entry);
        FileInputStream fis = new FileInputStream(fullName);
        while ((nBytes = fis.read(buffer)) > 0)
            tar.write(buffer, 0, nBytes);
        fis.close();
        tar.closeArchiveEntry();
    }
    tar.close();
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildEngineTest.java

private static void writeEntriesToArchive(Path file, ImmutableMap<Path, String> entries,
        ImmutableList<Path> directories) throws IOException {
    try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(file));
            OutputStream z = new ZstdCompressorOutputStream(o);
            TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
        archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (Map.Entry<Path, String> mapEntry : entries.entrySet()) {
            TarArchiveEntry e = new TarArchiveEntry(mapEntry.getKey().toString());
            e.setModTime(ZipConstants.getFakeTime());
            byte[] bytes = mapEntry.getValue().getBytes(UTF_8);
            e.setSize(bytes.length);// w ww  . j a v a  2s.  c o  m
            archive.putArchiveEntry(e);
            archive.write(bytes);
            archive.closeArchiveEntry();
        }
        for (Path dir : directories) {
            TarArchiveEntry e = new TarArchiveEntry(dir.toString() + "/");
            e.setModTime(ZipConstants.getFakeTime());
        }
        archive.finish();
    }
}

From source file:net.zyuiop.remoteworldloader.utils.CompressionUtils.java

private static void writeZipFile(File directoryToZip, List<File> fileList, File target) {
    try {//from   w  w  w.  j  av a2  s .  c  o  m
        FileOutputStream fos = new FileOutputStream(target);
        GzipParameters parameters = new GzipParameters();
        parameters.setCompressionLevel(9);
        GzipCompressorOutputStream gzip = new GzipCompressorOutputStream(fos, parameters);
        TarArchiveOutputStream stream = new TarArchiveOutputStream(gzip);

        for (File file : fileList) {
            if (!file.isDirectory()) { // we only zip files, not directories
                int retryCount = 0;
                while (retryCount < 10) {
                    try {
                        addToZip(directoryToZip, file, stream);
                        break;
                    } catch (Exception e) {
                        retryCount++;
                        if (retryCount > 9)
                            e.printStackTrace();
                    }
                }
            }
        }

        stream.close();
        gzip.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.camel.dataformat.tarfile.TarFileDataFormat.java

@Override
public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
    String filename = exchange.getIn().getHeader(FILE_NAME, String.class);
    Long filelength = exchange.getIn().getHeader(FILE_LENGTH, Long.class);
    if (filename != null) {
        filename = new File(filename).getName(); // remove any path elements
    } else {//  www .j a v  a  2 s.  c o  m
        // generate the file name as the camel file component would do
        filename = StringHelper.sanitize(exchange.getIn().getMessageId());
    }

    TarArchiveOutputStream tos = new TarArchiveOutputStream(stream);
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);

    InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, graph);
    if (filelength == null) {
        filelength = new Long(is.available());
    }

    TarArchiveEntry entry = new TarArchiveEntry(filename);
    entry.setSize(filelength);
    tos.putArchiveEntry(entry);

    try {
        IOHelper.copy(is, tos);
    } finally {
        tos.closeArchiveEntry();
        IOHelper.close(is, tos);
    }

    String newFilename = filename + ".tar";
    exchange.getOut().setHeader(FILE_NAME, newFilename);
}

From source file:org.apache.camel.dataformat.tarfile.TarFileDataFormatTest.java

private static byte[] getTaredText(String entryName) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(TEXT.getBytes("UTF-8"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TarArchiveOutputStream tos = new TarArchiveOutputStream(baos);
    try {/*from   w  w w .j  a  v  a 2s  .  c o  m*/
        TarArchiveEntry entry = new TarArchiveEntry(entryName);
        entry.setSize(bais.available());
        tos.putArchiveEntry(entry);
        IOHelper.copy(bais, tos);
    } finally {
        tos.closeArchiveEntry();
        IOHelper.close(bais, tos);
    }
    return baos.toByteArray();
}