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

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

Introduction

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

Prototype

ArchiveStreamFactory

Source Link

Usage

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

/**
 * Test assembling a bag which contain a data file and a metadata file with TAR format
 * @throws IOException/*from w  ww.  j a  va2  s.c  om*/
 */
@Test
public void testAssembleTARBagNoCompression() throws IOException, CompressorException, ArchiveException {
    PackageGenerationParameters params = new PackageGenerationParameters();
    setupCommonPackageParams(params);
    params.addParam(GeneralParameterNames.ARCHIVING_FORMAT, ArchiveStreamFactory.TAR);
    params.addParam(GeneralParameterNames.CHECKSUM_ALGORITHMS, checksumAlg);
    params.addParam(GeneralParameterNames.CHECKSUM_ALGORITHMS, "sha1");

    underTest = new BagItPackageAssembler();
    underTest.init(params);

    //Reserve a URI for data file
    String filePath = "myProject/dataFile.txt";
    URI result = underTest.reserveResource(filePath, PackageResourceType.DATA);
    String expectedURI = "file:///" + packageName + "/" + "data" + "/" + filePath;
    log.debug("URI expected: " + expectedURI);
    log.debug("URI result: " + result.toString());
    assertTrue(expectedURI.equals(result.toString()));

    //Put content into the space specified by the URI
    String fileContent = "This is the data file. data data data data data data data data data data data data.";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    //Reserve a URI for metadata file
    filePath = "metadataFile.txt";
    result = underTest.reserveResource(filePath, PackageResourceType.METADATA);
    expectedURI = "file:///" + packageName + "/" + filePath;
    assertTrue(expectedURI.equals(result.toString()));

    //Put content into the space specified by the URI
    fileContent = "This is the metadata file. metadata metadata metadata metadata metadata metadata metadata .";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    Package pkg = underTest.assemblePackage();

    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR,
            pkg.serialize());

    Set<String> files = new HashSet<String>();
    ArchiveEntry entry = ais.getNextEntry();
    while (entry != null) {
        files.add(entry.getName().replace("\\", "/"));
        entry = ais.getNextEntry();
    }

    //Make sure that the packageName is set properly (packageName + contentype extension)
    String expectedPackageName = packageName.concat(".tar");
    Assert.assertEquals(expectedPackageName, pkg.getPackageName());

    // There should be 10 files: the 2 files above, bagit.txt, bag-info.txt
    // directories data and data/myProject, 2 manifest files and 2 tag-manifest files
    assertEquals(10, files.size());

    // make sure that expected files are in there
    String pathSep = "/";
    String bagFilePath = packageName + pathSep;
    assertTrue(files.contains(bagFilePath + "bagit.txt"));
    assertTrue(files.contains(bagFilePath + "bag-info.txt"));
    assertTrue(files.contains(bagFilePath + "metadataFile.txt"));
    assertTrue(files.contains(bagFilePath + "data" + pathSep));
    assertTrue(files.contains(bagFilePath + "data" + pathSep + "myProject" + pathSep));
    assertTrue(files.contains(bagFilePath + "data" + pathSep + "myProject" + pathSep + "dataFile.txt"));
}

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

/**
 * Test that the bag-info.txt file contains the parameter information passed in, including
 * parameters passed in after the initialization.
 *
 * Note that the package being generated in this case is empty, so the bag size and payload
 * oxum values will be 0.//from www .ja  va 2s  . c  om
 * @throws CompressorException
 * @throws ArchiveException
 * @throws IOException
 */
@Test
public void testBagItInfoFile() throws CompressorException, ArchiveException, IOException {
    final String paramName = "TEST_PARAMETER";
    final String paramValue = "test parameter";
    underTest.addParameter(paramName, paramValue);

    Package pkg = underTest.assemblePackage();

    CompressorInputStream cis = new CompressorStreamFactory()
            .createCompressorInputStream(CompressorStreamFactory.GZIP, pkg.serialize());
    TarArchiveInputStream ais = (TarArchiveInputStream) (new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR, cis));

    String bagInfo = "";
    TarArchiveEntry entry = ais.getNextTarEntry();
    while (entry != null) {
        if (entry.getName().contains("bag-info.txt")) {
            byte[] content = new byte[(int) entry.getSize()];
            ais.read(content, 0, (int) entry.getSize());
            bagInfo = new String(content);
            break;
        }
        entry = ais.getNextTarEntry();
    }

    // Test that expected initial parameters are present
    String expected = GeneralParameterNames.PACKAGE_NAME + ": " + packageName;
    assertTrue("Expected to find: " + expected, bagInfo.contains(expected));

    // These two values should be 0 since there is nothing in the test package this time.
    expected = BagItParameterNames.BAG_SIZE + ": 0";
    assertTrue("Expected to find: " + expected, bagInfo.contains(expected));
    expected = BagItParameterNames.PAYLOAD_OXUM + ": 0";
    assertTrue("Expected to find: " + expected, bagInfo.contains(expected));

    // Test the post-init parameter
    expected = paramName + ": " + paramValue;
    assertTrue("Expected to find: " + expected, bagInfo.contains(expected));
}

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

/**
 * Test that the assembler includes the proper manifest file (and no other manifest files)
 * @throws IOException/*from   w w w  .j a v a  2 s  .com*/
 */
@Test
public void testSingleChecksum() throws IOException, CompressorException, ArchiveException {
    // The default setup only uses md5 checksum, so we'll stick with that one

    //Reserve a URI for data file and put it in package
    String filePath = "myProject/dataFile.txt";
    URI result = underTest.reserveResource(filePath, PackageResourceType.DATA);
    String fileContent = "This is the data file. data data data data data data data data data data data data.";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    //Reserve a URI for metadata file and put it in package
    filePath = "metadataFile.txt";
    result = underTest.reserveResource(filePath, PackageResourceType.METADATA);
    fileContent = "This is the metadata file. metadata metadata metadata metadata metadata metadata metadata .";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    Package pkg = underTest.assemblePackage();

    CompressorInputStream cis = new CompressorStreamFactory()
            .createCompressorInputStream(CompressorStreamFactory.GZIP, pkg.serialize());
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    Set<String> files = new HashSet<String>();
    ArchiveEntry entry = ais.getNextEntry();
    while (entry != null) {
        files.add(entry.getName());
        entry = ais.getNextEntry();
    }

    // make sure that expected files are in there
    String bagFilePath = packageName + "/";
    assertTrue(files.contains(bagFilePath + "manifest-md5.txt"));
    assertTrue(files.contains(bagFilePath + "tagmanifest-md5.txt"));
    assertFalse(files.contains(bagFilePath + "manifest-sha1.txt"));
    assertFalse(files.contains(bagFilePath + "tagmanifest-sha1.txt"));
}

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

/**
 * Test that the assembler generates both manifest files one for sha1 and one for md5
 * @throws IOException//w  w w.j a v  a 2 s.c o m
 */
@Test
public void testMultipleChecksum() throws IOException, CompressorException, ArchiveException {
    PackageGenerationParameters params = new PackageGenerationParameters();
    setupCommonPackageParams(params);
    params.addParam(GeneralParameterNames.ARCHIVING_FORMAT, ArchiveStreamFactory.TAR);
    params.addParam(GeneralParameterNames.COMPRESSION_FORMAT, CompressorStreamFactory.GZIP);
    params.addParam(GeneralParameterNames.CHECKSUM_ALGORITHMS, checksumAlg);
    params.addParam(GeneralParameterNames.CHECKSUM_ALGORITHMS, "sha1");

    underTest = new BagItPackageAssembler();
    underTest.init(params);

    //Reserve a URI for data file and put it in package
    String filePath = "myProject/dataFile.txt";
    URI result = underTest.reserveResource(filePath, PackageResourceType.DATA);
    String fileContent = "This is the data file. data data data data data data data data data data data data.";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    //Reserve a URI for metadata file and put it in package
    filePath = "metadataFile.txt";
    result = underTest.reserveResource(filePath, PackageResourceType.METADATA);
    fileContent = "This is the metadata file. metadata metadata metadata metadata metadata metadata metadata .";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    Package pkg = underTest.assemblePackage();

    CompressorInputStream cis = new CompressorStreamFactory()
            .createCompressorInputStream(CompressorStreamFactory.GZIP, pkg.serialize());
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    Set<String> files = new HashSet<String>();
    ArchiveEntry entry = ais.getNextEntry();
    while (entry != null) {
        files.add(entry.getName());
        entry = ais.getNextEntry();
    }

    // make sure that expected files are in there
    String bagFilePath = packageName + "/";
    assertTrue(files.contains(bagFilePath + "manifest-md5.txt"));
    assertTrue(files.contains(bagFilePath + "tagmanifest-md5.txt"));
    assertTrue(files.contains(bagFilePath + "manifest-sha1.txt"));
    assertTrue(files.contains(bagFilePath + "tagmanifest-sha1.txt"));
}

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

/**
 * Test that the assembler includes a manifest file for the default checksum
 * @throws IOException//from ww  w  .j  av a 2 s  .c  om
 */
@Test
public void testDefaultChecksum() throws IOException, CompressorException, ArchiveException {
    PackageGenerationParameters params = new PackageGenerationParameters();
    setupCommonPackageParams(params);
    params.addParam(GeneralParameterNames.ARCHIVING_FORMAT, ArchiveStreamFactory.TAR);
    params.addParam(GeneralParameterNames.COMPRESSION_FORMAT, CompressorStreamFactory.GZIP);
    // No checksum parameter is given, so md5 should be used by default

    underTest = new BagItPackageAssembler();
    underTest.init(params);

    //Reserve a URI for data file and put it in package
    String filePath = "myProject/dataFile.txt";
    URI result = underTest.reserveResource(filePath, PackageResourceType.DATA);
    String fileContent = "This is the data file. data data data data data data data data data data data data.";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    //Reserve a URI for metadata file and put it in package
    filePath = "metadataFile.txt";
    result = underTest.reserveResource(filePath, PackageResourceType.METADATA);
    fileContent = "This is the metadata file. metadata metadata metadata metadata metadata metadata metadata .";
    underTest.putResource(result, new ByteArrayInputStream(fileContent.getBytes()));

    Package pkg = underTest.assemblePackage();

    CompressorInputStream cis = new CompressorStreamFactory()
            .createCompressorInputStream(CompressorStreamFactory.GZIP, pkg.serialize());
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    Set<String> files = new HashSet<String>();
    ArchiveEntry entry = ais.getNextEntry();
    while (entry != null) {
        files.add(entry.getName());
        entry = ais.getNextEntry();
    }

    // make sure that expected files are in there
    String bagFilePath = packageName + "/";
    assertTrue(files.contains(bagFilePath + "manifest-md5.txt"));
    assertTrue(files.contains(bagFilePath + "tagmanifest-md5.txt"));
    assertFalse(files.contains(bagFilePath + "manifest-sha1.txt"));
    assertFalse(files.contains(bagFilePath + "tagmanifest-sha1.txt"));
}

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

@Test
public void testValidArchiveValidCompressionAssemblesAsSpecified()
        throws CompressorException, ArchiveException, IOException {
    PackageGenerationParameters params = new PackageGenerationParameters();
    setupCommonPackageParams(params);/*  ww  w.ja v a  2  s .co m*/
    params.addParam(GeneralParameterNames.ARCHIVING_FORMAT, ArchiveStreamFactory.TAR);
    params.addParam(GeneralParameterNames.COMPRESSION_FORMAT, CompressorStreamFactory.GZIP);

    underTest = new BagItPackageAssembler();
    underTest.init(params);

    // no data files need to be added, as the bag-it files will be sufficient to test

    Package pkg = underTest.assemblePackage();

    CompressorInputStream cis = new CompressorStreamFactory()
            .createCompressorInputStream(CompressorStreamFactory.GZIP, pkg.serialize());
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    assertNotNull(ais.getNextEntry());
}

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

@Test
public void testValidArchiveNoCompressionAssemblesUncompressedArchive() throws ArchiveException, IOException {
    PackageGenerationParameters params = new PackageGenerationParameters();
    setupCommonPackageParams(params);/*from  www  .  j av a  2s  .com*/
    params.addParam(GeneralParameterNames.ARCHIVING_FORMAT, ArchiveStreamFactory.TAR);

    underTest = new BagItPackageAssembler();
    underTest.init(params);

    // no data files need to be added, as the bag-it files will be sufficient to test

    Package pkg = underTest.assemblePackage();

    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR,
            pkg.serialize());

    assertNotNull(ais.getNextEntry());
}

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

@Test
public void testNoArchiveValidCompressionAssemblesCompressedTar()
        throws CompressorException, ArchiveException, IOException {
    PackageGenerationParameters params = new PackageGenerationParameters();
    setupCommonPackageParams(params);/*from w ww . jav  a 2  s  .com*/
    params.addParam(GeneralParameterNames.COMPRESSION_FORMAT, CompressorStreamFactory.GZIP);

    underTest = new BagItPackageAssembler();
    underTest.init(params);

    // no data files need to be added, as the bag-it files will be sufficient to test

    Package pkg = underTest.assemblePackage();

    CompressorInputStream cis = new CompressorStreamFactory()
            .createCompressorInputStream(CompressorStreamFactory.GZIP, pkg.serialize());
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    assertNotNull(ais.getNextEntry());
}

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

@Test
public void testNoArchiveNoCompressionAssemblesUncompressedTar() throws ArchiveException, IOException {
    PackageGenerationParameters params = new PackageGenerationParameters();
    setupCommonPackageParams(params);/*  w  w  w.j ava2s. c  om*/

    underTest = new BagItPackageAssembler();
    underTest.init(params);

    // no data files need to be added, as the bag-it files will be sufficient to test

    Package pkg = underTest.assemblePackage();

    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR,
            pkg.serialize());

    assertNotNull(ais.getNextEntry());
}

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

/**
 * Extract contents of an archive./*from   w w w  .j a va2  s.c o  m*/
 * 
 * @param dest_dir
 *            Destination to write archive content.
 * @param is
 *            Archive file.
 * @return Name of package base directory in dest_dir
 * @throws ArchiveException if there is an error creating the ArchiveInputStream
 * @throws IOException  if there is more than one package root
 */
protected String extract(File dest_dir, InputStream is) throws ArchiveException, IOException {
    // Apache commons compress requires buffered input streams

    if (!is.markSupported()) {
        is = new BufferedInputStream(is);
    }

    // If file is compressed, uncompress.

    try {
        is = new CompressorStreamFactory().createCompressorInputStream(is);
    } catch (CompressorException e) {
    }

    // Extract entries from archive

    if (!is.markSupported()) {
        is = new BufferedInputStream(is);
    }

    String archive_base = null;

    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(is);
    ArchiveEntry entry;

    while ((entry = ais.getNextEntry()) != null) {
        File file = extract(dest_dir, entry, ais);

        String root = get_root_file_name(file);

        if (archive_base == null) {
            archive_base = root;
        } else if (!archive_base.equals(root)) {
            throw new IOException("Package has more than one base directory.");
        }
    }

    return archive_base;
}