Example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextTarEntry

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextTarEntry

Introduction

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

Prototype

public TarArchiveEntry getNextTarEntry() throws IOException 

Source Link

Document

Get the next entry in this tar archive.

Usage

From source file:org.codehaus.plexus.archiver.tar.TarUnArchiver.java

protected void execute(File sourceFile, File destDirectory) throws ArchiverException {
    TarArchiveInputStream tis = null;
    try {//from w  w w . j  a  v  a 2s  . com
        getLogger().info("Expanding: " + sourceFile + " into " + destDirectory);
        TarFile tarFile = new TarFile(sourceFile);
        tis = new TarArchiveInputStream(
                decompress(compression, sourceFile, new BufferedInputStream(new FileInputStream(sourceFile))));
        TarArchiveEntry te;
        while ((te = tis.getNextTarEntry()) != null) {
            TarResource fileInfo = new TarResource(tarFile, te);
            if (isSelected(te.getName(), fileInfo)) {
                final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
                extractFile(sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
                        te.getMode() != 0 ? te.getMode() : null, symlinkDestination);
            }

        }
        getLogger().debug("expand complete");

    } catch (IOException ioe) {
        throw new ArchiverException("Error while expanding " + sourceFile.getAbsolutePath(), ioe);
    } finally {
        IOUtil.close(tis);
    }
}

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./* w w w.j  av  a  2  s .c o  m*/
 * @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.ui.util.TarPackageExtractor.java

@Override
protected List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName)
        throws UnpackException {
    final TarArchiveInputStream tarInStream;

    if (!TarArchiveInputStream.class.isAssignableFrom(packageInputStream.getClass())) {
        tarInStream = new TarArchiveInputStream(packageInputStream);
    } else {// w  w  w.ja v  a 2  s  .  c om
        tarInStream = (TarArchiveInputStream) packageInputStream;
    }

    List<File> files = new ArrayList<File>();
    try {
        TarArchiveEntry entry = tarInStream.getNextTarEntry();
        //Get next tar entry returns null when there are no more entries
        while (entry != null) {
            //Directories are automatically handled by the base class so we can ignore them in this class.
            if (!entry.isDirectory()) {
                File entryFile = new File(packageDir, entry.getName());
                if (entryFile != null) {
                    List<File> savedFiles = saveExtractedFile(entryFile, tarInStream);
                    files.addAll(savedFiles);
                }
            }
            entry = tarInStream.getNextTarEntry();
        }

        tarInStream.close();
    } catch (IOException e) {
        final String msg = "Error processing TarArchiveInputStream: " + e.getMessage();
        log.error(msg, e);
        throw new UnpackException(msg, e);
    }

    return files;
}

From source file:org.dcm4chee.storage.tar.TarContainerProvider.java

@Override
public InputStream seekEntry(RetrieveContext ctx, String name, String entryName, InputStream in)
        throws IOException {
    TarArchiveInputStream tar = new TarArchiveInputStream(in);
    String checksumEntry = container.getChecksumEntry();
    TarArchiveEntry nextEntry;/*from   w  w  w.  ja  va2 s .co m*/
    while ((nextEntry = tar.getNextTarEntry()) != null) {
        String nextEntryName = nextEntry.getName();
        if (nextEntry.isDirectory() || nextEntryName.equals(checksumEntry))
            continue;

        if (nextEntryName.equals(entryName))
            return tar;
    }
    throw new ObjectNotFoundException(ctx.getStorageSystem().getStorageSystemPath(), name, entryName);
}

From source file:org.dcm4chee.storage.tar.TarContainerProvider.java

private TarArchiveEntry skipDirectoryEntries(TarArchiveInputStream tar) throws IOException {
    for (TarArchiveEntry entry = tar.getNextTarEntry(); entry != null; entry = tar.getNextTarEntry()) {
        if (!entry.isDirectory())
            return entry;
    }//from  w ww.ja  v  a 2  s.  c  om
    return null;
}

From source file:org.dcm4chee.storage.test.unit.tar.TarContainerProviderTest.java

private static void assertTarEquals(TarArchiveInputStream expectedTar, TarArchiveInputStream actualTar)
        throws IOException {
    TarArchiveEntry expected;//from www. j  a va2s.  com
    TarArchiveEntry actual;
    while ((expected = expectedTar.getNextTarEntry()) != null) {
        actual = actualTar.getNextTarEntry();
        assertNotNull(actual);
        assertTarEntryEquals(expected, actual);
    }
}

From source file:org.dspace.pack.bagit.Bag.java

public void inflate(InputStream in, String fmt) throws IOException {
    if (filled) {
        throw new IllegalStateException("Cannot inflate filled bag");
    }/*from  w w  w  . j  a v a 2s . c  o  m*/
    if ("zip".equals(fmt)) {
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry entry = null;
        while ((entry = zin.getNextEntry()) != null) {
            File outFile = new File(baseDir.getParent(), entry.getName());
            outFile.getParentFile().mkdirs();
            FileOutputStream fout = new FileOutputStream(outFile);
            Utils.copy(zin, fout);
            fout.close();
        }
        zin.close();
    } else if ("tgz".equals(fmt)) {
        TarArchiveInputStream tin = new TarArchiveInputStream(new GzipCompressorInputStream(in));
        TarArchiveEntry entry = null;
        while ((entry = tin.getNextTarEntry()) != null) {
            File outFile = new File(baseDir.getParent(), entry.getName());
            outFile.getParentFile().mkdirs();
            FileOutputStream fout = new FileOutputStream(outFile);
            Utils.copy(tin, fout);
            fout.close();
        }
        tin.close();
    }
    filled = true;
}

From source file:org.eclipse.che.commons.lang.TarUtils.java

public static void untar(InputStream in, File targetDir) throws IOException {
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
    byte[] b = new byte[BUF_SIZE];
    TarArchiveEntry tarEntry;/*from w  ww.j  a v a  2  s.co m*/
    while ((tarEntry = tarIn.getNextTarEntry()) != null) {
        final File file = new File(targetDir, tarEntry.getName());
        if (tarEntry.isDirectory()) {
            if (!file.mkdirs()) {
                throw new IOException("Unable to create folder " + file.getAbsolutePath());
            }
        } else {
            final File parent = file.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create folder " + parent.getAbsolutePath());
                }
            }
            try (FileOutputStream fos = new FileOutputStream(file)) {
                int r;
                while ((r = tarIn.read(b)) != -1) {
                    fos.write(b, 0, r);
                }
            }
        }
    }
}

From source file:org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.TarFile.java

private static boolean skipToEntry(TarArchiveInputStream entryStream, TarArchiveEntry entry)
        throws IOException {
    TarArchiveEntry e = entryStream.getNextTarEntry();
    while (e != null) {
        if (e.equals(entry)) {
            return true;
        }//ww w .ja  v  a2 s.  c om

        e = entryStream.getNextTarEntry();
    }

    return false;
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

private Map<String, TarArchiveEntry> getTarEntries() throws IOException, FileNotFoundException {
    TarArchiveInputStream tarStream = new TarArchiveInputStream(
            new GzipCompressorInputStream(new FileInputStream(tarGzArchive)));
    Map<String, TarArchiveEntry> entries = new HashMap<String, TarArchiveEntry>();
    try {//from w  w  w . ja v  a  2 s  . c  o m
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarStream.getNextTarEntry()) != null) {
            entries.put(tarEntry.getName(), tarEntry);
        }
    } finally {
        tarStream.close();
    }
    return entries;
}