List of usage examples for java.util.zip GZIPInputStream GZIPInputStream
public GZIPInputStream(InputStream in) throws IOException
From source file:Main.java
protected static ByteArrayOutputStream inflate(final ByteArrayOutputStream pOutCompressed) throws IOException { final ByteArrayOutputStream uncompressed = new ByteArrayOutputStream(ONE_MB); final GZIPInputStream zin = new GZIPInputStream(new ByteArrayInputStream(pOutCompressed.toByteArray())); int read;//from ww w .ja va 2 s . com final byte[] buf = new byte[ONE_MB]; while ((read = zin.read(buf)) != -1) { uncompressed.write(buf, 0, read); } return uncompressed; }
From source file:Main.java
public static String decompress(String hexString) throws UnsupportedEncodingException { final byte[] byteArray = new byte[hexString.length() / 2]; int k = 0;// w w w . jav a2 s .co m for (int i = 0; i < byteArray.length; i++) { byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff); byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff); byteArray[i] = (byte) (high << 4 | low); k += 2; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(byteArray); try { GZIPInputStream gunzip = new GZIPInputStream(in); byte[] buffer = new byte[256]; int n; while ((n = gunzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } } catch (IOException e) { e.printStackTrace(); } return out.toString(); }
From source file:Main.java
static String downloadHtml(String urlString) { StringBuffer buffer = new StringBuffer(); try {/*from ww w .j a v a 2 s . com*/ URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); String encoding = conn.getContentEncoding(); InputStream inStr = null; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { inStr = new GZIPInputStream(conn.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); } else { inStr = conn.getInputStream(); } int ptr = 0; InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312")); while ((ptr = inStrReader.read()) != -1) { buffer.append((char) ptr); } inStrReader.close(); conn.disconnect(); inStr.close(); } catch (Exception e) { e.printStackTrace(); } return buffer.toString(); }
From source file:Main.java
public static String gzipToString(final HttpEntity entity, final String defaultCharset) throws IOException, ParseException { if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); }/* ww w . j av a2s. c o m*/ InputStream instream = entity.getContent(); if (instream == null) { return ""; } // gzip logic start if (entity.getContentEncoding().getValue().contains("gzip")) { instream = new GZIPInputStream(instream); } // gzip logic end if (entity.getContentLength() > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } int i = (int) entity.getContentLength(); if (i < 0) { i = 4096; } String charset = EntityUtils.getContentCharSet(entity); if (charset == null) { charset = defaultCharset; } if (charset == null) { charset = HTTP.DEFAULT_CONTENT_CHARSET; } Reader reader = new InputStreamReader(instream, charset); CharArrayBuffer buffer = new CharArrayBuffer(i); try { char[] tmp = new char[1024]; int l; while ((l = reader.read(tmp)) != -1) { buffer.append(tmp, 0, l); } } finally { reader.close(); } return buffer.toString(); }
From source file:Main.java
public static String toString(final File file, final boolean gzip, final Charset charset) throws IOException { InputStream is = new FileInputStream(file); if (gzip)// w w w. ja v a2 s . com is = new GZIPInputStream(is); return toString(is, charset); }
From source file:Main.java
public static void unzip(InputStream is, OutputStream os) { GZIPInputStream gzip = null;// w ww . j av a2 s. c o m try { gzip = new GZIPInputStream(is); byte[] buf = new byte[1024]; int len; while ((len = gzip.read(buf)) != -1) { os.write(buf, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { closeIO(gzip, os); } }
From source file:Main.java
public static void unzip(InputStream is, OutputStream os) { GZIPInputStream gzip = null;/*from w w w .jav a2 s . c om*/ try { gzip = new GZIPInputStream(is); byte[] buf = new byte[1024]; int len; while ((len = gzip.read(buf)) != -1) { os.write(buf, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { closeIO(gzip); closeIO(os); } }
From source file:Main.java
protected static String getResponseAsString(HttpURLConnection conn, boolean responseError) throws IOException { String charset = getResponseCharset(conn.getContentType()); String header = conn.getHeaderField("Content-Encoding"); boolean isGzip = false; if (header != null && header.toLowerCase().contains("gzip")) { isGzip = true;/*w w w . ja v a 2s. com*/ } InputStream es = conn.getErrorStream(); if (es == null) { InputStream input = conn.getInputStream(); if (isGzip) { input = new GZIPInputStream(input); } return getStreamAsString(input, charset); } else { if (isGzip) { es = new GZIPInputStream(es); } String msg = getStreamAsString(es, charset); if (TextUtils.isEmpty(msg)) { throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage()); } else if (responseError) { return msg; } else { throw new IOException(msg); } } }
From source file:ZipDemo.java
public static final String uncompress(final byte[] compressed) throws IOException { String uncompressed = ""; try {/*w w w . jav a2s . c o m*/ ByteArrayInputStream bais = new ByteArrayInputStream(compressed); GZIPInputStream zis = new GZIPInputStream(bais); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int numBytesRead = 0; byte[] tempBytes = new byte[DEFAULT_BUFFER_SIZE]; while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) { baos.write(tempBytes, 0, numBytesRead); } uncompressed = new String(baos.toByteArray()); } catch (ZipException e) { e.printStackTrace(System.err); } return uncompressed; }
From source file:Util.java
public static Object decompress(byte[] data) { if (data == null) { return null; }// www . j a v a 2s . co m try { ByteArrayInputStream bais = new ByteArrayInputStream(data); GZIPInputStream gin = new GZIPInputStream(bais); ObjectInputStream ois = new ObjectInputStream(gin); return ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return null; }