Example usage for java.util.zip GZIPInputStream read

List of usage examples for java.util.zip GZIPInputStream read

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream read.

Prototype

public int read(byte[] buf, int off, int len) throws IOException 

Source Link

Document

Reads uncompressed data into an array of bytes.

Usage

From source file:org.ofbiz.webapp.event.RestEventHandler.java

/** 
* ? /*from  w  w  w .ja va  2  s  .  c  om*/
*  
* @param is 
* @param os 
* @throws Exception 
*/
public static void decompress(InputStream is, OutputStream os) throws Exception {

    GZIPInputStream gis = new GZIPInputStream(is);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = gis.read(data, 0, BUFFER)) != -1) {
        os.write(data, 0, count);
    }

    gis.close();
}

From source file:com.krawler.common.util.ByteUtil.java

/**
 * uncompress the supplied data using GZIPInputStream and return the
 * uncompressed data.//w  ww .  j  av  a  2 s  . co  m
 * 
 * @param data
 *            data to uncompress
 * @return uncompressed data
 */
public static byte[] uncompress(byte[] data) throws IOException {
    // TODO: optimize, this makes my head hurt
    ByteArrayOutputStream baos = null;
    ByteArrayInputStream bais = null;
    GZIPInputStream gis = null;
    try {
        int estimatedResultSize = data.length * 3;
        baos = new ByteArrayOutputStream(estimatedResultSize);
        bais = new ByteArrayInputStream(data);
        byte[] buffer = new byte[8192];
        gis = new GZIPInputStream(bais);

        int numRead;
        while ((numRead = gis.read(buffer, 0, buffer.length)) != -1) {
            baos.write(buffer, 0, numRead);
        }
        return baos.toByteArray();
    } finally {
        if (gis != null)
            gis.close();
        else if (bais != null)
            bais.close();
        if (baos != null)
            baos.close();
    }
}

From source file:nl.nn.adapterframework.util.Misc.java

public static byte[] gunzip(byte[] input) throws DataFormatException, IOException {

    // Create an expandable byte array to hold the decompressed data
    ByteArrayInputStream bis = new ByteArrayInputStream(input);
    GZIPInputStream gz = new GZIPInputStream(bis);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (gz.available() > 0) {
        int count = gz.read(buf, 0, 1024);
        if (count > 0) {
            bos.write(buf, 0, count);//w  ww.j av  a 2  s  . c  o m
        }
    }
    bos.close();

    // Get the decompressed data
    return bos.toByteArray();
}

From source file:org.exist.xquery.modules.compression.UnGZipFunction.java

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // is there some data to unGZip?
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;

    BinaryValue bin = (BinaryValue) args[0].itemAt(0);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // ungzip the data
    try {/* w  w  w  . ja  v  a2 s  . co  m*/
        GZIPInputStream gzis = new GZIPInputStream(bin.getInputStream());
        int size;
        byte[] b = new byte[4096];
        while ((size = gzis.read(b, 0, 4096)) != -1) {
            baos.write(b, 0, size);
        }
        baos.flush();
        baos.close();
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(),
                new ByteArrayInputStream(baos.toByteArray()));
    } catch (IOException ioe) {
        throw new XPathException(this, ioe.getMessage());
    }
}

From source file:org.opensextant.util.FileUtility.java

/**
 *
 * @param filepath//  w  ww .  j  a  v a  2 s.c  o  m
 *            path to file
 * @return text buffer, UTF-8 decoded
 * @throws IOException
 *             on error
 */
public static String readGzipFile(String filepath) throws IOException {
    if (filepath == null) {
        return null;
    }

    final FileInputStream instream = new FileInputStream(filepath);
    final GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(instream), ioBufferSize);

    final byte[] inputBytes = new byte[ioBufferSize];
    final StringBuilder buf = new StringBuilder();

    int readcount = 0;
    while ((readcount = gzin.read(inputBytes, 0, ioBufferSize)) != -1) {
        buf.append(new String(inputBytes, 0, readcount, default_encoding));
    }
    instream.close();
    gzin.close();

    return buf.toString();

}

From source file:org.apache.hadoop.hbase.rest.TestGzipFilter.java

@Test
public void testGzipFilter() throws Exception {
    String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream os = new GZIPOutputStream(bos);
    os.write(VALUE_1);//from w  ww. j av a 2 s  .c  o m
    os.close();
    byte[] value_1_gzip = bos.toByteArray();

    // input side filter

    Header[] headers = new Header[2];
    headers[0] = new Header("Content-Type", Constants.MIMETYPE_BINARY);
    headers[1] = new Header("Content-Encoding", "gzip");
    Response response = client.put(path, headers, value_1_gzip);
    assertEquals(response.getCode(), 200);

    HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
    Get get = new Get(Bytes.toBytes(ROW_1));
    get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
    Result result = table.get(get);
    byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
    assertNotNull(value);
    assertTrue(Bytes.equals(value, VALUE_1));

    // output side filter

    headers[0] = new Header("Accept", Constants.MIMETYPE_BINARY);
    headers[1] = new Header("Accept-Encoding", "gzip");
    response = client.get(path, headers);
    assertEquals(response.getCode(), 200);
    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
    GZIPInputStream is = new GZIPInputStream(bis);
    value = new byte[VALUE_1.length];
    is.read(value, 0, VALUE_1.length);
    assertTrue(Bytes.equals(value, VALUE_1));
    is.close();
    table.close();

    testScannerResultCodes();
}

From source file:android.tether.system.WebserviceTask.java

public boolean downloadBluetoothModule(String downloadFileUrl, String destinationFilename) {
    if (android.os.Environment.getExternalStorageState()
            .equals(android.os.Environment.MEDIA_MOUNTED) == false) {
        return false;
    }//from w  ww.ja v  a2  s.c  o m
    File bluetoothDir = new File(BLUETOOTH_FILEPATH);
    if (bluetoothDir.exists() == false) {
        bluetoothDir.mkdirs();
    }
    if (this.downloadFile(downloadFileUrl, "", destinationFilename) == true) {
        try {
            FileOutputStream out = new FileOutputStream(new File(destinationFilename.replace(".gz", "")));
            FileInputStream fis = new FileInputStream(destinationFilename);
            GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(fis));
            int count;
            byte buf[] = new byte[8192];
            while ((count = gzin.read(buf, 0, 8192)) != -1) {
                //System.out.write(x);
                out.write(buf, 0, count);
            }
            out.flush();
            out.close();
            gzin.close();
            File inputFile = new File(destinationFilename);
            inputFile.delete();
        } catch (IOException e) {
            return false;
        }
        return true;
    } else
        return false;
}

From source file:net.yacy.kelondro.util.FileUtils.java

/**
 * This function determines if a byte array is gzip compressed and uncompress it
 *
 * @param source properly gzip compressed byte array
 * @return uncompressed byte array//from  ww w .  j  a v  a  2s .c o m
 * @throws IOException
 */
public static byte[] uncompressGZipArray(byte[] source) throws IOException {
    if (source == null) {
        return null;
    }

    // support of gzipped data (requested by roland)
    /* "Bitwise OR of signed byte value
     *
     * [...] Values loaded from a byte array are sign extended to 32 bits before
     * any any bitwise operations are performed on the value. Thus, if b[0]
     * contains the value 0xff, and x is initially 0, then the code ((x <<
     * 8) | b[0]) will sign extend 0xff to get 0xffffffff, and thus give the
     * value 0xffffffff as the result. [...]" findbugs description of BIT_IOR_OF_SIGNED_BYTE
     */
    if ((source.length > 1) && (((source[1] << 8) | (source[0] & 0xff)) == GZIPInputStream.GZIP_MAGIC)) {
        System.out.println("DEBUG: uncompressGZipArray - uncompressing source");
        try {
            final ByteArrayInputStream byteInput = new ByteArrayInputStream(source);
            final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(source.length / 5);
            final GZIPInputStream zippedContent = new GZIPInputStream(byteInput);
            final byte[] data = new byte[1024];
            int read = 0;

            // reading gzip file and store it uncompressed
            while ((read = zippedContent.read(data, 0, 1024)) != -1) {
                byteOutput.write(data, 0, read);
            }
            zippedContent.close();
            byteOutput.close();

            source = byteOutput.toByteArray();
        } catch (final Exception e) {
            if (!e.getMessage().equals("Not in GZIP format")) {
                throw new IOException(e.getMessage());
            }
        }
    }

    return source;
}

From source file:net.yacy.document.parser.gzipParser.java

@Override
public Document[] parse(final DigestURL location, final String mimeType, final String charset,
        final VocabularyScraper scraper, final int timezoneOffset, final InputStream source)
        throws Parser.Failure, InterruptedException {

    File tempFile = null;/* w  w  w .  j  a  v a  2  s  . co m*/
    Document maindoc = null;
    try {
        int read = 0;
        final byte[] data = new byte[1024];

        final GZIPInputStream zippedContent = new GZIPInputStream(source);

        tempFile = File.createTempFile("gunzip", "tmp");

        // creating a temp file to store the uncompressed data
        final FileOutputStream out = new FileOutputStream(tempFile);

        // reading gzip file and store it uncompressed
        while ((read = zippedContent.read(data, 0, 1024)) != -1) {
            out.write(data, 0, read);
        }
        zippedContent.close();
        out.close();
        final String filename = location.getFileName();
        // create maindoc for this gzip container, register with supplied url & mime
        maindoc = new Document(location, mimeType, charset, this, null, null,
                AbstractParser.singleList(
                        filename.isEmpty() ? location.toTokens() : MultiProtocolURL.unescape(filename)), // title
                null, null, null, null, 0.0d, 0.0d, (Object) null, null, null, null, false, new Date());
        // creating a new parser class to parse the unzipped content
        final String contentfilename = GzipUtils.getUncompressedFilename(location.getFileName());
        final String mime = TextParser.mimeOf(DigestURL.getFileExtension(contentfilename));
        Document[] docs = TextParser.parseSource(location, mime, null, scraper, timezoneOffset, 999, tempFile);
        if (docs != null)
            maindoc.addSubDocuments(docs);
    } catch (final Exception e) {
        if (e instanceof InterruptedException)
            throw (InterruptedException) e;
        if (e instanceof Parser.Failure)
            throw (Parser.Failure) e;

        throw new Parser.Failure("Unexpected error while parsing gzip file. " + e.getMessage(), location);
    } finally {
        if (tempFile != null)
            FileUtils.deletedelete(tempFile);
    }
    return maindoc == null ? null : new Document[] { maindoc };
}