List of usage examples for java.util.zip GZIPInputStream GZIPInputStream
public GZIPInputStream(InputStream in) throws IOException
From source file:Main.java
/** * Uncompresses a GZIP file.// w w w . j a v a 2s. c o m * * @param bytes * The compressed bytes. * @return The uncompressed bytes. * @throws IOException * if an I/O error occurs. */ public static byte[] gunzip(byte[] bytes) throws IOException { /* create the streams */ InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes)); try { ByteArrayOutputStream os = new ByteArrayOutputStream(); try { /* copy data between the streams */ byte[] buf = new byte[4096]; int len = 0; while ((len = is.read(buf, 0, buf.length)) != -1) { os.write(buf, 0, len); } } finally { os.close(); } /* return the uncompressed bytes */ return os.toByteArray(); } finally { is.close(); } }
From source file:Main.java
/** * Degzips <strong>all</strong> of the datain the specified {@link ByteBuffer}. * * @param compressed The compressed buffer. * @return The decompressed array.//from w w w . j a v a 2s .com * @throws IOException If there is an error decompressing the buffer. */ public static byte[] degzip(ByteBuffer compressed) throws IOException { try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressed.array())); ByteArrayOutputStream out = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; while (true) { int read = is.read(buffer, 0, buffer.length); if (read == -1) { break; } out.write(buffer, 0, read); } return out.toByteArray(); } }
From source file:Main.java
@SuppressWarnings("unchecked") static boolean loadSharedPreferencesFromFile(Context context, File src) { boolean res = false; ObjectInputStream input = null; try {//from w ww.ja va 2s .c o m GZIPInputStream inputGZIP = new GZIPInputStream(new FileInputStream(src)); input = new ObjectInputStream(inputGZIP); Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context).edit(); prefEdit.clear(); Map<String, Object> entries = (Map<String, Object>) input.readObject(); for (Entry<String, ?> entry : entries.entrySet()) { Object v = entry.getValue(); String key = entry.getKey(); if (v instanceof Boolean) prefEdit.putBoolean(key, ((Boolean) v).booleanValue()); else if (v instanceof Float) prefEdit.putFloat(key, ((Float) v).floatValue()); else if (v instanceof Integer) prefEdit.putInt(key, ((Integer) v).intValue()); else if (v instanceof Long) prefEdit.putLong(key, ((Long) v).longValue()); else if (v instanceof String) prefEdit.putString(key, ((String) v)); } prefEdit.commit(); res = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (input != null) { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return res; }
From source file:Main.java
public static String stringFromHttpPost(String urlStr, String body) { HttpURLConnection conn;/*from w ww . j av a2s .c om*/ try { URL e = new URL(urlStr); conn = (HttpURLConnection) e.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setInstanceFollowRedirects(true); conn.setRequestMethod("POST"); OutputStream os1 = conn.getOutputStream(); DataOutputStream out1 = new DataOutputStream(os1); out1.write(body.getBytes("UTF-8")); out1.flush(); conn.connect(); String line; BufferedReader reader; StringBuffer sb = new StringBuffer(); if ("gzip".equals(conn.getHeaderField("Content-Encoding"))) { reader = new BufferedReader( new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8")); } else { reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); } while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); logError(e.getMessage()); } return null; }
From source file:net.orpiske.tcs.utils.compression.Decompressor.java
/** * Decompress an array of bytes//from www . j a v a 2 s .c om * @param bytes the array to decompress * @return a String object with the text * @throws IOException if unable to decompress it for any reason */ public static String decompress(final byte[] bytes) throws IOException { ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); InputStream gzipInputStream = new GZIPInputStream(inputStream); /** * Ok, this should be "smarter". Will fix this, eventually ... */ Reader reader = new InputStreamReader(gzipInputStream, Charset.forName("UTF-8")); BufferedReader bufferedReader = new BufferedReader(reader); StringBuilder builder = new StringBuilder(); try { char[] buffer = new char[1]; while (bufferedReader.read(buffer) > 0) { builder.append(buffer); } return builder.toString(); } finally { IOUtils.closeQuietly(gzipInputStream); IOUtils.closeQuietly(inputStream); } }
From source file:CompressTransfer.java
/** * ?//from w w w. j a va2s.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.qwazr.utils.SerializationUtils.java
/** * Read an object from a file using a buffered stream and GZIP compression * * @param file the destination file/* w ww.j av a2 s . com*/ * @param <T> the type of the object * @return the deserialized object * @throws IOException */ public static <T> T deserialize(File file) throws IOException { FileInputStream is = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(is); GZIPInputStream zis = new GZIPInputStream(bis); try { return SerializationUtils.deserialize(zis); } finally { IOUtils.close(zis, bis, is); } }
From source file:Main.java
public static final byte[] uncompress(final byte[] bytes) { if (bytes == null) { throw new NullPointerException("byte[] is NULL !"); }/* w w w . j a v a 2s. com*/ ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(8192); byte[] buffer = new byte[bytes.length]; int length; try { GZIPInputStream gis = new GZIPInputStream(bais); while ((length = gis.read(buffer)) != -1) { bos.write(buffer, 0, length); } byte[] moreBytes = bos.toByteArray(); bos.close(); bais.close(); gis.close(); bos = null; bais = null; gis = null; return moreBytes; } 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); }/* w ww.j a v a 2s .c o m*/ 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:it.unimi.dsi.sux4j.mph.PaCoTrieDistributorMonotoneMinimalPerfectHashFunction.java
public static void main(final String[] arg) throws NoSuchMethodException, IOException, JSAPException { final SimpleJSAP jsap = new SimpleJSAP( PaCoTrieDistributorMonotoneMinimalPerfectHashFunction.class.getName(), "Builds an PaCo trie-based monotone minimal perfect hash function reading a newline-separated list of strings.", new Parameter[] { new FlaggedOption("encoding", ForNameStringParser.getParser(Charset.class), "UTF-8", JSAP.NOT_REQUIRED, 'e', "encoding", "The string file encoding."), new Switch("huTucker", 'h', "hu-tucker", "Use Hu-Tucker coding to reduce string length."), new Switch("iso", 'i', "iso", "Use ISO-8859-1 coding internally (i.e., just use the lower eight bits of each character)."), new Switch("utf32", JSAP.NO_SHORTFLAG, "utf-32", "Use UTF-32 internally (handles surrogate pairs)."), new Switch("zipped", 'z', "zipped", "The string list is compressed in gzip format."), new UnflaggedOption("function", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, JSAP.NOT_GREEDY, "The filename for the serialised monotone minimal perfect hash function."), new UnflaggedOption("stringFile", JSAP.STRING_PARSER, "-", JSAP.NOT_REQUIRED, JSAP.NOT_GREEDY, "The name of a file containing a newline-separated list of strings, or - for standard input; in the first case, strings will not be loaded into core memory."), }); JSAPResult jsapResult = jsap.parse(arg); if (jsap.messagePrinted()) return;/*w ww . j a v a 2s . c o m*/ final String functionName = jsapResult.getString("function"); final String stringFile = jsapResult.getString("stringFile"); final Charset encoding = (Charset) jsapResult.getObject("encoding"); final boolean zipped = jsapResult.getBoolean("zipped"); final boolean iso = jsapResult.getBoolean("iso"); final boolean utf32 = jsapResult.getBoolean("utf32"); final boolean huTucker = jsapResult.getBoolean("huTucker"); final Collection<MutableString> collection; if ("-".equals(stringFile)) { final ProgressLogger pl = new ProgressLogger(LOGGER); pl.displayLocalSpeed = true; pl.displayFreeMemory = true; pl.start("Loading strings..."); collection = new LineIterator( new FastBufferedReader( new InputStreamReader(zipped ? new GZIPInputStream(System.in) : System.in, encoding)), pl).allLines(); pl.done(); } else collection = new FileLinesCollection(stringFile, encoding.toString(), zipped); final TransformationStrategy<CharSequence> transformationStrategy = huTucker ? new HuTuckerTransformationStrategy(collection, true) : iso ? TransformationStrategies.prefixFreeIso() : utf32 ? TransformationStrategies.prefixFreeUtf32() : TransformationStrategies.prefixFreeUtf16(); BinIO.storeObject(new PaCoTrieDistributorMonotoneMinimalPerfectHashFunction<CharSequence>(collection, transformationStrategy), functionName); LOGGER.info("Completed."); }