Example usage for java.util.zip GZIPInputStream GZIPInputStream

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

Introduction

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

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:com.rapleaf.hank.storage.HdfsPartitionRemoteFileOps.java

@Override
public InputStream getInputStream(String remoteRelativePath) throws IOException {
    InputStream inputStream = fs.open(new Path(getAbsoluteRemotePath(remoteRelativePath)));
    if (compressionCodec == null) {
        return inputStream;
    } else {//w w  w .  ja v  a  2 s.  c o m
        switch (compressionCodec) {
        case GZIP:
            return new GZIPInputStream(inputStream);
        default:
            throw new RuntimeException("Compression codec not supported: " + compressionCodec);
        }
    }
}

From source file:com.jimdo.graylog.net.Request.java

/**
 * Execute a request/*from  w w  w  .  j av  a 2s.  c  o  m*/
 * 
 * @param url
 * @return Content of the response
 */
public String execute(String url) {
    Log.d(TAG, "Request to " + url);

    try {
        HttpGet getRequest = new HttpGet(url);
        HttpResponse response = (HttpResponse) httpClient.execute(getRequest);

        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream stream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                stream = new GZIPInputStream(stream);
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer sb = new StringBuffer();

            String line = null;

            while (null != (line = reader.readLine())) {
                sb.append(line + "\n");
            }

            //Log.d(TAG, "Response: " + sb);

            return sb.toString().trim();
        }
    } catch (Exception e) {
        Log.d(TAG, "Exception: " + e.getMessage());
    }

    return null;
}

From source file:com.asksunny.tool.RemoteDataStreamer.java

public void receiveCompress() throws Exception {

    try (ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(getPort());
            Socket client = server.accept();
            InputStream in = client.getInputStream();
            GZIPInputStream gin = new GZIPInputStream(in);
            FileOutputStream fout = new FileOutputStream(getFilePath())) {
        StreamCopier.copy(gin, fout);/*  w  w  w .  j ava2  s . co m*/
    }

}

From source file:com.taobao.tair.etc.TranscoderUtil.java

public static byte[] decompress(byte[] in) {
    ByteArrayOutputStream bos = null;

    if (in != null) {
        ByteArrayInputStream bis = new ByteArrayInputStream(in);

        bos = new ByteArrayOutputStream();

        GZIPInputStream gis = null;

        try {/*from   w  w w .j  a  v a 2s  .  c om*/
            gis = new GZIPInputStream(bis);

            byte[] buf = new byte[8192];
            int r = -1;

            while ((r = gis.read(buf)) > 0) {
                bos.write(buf, 0, r);
            }
        } catch (IOException e) {
            bos = null;
            throw new RuntimeException(e);
        } finally {
            try {
                gis.close();
                bos.close();
            } catch (Exception e) {
            }
        }
    }

    return (bos == null) ? null : bos.toByteArray();
}

From source file:jcurl.core.io.SetupSaxDeSer.java

public static SetupBuilder parse(final File file) throws SAXException, IOException {
    if (file.getName().endsWith("z"))
        return parse(new GZIPInputStream(new FileInputStream(file)));
    return parse(new FileInputStream(file));
}

From source file:com.github.jrh3k5.flume.mojo.plugin.io.ArchiveUtils.java

/**
 * Un-GZIP a file./*  w w  w . jav a2s  .  c om*/
 * 
 * @param toUnzip
 *            A {@link File} representing the GZIP file to be unzipped.
 * @param toFile
 *            A {@link File} representing the location to which the unzipped
 *            file should be placed.
 * @throws IOException
 *             If any errors occur during the unzipping.
 * @see #gzipFile(File, File)
 */
public void gunzipFile(File toUnzip, File toFile) throws IOException {
    if (!toUnzip.isFile()) {
        throw new IllegalArgumentException("Source file " + toUnzip + " must be an existent file.");
    }

    if (toFile.exists() && !toFile.isFile()) {
        throw new IllegalArgumentException("Destination file " + toFile
                + " exists, but is not a file and, as such, cannot be written to.");
    }

    GZIPInputStream zipIn = null;
    FileOutputStream fileOut = null;
    try {
        zipIn = new GZIPInputStream(new FileInputStream(toUnzip));
        fileOut = new FileOutputStream(toFile);
        IOUtils.copy(zipIn, fileOut);
    } finally {
        IOUtils.closeQuietly(fileOut);
        IOUtils.closeQuietly(zipIn);
    }
}

From source file:com.insightml.utils.io.IoUtils.java

public static InputStream inputStream(final File file) throws IOException {
    return file.getName().endsWith(".gz") ? new GZIPInputStream(new FileInputStream(file))
            : new FileInputStream(file);
}

From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

/**
 * Decompresses the given byte array and decodes with Base64 decoding
 *
 * @param compressed byte array input/*from   ww w.  ja  va2  s. c  o  m*/
 * @return decompressed data in string format
 * @throws IOException
 */
public static String decompressString(byte[] compressed) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(compressed);
    InputStream gis = new GZIPInputStream(is);
    return new String(Base64.decode(IOUtils.toByteArray(gis)), StandardCharsets.UTF_8);
}

From source file:com.icesoft.applications.faces.address.MatchAddressDB.java

/**
 * Decodes the stored XML wrapper object representation of the address
 * database and maps the entries as MatchCity, MatchState, and MatchZip
 * objects./*from  w  ww . j ava  2 s.c  o m*/
 *
 * @see XAddressDataWrapper
 */
private void loadXDB() {

    //each of these contains a city, state, and zip value
    XAddressDataWrapper xData = null;

    //for decoding the wrapper objects
    XMLDecoder xDecode = null;

    try {
        //load andunzip the xml file
        GZIPInputStream in = new GZIPInputStream(MatchAddressDB.class.getResourceAsStream("address.xml.gz"));

        //xml decoding mechanism
        xDecode = new XMLDecoder(new BufferedInputStream(in));
        xData = (XAddressDataWrapper) xDecode.readObject();

    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Database not found.");
    }

    if (xDecode == null)
        return;

    //loop through every entry in the xml file
    MatchBean city;
    MatchState state;
    MatchZip zip;

    while (xData != null) {

        //create zip, city and state objects
        zip = (MatchZip) zipMap.get((xData.getZip()));

        //new zip
        if (zip == null) {
            zip = new MatchZip(xData.getZip(), xData.getCity(), xData.getState());
            zipMap.put((xData.getZip()), zip);

            city = new MatchCity(xData.getCity(), xData.getState());

            state = (MatchState) stateMap.get((xData.getState()));

            //new state
            if (state == null) {
                state = new MatchState(xData.getState());
                stateMap.put((xData.getState()), state);

            }
            city = state.addCity((MatchCity) city);
            ((MatchCity) city).addZip(zip);
        }
        //get the next encoded object
        try {
            xData = (XAddressDataWrapper) xDecode.readObject();
        }
        //end of file
        catch (ArrayIndexOutOfBoundsException e) {
            if (log.isDebugEnabled())
                log.debug("Reached end of XML file.");
            return;
        }

    }
    //close the XML decoder
    xDecode.close();
}

From source file:com.marklogic.contentpump.CompressedDelimitedTextReader.java

protected void initStream(InputSplit inSplit) throws IOException {
    setFile(((FileSplit) inSplit).getPath());
    FSDataInputStream fileIn = fs.open(file);

    String codecString = conf.get(ConfigConstants.CONF_INPUT_COMPRESSION_CODEC,
            CompressionCodec.ZIP.toString());
    if (codecString.equalsIgnoreCase(CompressionCodec.ZIP.toString())) {
        zipIn = new ZipInputStream(fileIn);
        codec = CompressionCodec.ZIP;//  w  w  w  .  j  a  v  a 2  s.  c  om
    } else if (codecString.equalsIgnoreCase(CompressionCodec.GZIP.toString())) {
        zipIn = new GZIPInputStream(fileIn);
        codec = CompressionCodec.GZIP;
    }

    if (uriName == null) {
        generateId = conf.getBoolean(CONF_INPUT_GENERATE_URI, false);
        if (generateId) {
            idGen = new IdGenerator(file.toUri().getPath() + "-" + ((FileSplit) inSplit).getStart());
        } else {
            uriId = 0;
        }
    }
}