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

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

Introduction

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

Prototype

public boolean canReadEntryData(ArchiveEntry ae) 

Source Link

Document

Whether this class is able to read the given entry.

Usage

From source file:de.flapdoodle.embedmongo.extract.TgzExtractor.java

@Override
public void extract(RuntimeConfig runtime, File source, File destination, Pattern file) throws IOException {

    IProgressListener progressListener = runtime.getProgressListener();
    String progressLabel = "Extract " + source;
    progressListener.start(progressLabel);

    FileInputStream fin = new FileInputStream(source);
    BufferedInputStream in = new BufferedInputStream(fin);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);

    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
    try {//from   ww  w . j a va 2s .  c om
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            if (file.matcher(entry.getName()).matches()) {
                //               System.out.println("File: " + entry.getName());
                if (tarIn.canReadEntryData(entry)) {
                    //                  System.out.println("Can Read: " + entry.getName());
                    long size = entry.getSize();
                    Files.write(tarIn, size, destination);
                    destination.setExecutable(true);
                    //                  System.out.println("DONE");
                    progressListener.done(progressLabel);
                }
                break;

            } else {
                //               System.out.println("SKIP File: " + entry.getName());
            }
        }

    } finally {
        tarIn.close();
        gzIn.close();
    }
}

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>/*from  w ww  .  j a  v a2s .  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.xenmaster.setup.debian.Bootstrapper.java

protected boolean downloadNetbootFiles() {
    if (System.currentTimeMillis() - lastChecked < 50 * 60 * 1000) {
        return false;
    }//from ww w .ja  v  a2s  . c  o m

    File localVersionFile = new File(Settings.getInstance().getString("StorePath") + "/netboot/version");
    int localVersion = -1;
    try (FileInputStream fis = new FileInputStream(localVersionFile)) {
        localVersion = Integer.parseInt(IOUtils.toString(fis));
    } catch (IOException | NumberFormatException ex) {
        Logger.getLogger(getClass()).error("Failed to retrieve local version file", ex);
    }

    int remoteVersion = -1;
    try {
        remoteVersion = Integer.parseInt(IOUtils.toString(XenMasterSite.getFileAsStream("/netboot/version")));
    } catch (IOException | NumberFormatException ex) {
        Logger.getLogger(getClass()).error("Failed to retrieve remote version file", ex);
    }

    lastChecked = System.currentTimeMillis();

    if (localVersion < remoteVersion) {
        Logger.getLogger(getClass())
                .info("New version " + remoteVersion + " found. Please hold while downloading netboot data");
        try {
            TarArchiveInputStream tais = new TarArchiveInputStream(
                    new GZIPInputStream(XenMasterSite.getFileAsStream("/netboot/netboot.tar.gz")));
            TarArchiveEntry tae = null;
            FileOutputStream fos = null;
            while ((tae = tais.getNextTarEntry()) != null) {
                if (tais.canReadEntryData(tae)) {
                    String target = Settings.getInstance().getString("StorePath") + "/" + tae.getName();

                    if (tae.isSymbolicLink()) {
                        Path targetFile = FileSystems.getDefault().getPath(tae.getLinkName());
                        Path linkName = FileSystems.getDefault().getPath(target);

                        // Link might already have been written as null file
                        if (targetFile.toFile().exists()) {
                            targetFile.toFile().delete();
                        }

                        Files.createSymbolicLink(linkName, targetFile);
                        Logger.getLogger(getClass()).info(
                                "Created sym link " + linkName.toString() + " -> " + targetFile.toString());
                    } else if (tae.isDirectory()) {
                        new File(target).mkdir();

                        Logger.getLogger(getClass()).info("Created dir " + target);
                    } else if (tae.isFile()) {
                        fos = new FileOutputStream(target);
                        byte[] b = new byte[1024];
                        int curPos = 0;
                        while (tais.available() > 0) {
                            tais.read(b, curPos, 1024);
                            fos.write(b, curPos, 1024);
                        }

                        fos.flush();
                        fos.close();

                        Logger.getLogger(getClass()).info("Wrote file " + target);
                    }
                }
            }

            tais.close();
            // Write local version
            IOUtils.write("" + remoteVersion, new FileOutputStream(localVersionFile));
        } catch (IOException ex) {
            Logger.getLogger(getClass()).error("Failed to download netboot files", ex);
        }
    } else {
        return false;
    }

    return true;
}