Example usage for org.apache.commons.compress.compressors CompressorInputStream read

List of usage examples for org.apache.commons.compress.compressors CompressorInputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:darks.codec.wrap.zip.CommonsCompress.java

/**
 * {@inheritDoc}// w w  w.ja v a  2s. co m
 */
@Override
public void uncompress(InputStream input, OutputStream out) throws Exception {
    CompressorInputStream cin = null;
    try {
        cin = factory.createCompressorInputStream(type, input);
        byte[] buf = new byte[1024];
        int len;
        while ((len = cin.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.flush();
    } catch (CompressorException e) {
        throw new Exception("Fail to decompress data by commons compress. Cause " + e.getMessage(), e);
    } finally {
        IoHelper.closeIO(cin);
    }

}

From source file:edu.utah.bmi.ibiomes.io.IBIOMESFileReader.java

/**
 * Read first bytes of a file (for format identification) after decompression (if applicable)
 * @param nBytes NUmber of bytes to read
 * @return nBytes first bytes as a hexadecimal String
 * @throws IOException/*from w ww.  j  a v  a  2 s. c om*/
 * @throws CompressorException 
 */
public String readFirstBytesDecompressed(int nBytes) throws IOException, CompressorException {
    FileInputStream fin = null;
    if (!isCompressed)
        return this.hex;
    else {
        try {
            CompressorInputStream stream = getInputStreamForCompressedFile(hex);
            byte[] bytes = new byte[nBytes];
            stream.read(bytes);
            return Utils.getHex(bytes);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fin != null) {
                    fin.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.jboss.as.plugin.common.Files.java

private static File extract(final File file) throws IOException {
    final File f = dropExtension(file);
    final File tempFile = File.createTempFile(dropExtension(f).getName(), getExtension(f));
    tempFile.deleteOnExit();// w w  w  .  j a v a 2  s. com
    BufferedInputStream in = null;
    FileOutputStream out = null;
    CompressorInputStream compressorIn = null;
    try {
        in = new BufferedInputStream(new FileInputStream(file));
        out = new FileOutputStream(tempFile);
        compressorIn = new CompressorStreamFactory().createCompressorInputStream(in);
        final byte[] buffer = new byte[1024];
        int i;
        while ((i = compressorIn.read(buffer)) != -1) {
            out.write(buffer, 0, i);
        }
    } catch (CompressorException e) {
        throw new IOException(e);
    } finally {
        IoUtils.safeClose(in);
        IoUtils.safeClose(out);
        IoUtils.safeClose(compressorIn);
    }
    return tempFile;
}

From source file:org.owasp.dependencycheck.analyzer.ArchiveAnalyzer.java

/**
 * Decompresses a file.//w  w  w  .j  a  v  a 2  s. c  om
 *
 * @param inputStream the compressed file
 * @param outputFile the location to write the decompressed file
 * @throws ArchiveExtractionException thrown if there is an exception decompressing the file
 */
private void decompressFile(CompressorInputStream inputStream, File outputFile)
        throws ArchiveExtractionException {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(outputFile);
        final byte[] buffer = new byte[BUFFER_SIZE];
        int n = 0;
        while (-1 != (n = inputStream.read(buffer))) {
            out.write(buffer, 0, n);
        }
    } catch (FileNotFoundException ex) {
        LOGGER.log(Level.FINE, null, ex);
        throw new ArchiveExtractionException(ex);
    } catch (IOException ex) {
        LOGGER.log(Level.FINE, null, ex);
        throw new ArchiveExtractionException(ex);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                LOGGER.log(Level.FINEST, null, ex);
            }
        }
    }
}