Example usage for org.apache.commons.compress.archivers ArchiveStreamFactory TAR

List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory TAR

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveStreamFactory TAR.

Prototype

String TAR

To view the source code for org.apache.commons.compress.archivers ArchiveStreamFactory TAR.

Click Source Link

Document

Constant used to identify the TAR archive format.

Usage

From source file:consulo.cold.runner.execute.target.artifacts.Generator.java

private ArchiveEntryWrapper createEntry(String type, String name, ArchiveEntry tempEntry) {
    name = replaceBuildDirectory(name);/*  w w w .ja  v  a 2s .c o m*/

    if (type.equals(ArchiveStreamFactory.TAR)) {
        return new ArchiveEntryWrapper.Tar(name, tempEntry);
    }
    return new ArchiveEntryWrapper.Zip(name);
}

From source file:consulo.cold.runner.execute.target.artifacts.Generator.java

private OutputStream createOutputStream(String type, String prefix) throws Exception {
    final String fileName;
    if (type.equals(ArchiveStreamFactory.ZIP)) {
        fileName = prefix + ".zip";
    } else if (type.equals(ArchiveStreamFactory.TAR)) {
        fileName = prefix + ".tar.gz";
    } else {//  www . j  a v  a2s.c  o  m
        throw new IllegalArgumentException(type);
    }

    File child = new File(myTargetDir, fileName);

    final OutputStream outputStream = new FileOutputStream(child);
    if (type.equals(ArchiveStreamFactory.TAR)) {
        return new GzipCompressorOutputStream(outputStream);
    }
    return outputStream;
}

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

@Override
public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
    if (usingIterator) {
        return new TarIterator(exchange.getIn(), stream);
    } else {/* w w  w  .  j  ava 2  s  .  c o  m*/
        InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
        TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory()
                .createArchiveInputStream(ArchiveStreamFactory.TAR, new BufferedInputStream(is));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            TarArchiveEntry entry = tis.getNextTarEntry();
            if (entry != null) {
                exchange.getOut().setHeader(FILE_NAME, entry.getName());
                IOHelper.copy(tis, baos);
            }

            entry = tis.getNextTarEntry();
            if (entry != null) {
                throw new IllegalStateException("Tar file has more than 1 entry.");
            }

            return baos.toByteArray();

        } finally {
            IOHelper.close(tis, baos);
        }
    }
}

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

public TarIterator(Message inputMessage, InputStream inputStream) {
    this.inputMessage = inputMessage;
    //InputStream inputStream = inputMessage.getBody(InputStream.class);

    if (inputStream instanceof TarArchiveInputStream) {
        tarInputStream = (TarArchiveInputStream) inputStream;
    } else {/*  w  ww  .  j a  v a  2 s . co m*/
        try {
            ArchiveInputStream input = new ArchiveStreamFactory()
                    .createArchiveInputStream(ArchiveStreamFactory.TAR, new BufferedInputStream(inputStream));
            tarInputStream = (TarArchiveInputStream) input;
        } catch (ArchiveException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    parent = null;
}

From source file:org.apache.camel.processor.aggregate.tarfile.AggregationStrategyWithFilenameHeaderTest.java

@Test
public void testSplitter() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:aggregateToTarEntry");
    mock.expectedMessageCount(1);/*from   www  . j  a  v  a  2 s.c  o m*/

    template.setDefaultEndpointUri("direct:start");
    template.sendBodyAndHeader("foo", Exchange.FILE_NAME, FILE_NAMES.get(0));
    template.sendBodyAndHeader("bar", Exchange.FILE_NAME, FILE_NAMES.get(1));
    assertMockEndpointsSatisfied();

    Thread.sleep(500);

    File[] files = new File("target/out").listFiles();
    assertTrue(files != null);
    assertTrue("Should be a file in target/out directory", files.length > 0);

    File resultFile = files[0];

    final TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR,
                    new BufferedInputStream(new FileInputStream(resultFile)));
    try {
        int fileCount = 0;
        for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; entry = tis.getNextTarEntry()) {
            fileCount++;
            assertTrue("Tar entry file name should be on of: " + FILE_NAMES,
                    FILE_NAMES.contains(entry.getName()));
        }
        assertEquals("Tar file should contain " + FILE_NAMES.size() + " files", FILE_NAMES.size(), fileCount);
    } finally {
        IOHelper.close(tis);
    }
}

From source file:org.apache.camel.processor.aggregate.tarfile.TarAggregationStrategy.java

private static void addFileToTar(File source, File file, String fileName) throws IOException, ArchiveException {
    File tmpTar = File.createTempFile(source.getName(), null);
    tmpTar.delete();/*from  w w  w  .  j  a v  a  2 s.c o  m*/
    if (!source.renameTo(tmpTar)) {
        throw new IOException("Could not make temp file (" + source.getName() + ")");
    }

    TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR, new FileInputStream(tmpTar));
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);

    InputStream in = new FileInputStream(file);

    // copy the existing entries    
    ArchiveEntry nextEntry;
    while ((nextEntry = tin.getNextEntry()) != null) {
        tos.putArchiveEntry(nextEntry);
        IOUtils.copy(tin, tos);
        tos.closeArchiveEntry();
    }

    // Add the new entry
    TarArchiveEntry entry = new TarArchiveEntry(fileName == null ? file.getName() : fileName);
    entry.setSize(file.length());
    tos.putArchiveEntry(entry);
    IOUtils.copy(in, tos);
    tos.closeArchiveEntry();

    IOHelper.close(in);
    IOHelper.close(tin);
    IOHelper.close(tos);
}

From source file:org.apache.camel.processor.aggregate.tarfile.TarAggregationStrategy.java

private static void addEntryToTar(File source, String entryName, byte[] buffer, int length)
        throws IOException, ArchiveException {
    File tmpTar = File.createTempFile(source.getName(), null);
    tmpTar.delete();/*from   w w w.  j av  a2  s.c  om*/
    if (!source.renameTo(tmpTar)) {
        throw new IOException("Cannot create temp file: " + source.getName());
    }
    TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR, new FileInputStream(tmpTar));
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);

    // copy the existing entries    
    ArchiveEntry nextEntry;
    while ((nextEntry = tin.getNextEntry()) != null) {
        tos.putArchiveEntry(nextEntry);
        IOUtils.copy(tin, tos);
        tos.closeArchiveEntry();
    }

    // Create new entry
    TarArchiveEntry entry = new TarArchiveEntry(entryName);
    entry.setSize(length);
    tos.putArchiveEntry(entry);
    tos.write(buffer, 0, length);
    tos.closeArchiveEntry();

    IOHelper.close(tin);
    IOHelper.close(tos);
}

From source file:org.dataconservancy.packaging.tool.impl.BOREMPackageGeneratorTest.java

/**
 * Tests generating a well formed package, with all required parameters and the following options:
 * <ul>// www  .  j a  v  a 2 s  .  c om
 *     <li>checksum alg: md5</li>
 *     <li>compression-format: gz</li>
 *     <li>archiving-format: not specified</li>
 * </ul>
 *
 * <p/>
 *
 * Expects the de-compressed, deserialized package to contain:
 * <ul>
 *     <li>bag-info.txt file: Besides the input parameters, bag-info.txt file is expected to contain reference
 *     to the ReM of the whole package, expressed in PKG-ORE-REM parameter</li>
 *     <li>bagit.txt file</li>
 *     <li>manifest-<checksumalg>.txt files</checksumalg></li>
 *     <li>tagmanifest-<checksumalg>.txt files</checksumalg></li>
 *     <li>data/ folder</li>
 *     <li>payload files in data/ folder</li>
 *     <li>ORE-REM folder</li>
 *     <li>description files in ORE-REM/folder</li>
 * </ul>
 *
 *
 * @throws CompressorException
 * @throws ArchiveException
 * @throws IOException
 */
@Test
public void testGeneratingAGoodPackage() throws CompressorException, ArchiveException, IOException {
    params.addParam(GeneralParameterNames.PACKAGE_FORMAT_ID, PackagingFormat.BOREM.toString());
    params.addParam(GeneralParameterNames.PACKAGE_NAME, packageName);
    params.addParam(GeneralParameterNames.PACKAGE_LOCATION, packageLocationName);
    params.addParam(GeneralParameterNames.PACKAGE_STAGING_LOCATION, packageStagingLocationName);
    params.addParam(BagItParameterNames.BAGIT_PROFILE_ID, bagItProfileId);
    params.addParam(BagItParameterNames.CONTACT_NAME, contactName);
    params.addParam(BagItParameterNames.CONTACT_EMAIL, contactEmail);
    params.addParam(BagItParameterNames.CONTACT_PHONE, contactPhone);
    params.addParam(GeneralParameterNames.CHECKSUM_ALGORITHMS, checksumAlg);
    params.addParam(BagItParameterNames.COMPRESSION_FORMAT, compressionFormat);
    params.addParam(BagItParameterNames.PKG_BAG_DIR, packageName);
    // params.addParam(GeneralParameterNames.CONTENT_ROOT_LOCATION, pkgBagDir);
    params.addParam(GeneralParameterNames.CONTENT_ROOT_LOCATION, contentRootLocation);
    Package resultedPackage = underTest.generatePackage(desc, params);

    //Decompress and de archive files
    CompressorInputStream cis = new CompressorStreamFactory()
            .createCompressorInputStream(CompressorStreamFactory.GZIP, resultedPackage.serialize());
    TarArchiveInputStream ais = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    //get files from archive
    Set<String> files = new HashSet<String>();
    ArchiveEntry entry = ais.getNextEntry();
    while (entry != null) {
        files.add(entry.getName().replace("\\", "/"));
        if (entry.getName().equals(packageName + "/bag-info.txt") && ais.canReadEntryData(entry)) {
            verifyBagInfoContent(ais);
        }
        if (entry.getName()
                .equals(packageName + "/data/ProjectOne/Collection One/DataItem One/" + dataFileOneName)) {
            compareDataFile(ais, pathToFileOne);
        }
        if (entry.getName()
                .equals(packageName + "/data/ProjectOne/Collection One/DataItem One/" + dataFileTwoName)) {
            compareDataFile(ais, pathToFileTwo);
        }
        entry = ais.getNextEntry();
    }
    assertTrue(files.contains(packageName + "/bag-info.txt"));
    assertTrue(files.contains(packageName + "/bagit.txt"));
    assertTrue(files.contains(packageName + "/tagmanifest-md5.txt"));
    assertTrue(files.contains(packageName + "/manifest-md5.txt"));
    assertTrue(files.contains(packageName + "/data/"));
    assertTrue(files.contains(packageName + "/data/ProjectOne/Collection One/DataItem One/" + dataFileOneName));
    assertTrue(files.contains(packageName + "/data/ProjectOne/Collection One/DataItem One/" + dataFileTwoName));
    assertTrue(files.contains(packageName + "/ORE-REM/"));

    assertTrue(SupportedMimeTypes.getMimeType(compressionFormat).contains(resultedPackage.getContentType()));

}

From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java

private void addFilesToArchive(ArchiveOutputStream taos, File file) throws IOException {
    // Create an entry for the file
    //taos.putArchiveEntry(new TarArchiveEntry(file, file.getParentFile().toURI().relativize(file.toURI()).toString()));
    switch (archivingFormat) {
    case ArchiveStreamFactory.TAR:
        taos.putArchiveEntry(new TarArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;/*from w  w  w .  java  2  s  .  com*/
    case ArchiveStreamFactory.ZIP:
        taos.putArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    case ArchiveStreamFactory.JAR:
        taos.putArchiveEntry(new JarArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))));
        break;
    case ArchiveStreamFactory.AR:
        taos.putArchiveEntry(new ArArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    case ArchiveStreamFactory.CPIO:
        taos.putArchiveEntry(new CpioArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    }
    if (file.isFile()) {
        // Add the file to the archive
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        // close the archive entry
        taos.closeArchiveEntry();
        // go through all the files in the directory and using recursion, add them to the archive
        for (File childFile : file.listFiles()) {
            addFilesToArchive(taos, childFile);
        }
    }
}

From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java

private void validateArchivingFormat() {
    if (!archivingFormat.equals(ArchiveStreamFactory.CPIO) && !archivingFormat.equals(ArchiveStreamFactory.TAR)
            && !archivingFormat.equals(ArchiveStreamFactory.JAR)
            && !archivingFormat.equals(ArchiveStreamFactory.AR)
            && !archivingFormat.equals(ArchiveStreamFactory.ZIP) && !archivingFormat.equals("exploded")) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_ASSEMBLER_INVALID_PARAMS,
                String.format(/* ww w. j a v a  2  s  . com*/
                        "Specified archiving format <%s> is not supported. The supported archiving "
                                + "formats are: %s, %s, %s, %s, %s, %s.",
                        archivingFormat, ArchiveStreamFactory.AR, ArchiveStreamFactory.CPIO,
                        ArchiveStreamFactory.JAR, ArchiveStreamFactory.TAR, ArchiveStreamFactory.ZIP,
                        "exploded"));
    }
}