Example usage for org.apache.commons.compress.compressors.xz XZCompressorInputStream XZCompressorInputStream

List of usage examples for org.apache.commons.compress.compressors.xz XZCompressorInputStream XZCompressorInputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.xz XZCompressorInputStream XZCompressorInputStream.

Prototype

public XZCompressorInputStream(final InputStream inputStream, final boolean decompressConcatenated)
        throws IOException 

Source Link

Document

Creates a new input stream that decompresses XZ-compressed data from the specified input stream.

Usage

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

/**
 * Utility file open function - handles compressed and un-compressed files.
 * /*from   www . ja va2  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 + "'.");
    }
}

From source file:io.druid.java.util.common.CompressionUtils.java

/**
 * Decompress an input stream from a file, based on the filename.
 *///from www  . j a  v  a  2s. c o  m
public static InputStream decompress(final InputStream in, final String fileName) throws IOException {
    if (fileName.endsWith(GZ_SUFFIX)) {
        return gzipInputStream(in);
    } else if (fileName.endsWith(BZ2_SUFFIX)) {
        return new BZip2CompressorInputStream(in, true);
    } else if (fileName.endsWith(XZ_SUFFIX)) {
        return new XZCompressorInputStream(in, true);
    } else if (fileName.endsWith(ZIP_SUFFIX)) {
        // This reads the first file in the archive.
        final ZipInputStream zipIn = new ZipInputStream(in, StandardCharsets.UTF_8);
        try {
            final ZipEntry nextEntry = zipIn.getNextEntry();
            if (nextEntry == null) {
                zipIn.close();

                // No files in the archive - return an empty stream.
                return new ByteArrayInputStream(new byte[0]);
            }
            return zipIn;
        } catch (IOException e) {
            try {
                zipIn.close();
            } catch (IOException e2) {
                e.addSuppressed(e2);
            }
            throw e;
        }
    } else {
        return in;
    }
}

From source file:org.apache.ant.compress.util.XZStreamFactory.java

/**
 * @param stream the stream to read from, should be buffered
 * @param       decompressConcatenated//from  w w w .  ja  v a  2s  .  c o  m
 *                          if true, decompress until the end of the
 *                          input; if false, stop after the first
 *                          stream
 */
public CompressorInputStream getCompressorStream(InputStream stream, boolean decompressConcatenated)
        throws IOException {
    return new XZCompressorInputStream(stream, decompressConcatenated);
}

From source file:org.apache.druid.java.util.common.CompressionUtils.java

/**
 * Decompress an input stream from a file, based on the filename.
 *///from   w  ww . jav  a  2s  .  co m
public static InputStream decompress(final InputStream in, final String fileName) throws IOException {
    if (fileName.endsWith(GZ_SUFFIX)) {
        return gzipInputStream(in);
    } else if (fileName.endsWith(BZ2_SUFFIX)) {
        return new BZip2CompressorInputStream(in, true);
    } else if (fileName.endsWith(XZ_SUFFIX)) {
        return new XZCompressorInputStream(in, true);
    } else if (fileName.endsWith(SNAPPY_SUFFIX)) {
        return new FramedSnappyCompressorInputStream(in);
    } else if (fileName.endsWith(ZSTD_SUFFIX)) {
        return new ZstdCompressorInputStream(in);
    } else if (fileName.endsWith(ZIP_SUFFIX)) {
        // This reads the first file in the archive.
        final ZipInputStream zipIn = new ZipInputStream(in, StandardCharsets.UTF_8);
        try {
            final ZipEntry nextEntry = zipIn.getNextEntry();
            if (nextEntry == null) {
                zipIn.close();

                // No files in the archive - return an empty stream.
                return new ByteArrayInputStream(new byte[0]);
            }
            return zipIn;
        } catch (IOException e) {
            try {
                zipIn.close();
            } catch (IOException e2) {
                e.addSuppressed(e2);
            }
            throw e;
        }
    } else {
        return in;
    }
}

From source file:org.apache.flink.api.common.io.compression.XZInputStreamFactory.java

@Override
public XZCompressorInputStream create(InputStream in) throws IOException {
    return new XZCompressorInputStream(in, true);
}

From source file:org.apache.marmotta.loader.core.MarmottaLoader.java

/**
 * Load data from the reader given as first argument into the handler given as second argument.
 *
 * @param file     file to read the data from; in case a compression format is not explicitly given, the method will
 *                 try to decide from the file name if the file is in a compressed format
 * @param handler  handler to add the data to
 * @param format   format to use for creating the parser or null for auto-detection
 * @param compression compression format to use, or null for auto-detection (see formats in org.apache.commons.compress.compressors.CompressorStreamFactory)
 * @throws RDFParseException/*  ww w  .  j  av  a  2 s .c  o m*/
 * @throws IOException
 */
public void loadFile(File file, LoaderHandler handler, RDFFormat format, String compression)
        throws RDFParseException, IOException {
    log.info("loading file {} ...", file);

    CompressorStreamFactory cf = new CompressorStreamFactory();
    cf.setDecompressConcatenated(true);

    // detect the file compression
    String detectedCompression = detectCompression(file);
    if (compression == null) {
        if (detectedCompression != null) {
            log.info("using auto-detected compression ({})", detectedCompression);
            compression = detectedCompression;
        }
    } else {
        if (detectedCompression != null && !compression.equals(detectedCompression)) {
            log.warn("user-specified compression ({}) overrides auto-detected compression ({})", compression,
                    detectedCompression);
        } else {
            log.info("using user-specified compression ({})", compression);
        }
    }

    // detect the file format
    RDFFormat detectedFormat = Rio.getParserFormatForFileName(uncompressedName(file));
    if (format == null) {
        if (detectedFormat != null) {
            log.info("using auto-detected format ({})", detectedFormat.getName());
            format = detectedFormat;
        } else {
            throw new RDFParseException("could not detect input format of file " + file);
        }
    } else {
        if (detectedFormat != null && !format.equals(detectedFormat)) {
            log.warn("user-specified format ({}) overrides auto-detected format ({})", format.getName(),
                    detectedFormat.getName());
        }
    }

    // create input stream from file and wrap in compressor stream
    InputStream in;
    InputStream fin = new BufferedInputStream(new FileInputStream(file));
    try {
        if (compression != null) {
            if (CompressorStreamFactory.GZIP.equalsIgnoreCase(compression)) {
                in = new GzipCompressorInputStream(fin, true);
            } else if (CompressorStreamFactory.BZIP2.equalsIgnoreCase(compression)) {
                in = new BZip2CompressorInputStream(fin, true);
            } else if (CompressorStreamFactory.XZ.equalsIgnoreCase(compression)) {
                in = new XZCompressorInputStream(fin, true);
            } else {
                // does not honour decompressConcatenated
                in = cf.createCompressorInputStream(compression, fin);
            }
        } else {
            in = cf.createCompressorInputStream(fin);
        }
    } catch (CompressorException ex) {
        log.info("no compression detected, using plain input stream");
        in = fin;
    }

    // load using the input stream
    load(in, handler, format);
}

From source file:org.apache.marmotta.loader.core.MarmottaLoader.java

public void loadArchive(File archive, LoaderHandler handler, RDFFormat format)
        throws RDFParseException, IOException, ArchiveException {
    log.info("loading files in archive {} ...", archive);

    if (archive.exists() && archive.canRead()) {

        if (archive.getName().endsWith("7z")) {
            log.info("auto-detected archive format: 7Z");

            final SevenZFile sevenZFile = new SevenZFile(archive);

            try {
                SevenZArchiveEntry entry;
                while ((entry = sevenZFile.getNextEntry()) != null) {

                    if (!entry.isDirectory()) {
                        log.info("loading entry {} ...", entry.getName());

                        // detect the file format
                        RDFFormat detectedFormat = Rio.getParserFormatForFileName(entry.getName());
                        if (format == null) {
                            if (detectedFormat != null) {
                                log.info("auto-detected entry format: {}", detectedFormat.getName());
                                format = detectedFormat;
                            } else {
                                throw new RDFParseException(
                                        "could not detect input format of entry " + entry.getName());
                            }//from w  w w .  j  ava2 s. c o m
                        } else {
                            if (detectedFormat != null && !format.equals(detectedFormat)) {
                                log.warn("user-specified entry format ({}) overrides auto-detected format ({})",
                                        format.getName(), detectedFormat.getName());
                            } else {
                                log.info("user-specified entry format: {}", format.getName());
                            }
                        }

                        load(new InputStream() {
                            @Override
                            public int read() throws IOException {
                                return sevenZFile.read();
                            }

                            @Override
                            public int read(byte[] b) throws IOException {
                                return sevenZFile.read(b);
                            }

                            @Override
                            public int read(byte[] b, int off, int len) throws IOException {
                                return sevenZFile.read(b, off, len);
                            }
                        }, handler, format);
                    }
                }
            } finally {
                sevenZFile.close();
            }

        } else {
            InputStream in;

            String archiveCompression = detectCompression(archive);
            InputStream fin = new BufferedInputStream(new FileInputStream(archive));
            if (archiveCompression != null) {
                if (CompressorStreamFactory.GZIP.equalsIgnoreCase(archiveCompression)) {
                    log.info("auto-detected archive compression: GZIP");
                    in = new GzipCompressorInputStream(fin, true);
                } else if (CompressorStreamFactory.BZIP2.equalsIgnoreCase(archiveCompression)) {
                    log.info("auto-detected archive compression: BZIP2");
                    in = new BZip2CompressorInputStream(fin, true);
                } else if (CompressorStreamFactory.XZ.equalsIgnoreCase(archiveCompression)) {
                    log.info("auto-detected archive compression: XZ");
                    in = new XZCompressorInputStream(fin, true);
                } else {
                    in = fin;
                }
            } else {
                in = fin;
            }

            ArchiveInputStream zipStream = new ArchiveStreamFactory()
                    .createArchiveInputStream(new BufferedInputStream(in));
            logArchiveType(zipStream);

            ArchiveEntry entry;
            while ((entry = zipStream.getNextEntry()) != null) {

                if (!entry.isDirectory()) {
                    log.info("loading entry {} ...", entry.getName());

                    // detect the file format
                    RDFFormat detectedFormat = Rio.getParserFormatForFileName(entry.getName());
                    if (format == null) {
                        if (detectedFormat != null) {
                            log.info("auto-detected entry format: {}", detectedFormat.getName());
                            format = detectedFormat;
                        } else {
                            throw new RDFParseException(
                                    "could not detect input format of entry " + entry.getName());
                        }
                    } else {
                        if (detectedFormat != null && !format.equals(detectedFormat)) {
                            log.warn("user-specified entry format ({}) overrides auto-detected format ({})",
                                    format.getName(), detectedFormat.getName());
                        } else {
                            log.info("user-specified entry format: {}", format.getName());
                        }
                    }

                    load(zipStream, handler, format);
                }
            }
        }

    } else {
        throw new RDFParseException(
                "could not load files from archive " + archive + ": it does not exist or is not readable");
    }

}

From source file:org.apache.tika.parser.pkg.TikaCompressorStreamFactory.java

public CompressorInputStream createCompressorInputStream(final String name, final InputStream in,
        final boolean actualDecompressConcatenated) throws CompressorException {
    if (name == null || in == null) {
        throw new IllegalArgumentException("Compressor name and stream must not be null.");
    }//from  www .ja  va  2  s  .c  om

    try {

        if (GZIP.equalsIgnoreCase(name)) {
            return new GzipCompressorInputStream(in, actualDecompressConcatenated);
        }

        if (BZIP2.equalsIgnoreCase(name)) {
            return new BZip2CompressorInputStream(in, actualDecompressConcatenated);
        }

        if (XZ.equalsIgnoreCase(name)) {
            if (!XZUtils.isXZCompressionAvailable()) {
                throw new CompressorException("XZ compression is not available.");
            }
            return new XZCompressorInputStream(in, actualDecompressConcatenated);
        }

        if (LZMA.equalsIgnoreCase(name)) {
            if (!LZMAUtils.isLZMACompressionAvailable()) {
                throw new CompressorException("LZMA compression is not available");
            }
            try {
                return new SaferLZMACompressorInputStream(in);
            } catch (MemoryLimitException e) {
                throw new CompressorException("MemoryLimitException: " + e.getMessage(), e);
            }
        }

        if (PACK200.equalsIgnoreCase(name)) {
            return new Pack200CompressorInputStream(in);
        }

        if (SNAPPY_RAW.equalsIgnoreCase(name)) {
            return new SnappyCompressorInputStream(in);
        }

        if (SNAPPY_FRAMED.equalsIgnoreCase(name)) {
            return new FramedSnappyCompressorInputStream(in);
        }

        if (Z.equalsIgnoreCase(name)) {
            try {
                return new SaferZCompressorInputStream(in);
            } catch (TikaRuntimeMemoryLimitException e) {
                throw new CompressorException("MemoryLimitException: " + e.getMessage(), e);
            }
        }

        if (DEFLATE.equalsIgnoreCase(name)) {
            return new DeflateCompressorInputStream(in);
        }
        /*
        not currently supported
        if (LZ4_BLOCK.equalsIgnoreCase(name)) {
            return new BlockLZ4CompressorInputStream(in);
        }
                
        if (LZ4_FRAMED.equalsIgnoreCase(name)) {
            return new FramedLZ4CompressorInputStream(in, actualDecompressConcatenated);
        }
         */

    } catch (final IOException e) {
        throw new CompressorException("Could not create CompressorInputStream.", e);
    }

    final CompressorStreamProvider compressorStreamProvider = getCompressorInputStreamProviders()
            .get(toKey(name));
    if (compressorStreamProvider != null) {
        return compressorStreamProvider.createCompressorInputStream(name, in, actualDecompressConcatenated);
    }

    throw new CompressorException("Compressor: " + name + " not found.");
}