List of usage examples for java.util.zip GZIPInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:org.commoncrawl.util.GZIPUtils.java
/** * Returns an gunzipped copy of the input array, truncated to * <code>sizeLimit</code> bytes, if necessary. If the gzipped input has been * truncated or corrupted, a best-effort attempt is made to unzip as much as * possible. If no data can be extracted <code>null</code> is returned. *//* w w w. j a v a 2 s .c o m*/ public static final UnzipResult unzipBestEffort(byte[] in, int offset, int sizeIn, int sizeLimit) { try { // decompress using GZIPInputStream DataOutputBuffer outStream = new DataOutputBuffer(EXPECTED_COMPRESSION_RATIO * in.length); boolean truncated = false; GZIPInputStream inStream = new GZIPInputStream(new ByteArrayInputStream(in, offset, sizeIn)); byte[] buf = new byte[BUF_SIZE]; int written = 0; while (true) { try { int size = inStream.read(buf); if (size <= 0) break; if ((written + size) > sizeLimit) { outStream.write(buf, 0, sizeLimit - written); truncated = true; break; } outStream.write(buf, 0, size); written += size; } catch (Exception e) { break; } } try { outStream.close(); } catch (IOException e) { } return new UnzipResult(outStream.getData(), 0, outStream.getLength(), truncated); } catch (IOException e) { return null; } catch (OutOfMemoryError e) { LOG.fatal(CCStringUtils.stringifyException(e)); return null; } }
From source file:de.zib.scalaris.examples.wikipedia.data.Revision.java
/** * De-compresses the given text and returns it as a string. * // www .jav a 2s . c o m * @param text * the compressed text * * @return the de-compressed text * * @throws RuntimeException * if de-compressing the text did not work */ protected static String unpackText(byte[] text) throws RuntimeException { try { ByteArrayOutputStream unpacked = new ByteArrayOutputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(text); GZIPInputStream gis = new GZIPInputStream(bis); byte[] bbuf = new byte[256]; int read = 0; while ((read = gis.read(bbuf)) >= 0) { unpacked.write(bbuf, 0, read); } gis.close(); return new String(unpacked.toString("UTF-8")); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.apache.ode.daohib.bpel.hobj.GZipDataType.java
/** * Decompress (using gzip algorithm) a byte array. *//*from w w w.j a va 2s . c o m*/ public static byte[] gunzip(InputStream input) { try { GZIPInputStream unzip = new GZIPInputStream(input); ByteArrayOutputStream baos = new ByteArrayOutputStream(32 * 1024); byte[] buf = new byte[4096]; int len; while ((len = unzip.read(buf)) > 0) { baos.write(buf, 0, len); } unzip.close(); return baos.toByteArray(); } catch (IOException ex) { throw new RuntimeException(ex); } }
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 {/* w w w . j av a 2 s . 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:com.crawlersick.nettool.GetattchmentFromMail1.java
public static String decompress(byte[] compressed) throws IOException { final int BUFFER_SIZE = 32; ByteArrayInputStream is = new ByteArrayInputStream(compressed); GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE); StringBuilder string = new StringBuilder(); byte[] data = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = gis.read(data)) != -1) { string.append(new String(data, 0, bytesRead)); }//from www .j av a2s . c om gis.close(); is.close(); return string.toString(); }
From source file:org.squashtest.tm.web.internal.controller.report.ReportController.java
public static byte[] decompress(byte[] str) throws IOException { byte[] buf = new byte[str.length * 10]; GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str)); int len;/*from w w w.j a v a 2 s . com*/ ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((len = gis.read(buf)) > 0) { out.write(buf, 0, len); } return out.toByteArray(); }
From source file:org.eclipse.smila.connectivity.framework.crawler.web.util.GZIPUtils.java
/** * Returns an gunzipped copy of the input array, truncated to <code>sizeLimit</code> bytes, if necessary. If the * gzipped input has been truncated or corrupted, a best-effort attempt is made to unzip as much as possible. If no * data can be extracted <code>null</code> is returned. * // w ww . ja va 2 s. c o m * @param in * input byte array * @param sizeLimit * value in bytes to truncate gunzziped copy of the input array * @return gunzipped byte array */ public static byte[] unzipBestEffort(byte[] in, int sizeLimit) { try { // decompress using GZIPInputStream final ByteArrayOutputStream outStream = new ByteArrayOutputStream( EXPECTED_COMPRESSION_RATIO * in.length); final GZIPInputStream inStream = new GZIPInputStream(new ByteArrayInputStream(in)); final byte[] buf = new byte[BUF_SIZE]; int written = 0; while (true) { try { final int size = inStream.read(buf); if (size <= 0) { break; } if (sizeLimit > 0) { if ((written + size) > sizeLimit) { outStream.write(buf, 0, sizeLimit - written); break; } } outStream.write(buf, 0, size); written += size; } catch (IOException e) { break; } } try { outStream.close(); } catch (IOException exception) { // don't matter ; } return outStream.toByteArray(); } catch (IOException e) { return null; } }
From source file:com.jivesoftware.os.jive.utils.shell.utils.Unzip.java
public static File unGzip(boolean verbose, File outputDir, String outName, File inputFile, boolean deleteOriginal) throws FileNotFoundException, IOException { String inFilePath = inputFile.getAbsolutePath(); if (verbose) { System.out.println("unzipping " + inFilePath); }//from w ww . jav a2s.com GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inFilePath)); File outFile = new File(outputDir, outName); outFile.getParentFile().mkdirs(); String outFilePath = outFile.getAbsolutePath(); OutputStream out = new FileOutputStream(outFilePath); byte[] buf = new byte[1024]; int len; while ((len = gzipInputStream.read(buf)) > 0) { out.write(buf, 0, len); } gzipInputStream.close(); out.close(); if (deleteOriginal) { FileUtils.forceDelete(inputFile); if (verbose) { System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath())); } } if (verbose) { System.out.println("unzipped " + inFilePath); } return new File(outFilePath); }
From source file:org.zaproxy.libs.DownloadTools.java
public static void downloadDriver(String urlStr, String destDir, String destFile) { File dest = new File(destDir + destFile); if (dest.exists()) { System.out.println("Already exists: " + dest.getAbsolutePath()); return;//from w w w . jav a 2s. c om } File parent = dest.getParentFile(); if (!parent.exists() && !parent.mkdirs()) { System.out.println("Failed to create directory : " + dest.getParentFile().getAbsolutePath()); } byte[] buffer = new byte[1024]; if (urlStr.endsWith(".zip")) { try { URL url = new URL(urlStr); ZipInputStream zipIn = new ZipInputStream(url.openStream()); ZipEntry entry; boolean isFound = false; while ((entry = zipIn.getNextEntry()) != null) { if (destFile.equals(entry.getName())) { isFound = true; FileOutputStream out = new FileOutputStream(dest); int read = 0; while ((read = zipIn.read(buffer)) != -1) { out.write(buffer, 0, read); } out.close(); System.out.println("Updated: " + dest.getAbsolutePath()); } else { System.out.println("Found " + entry.getName()); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); if (!isFound) { System.out.println("Failed to find " + destFile); System.exit(1); } } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } else if (urlStr.endsWith(".tar.gz")) { try { URL url = new URL(urlStr); GZIPInputStream gzis = new GZIPInputStream(url.openStream()); File tarFile = new File(dest.getAbsolutePath() + ".tar"); FileOutputStream out = new FileOutputStream(tarFile); int len; while ((len = gzis.read(buffer)) > 0) { out.write(buffer, 0, len); } gzis.close(); out.close(); TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(tarFile)); ArchiveEntry entry; boolean isFound = false; while ((entry = tar.getNextEntry()) != null) { if (destFile.equals(entry.getName())) { out = new FileOutputStream(dest); isFound = true; int read = 0; while ((read = tar.read(buffer)) != -1) { out.write(buffer, 0, read); } out.close(); System.out.println("Updated: " + dest.getAbsolutePath()); } } tar.close(); tarFile.delete(); if (!isFound) { System.out.println("Failed to find " + destFile); System.exit(1); } } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } }
From source file:com.norman0406.slimgress.API.Interface.Interface.java
private static String decompressGZIP(HttpEntity compressedEntity) throws IOException { final int bufferSize = 8192; InputStream input = compressedEntity.getContent(); GZIPInputStream gzipStream = new GZIPInputStream(input, bufferSize); StringBuilder string = new StringBuilder(); byte[] data = new byte[bufferSize]; int bytesRead; while ((bytesRead = gzipStream.read(data)) != -1) { string.append(new String(data, 0, bytesRead)); }//from w ww .ja va 2s . com gzipStream.close(); input.close(); return string.toString(); }