Example usage for org.apache.commons.compress.archivers.sevenz SevenZFileGiveStream getNextEntry

List of usage examples for org.apache.commons.compress.archivers.sevenz SevenZFileGiveStream getNextEntry

Introduction

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

Prototype

public SevenZArchiveEntry getNextEntry() throws IOException 

Source Link

Usage

From source file:com.vividsolutions.jump.io.CompressedFile.java

/**
 * Utility file open function - handles compressed and un-compressed files.
 * //from  w ww  .ja  v  a 2 s .  c o m
 * @param filePath
 *          name of the file to search for.
 * @param compressedEntry
 *          name of the compressed file.
 * 
 *          <p>
 *          If compressedEntry = null, opens a FileInputStream on filePath
 *          </p>
 * 
 *          <p>
 *          If filePath ends in ".zip" - opens the compressed Zip and
 *          looks for the file called compressedEntry
 *          </p>
 * 
 *          <p>
 *          If filePath ends in ".gz" - opens the compressed .gz file.
 *          </p>
 */
public static InputStream openFile(String filePath, String compressedEntry) throws IOException {

    System.out.println(filePath + " extract " + compressedEntry);

    if (isTar(filePath)) {
        InputStream is = new BufferedInputStream(new FileInputStream(filePath));
        if (filePath.toLowerCase().endsWith("gz"))
            is = new GzipCompressorInputStream(is, true);
        else if (filePath.matches("(?i).*bz2?"))
            is = new BZip2CompressorInputStream(is, true);
        else if (filePath.matches("(?i).*xz"))
            is = new XZCompressorInputStream(is, true);

        TarArchiveInputStream tis = new TarArchiveInputStream(is);
        if (compressedEntry == null)
            return is;

        TarArchiveEntry entry;
        while ((entry = tis.getNextTarEntry()) != null) {
            if (entry.getName().equals(compressedEntry))
                return tis;
        }

        throw createArchiveFNFE(filePath, compressedEntry);
    }

    else if (compressedEntry == null && isGZip(filePath)) {
        // gz compressed file -- easy
        InputStream is = new BufferedInputStream(new FileInputStream(filePath));
        return new GzipCompressorInputStream(is, true);
    }

    else if (compressedEntry == null && isBZip(filePath)) {
        // bz compressed file -- easy
        InputStream is = new BufferedInputStream(new FileInputStream(filePath));
        return new BZip2CompressorInputStream(is, true);
        //return new org.itadaki.bzip2.BZip2InputStream(is, false);
    }

    else if (compressedEntry == null && isXZ(filePath)) {
        InputStream is = new BufferedInputStream(new FileInputStream(filePath));
        return new XZCompressorInputStream(is, true);
    }

    else if (compressedEntry != null && isZip(filePath)) {

        ZipFile zipFile = new ZipFile(filePath);
        ZipArchiveEntry zipEntry = zipFile.getEntry(compressedEntry);

        if (zipEntry != null)
            return zipFile.getInputStream(zipEntry);

        throw createArchiveFNFE(filePath, compressedEntry);
    }

    else if (compressedEntry != null && isSevenZ(filePath)) {

        SevenZFileGiveStream sevenZFile = new SevenZFileGiveStream(new File(filePath));
        SevenZArchiveEntry entry;
        while ((entry = sevenZFile.getNextEntry()) != null) {
            if (entry.getName().equals(compressedEntry))
                return sevenZFile.getCurrentEntryInputStream();
        }
        throw createArchiveFNFE(filePath, compressedEntry);
    }
    // return plain stream if no compressedEntry
    else if (compressedEntry == null) {
        return new FileInputStream(filePath);
    }

    else {
        throw new IOException("Couldn't determine compressed file type for file '" + filePath
                + "' supposedly containing '" + compressedEntry + "'.");
    }
}