public static byte[] gunzip(byte[] data) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(data); GZIPInputStream in = new GZIPInputStream(bis); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) >= 0) { bos.write(buffer, 0, len); } in.close(); bos.close(); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
private static boolean isGZIP(File headDump) { try { FileInputStream in = new FileInputStream(headDump); GZIPInputStream is; try { is = new GZIPInputStream(in); is.read(); is.close(); return true; } catch (IOException e) { in.close(); } } catch (IOException e) { // ignore } return false; }
/** * gzip decompress 2 string * * @param compressed * @param charsetName * @return */ public static String decompressForGzip(byte[] compressed, String charsetName) { final int BUFFER_SIZE = compressed.length; GZIPInputStream gis = null; ByteArrayInputStream is = null; try { is = new ByteArrayInputStream(compressed); 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, charsetName)); } return string.toString(); } catch (IOException e) { e.printStackTrace(); } finally { closeQuietly(gis); closeQuietly(is); } return null; }
public static ByteBuffer doDecompress(ByteBuffer buffer, int length) { byte[] byteArrayIn = new byte[1024]; ByteArrayInputStream byteIn; if (buffer.hasArray()) { byteIn = new ByteArrayInputStream(buffer.array(), buffer.position() + buffer.arrayOffset(), buffer.remaining()); } else { byte[] array = new byte[buffer.limit() - buffer.position()]; buffer.get(array); byteIn = new ByteArrayInputStream(array); } ByteBuffer retBuff = ByteBuffer.allocate(length); int len = 0; try { GZIPInputStream in = new GZIPInputStream(byteIn); while ((len = in.read(byteArrayIn)) > 0) { retBuff.put(byteArrayIn, 0, len); } in.close(); } catch (IOException e) { s_logger.error("Fail to decompress the request!", e); } retBuff.flip(); return retBuff; }
private static String uncompressGzip(InputStream stream) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPInputStream gzis = new GZIPInputStream(stream); byte[] buffer = new byte[1024]; int len = 0; String str = ""; while ((len = gzis.read(buffer)) > 0) { str += new String(buffer, 0, len); } gzis.close(); return str; }
public static byte[] unGzip(byte[] data) throws IOException { GZIPInputStream gzip = null; try { gzip = new GZIPInputStream(new ByteArrayInputStream(data)); byte[] buf = new byte[2048]; int size = -1; ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length + 1024); while ((size = gzip.read(buf, 0, buf.length)) != -1) { bos.write(buf, 0, size); } return bos.toByteArray(); } finally { if (gzip != null) { gzip.close(); } } } }
/** * Extract buffer from direct shuffle message. * * @param msg Message. * @return Buffer. * @throws IgniteCheckedException On error. */ private byte[] extractBuffer(HadoopDirectShuffleMessage msg) throws IgniteCheckedException { if (msgGzip) { byte[] res = new byte[msg.dataLength()]; try (GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(msg.buffer()), res.length)) { int len = in.read(res, 0, res.length); assert len == res.length; } catch (IOException e) { throw new IgniteCheckedException("Failed to uncompress direct shuffle message.", e); } return res; } else return msg.buffer(); }
public static void gunzip(File source, File dest, boolean deleteSource) throws IOException { byte[] buffer = new byte[2^20]; FileOutputStream out = new FileOutputStream(dest); GZIPInputStream in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(source))); int l; while ((l = in.read(buffer)) > 0) out.write(buffer, 0, l); in.close(); out.close(); if (deleteSource && dest.exists()) source.delete(); }
public static byte[] readCompressedByteArray(DataInput in) throws IOException { int length = in.readInt(); if (length == -1) { return null; } byte[] buffer = new byte[length]; in.readFully(buffer); // could/should use readFully(buffer,0,length)? GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buffer, 0, buffer.length)); byte[] outbuf = new byte[length]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len; while ((len = gzi.read(outbuf, 0, outbuf.length)) != -1) { bos.write(outbuf, 0, len); } byte[] decompressed = bos.toByteArray(); bos.close(); gzi.close(); return decompressed; }
private static void extractGzip(String filePath, String outputPath) throws IOException { System.out.println("Extracting files..."); byte[] buffer = new byte[BUFFER_SIZE]; try{ GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(new File(filePath))); FileOutputStream out = new FileOutputStream(new File(outputPath)); int len; while ((len = gzis.read(buffer)) > 0) { out.write(buffer, 0, len); } gzis.close(); out.close(); System.out.println("Done"); }catch(IOException ex){ ex.printStackTrace(); } }
public static byte[] gunzip(byte[] data) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(data); GZIPInputStream in = new GZIPInputStream(bis); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) { bos.write(buffer, 0, len); } in.close(); bos.close(); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
public static File decodeGZipFile(File file) throws Throwable { GZIPInputStream gzipIn = null; FileOutputStream gzipOut = null; try { gzipIn = new GZIPInputStream(new FileInputStream(file)); file = new File(file.getAbsolutePath() + ".gziptemp"); gzipOut = new FileOutputStream(file); byte[] buf = new byte[1024 * 8]; int num = -1; while ((num = gzipIn.read(buf, 0, buf.length)) != -1) { gzipOut.write(buf, 0, num); } } finally { if (gzipIn != null) gzipIn.close(); if (gzipOut != null) gzipOut.close(); } return file; }
public static byte[] gunzip(byte[] b) { byte[] buffer = new byte[Math.min(2^20, b.length)]; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(b.length * 2); GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(b), Math.min(65536, b.length)); int l; while ((l = in.read(buffer)) > 0) baos.write(buffer, 0, l); in.close(); baos.close(); return baos.toByteArray(); } catch (IOException e) {} return null; }
in = new GZIPInputStream(new FileInputStream(inFileName)); } catch (FileNotFoundException e) { System.err.println("File not found. " + inFileName); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } finally { try { if (in != null) in.close(); if (out != null) out.close(); } catch (IOException e) {
public static byte[] readCompressedByteArray(DataInput in) throws IOException { int length = in.readInt(); if (length == -1) return null; byte[] buffer = new byte[length]; in.readFully(buffer); // could/should use readFully(buffer,0,length)? GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buffer, 0, buffer.length)); byte[] outbuf = new byte[length]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len; while ((len = gzi.read(outbuf, 0, outbuf.length)) != -1) { bos.write(outbuf, 0, len); } byte[] decompressed = bos.toByteArray(); bos.close(); gzi.close(); return decompressed; }
progressMax = 50; GZIPInputStream gzipInputStream = new GZIPInputStream( new BufferedInputStream(new FileInputStream(zippedFile))); OutputStream outputStream = new FileOutputStream(outFilePath); byte[] data = new byte[BUFFER]; int len; while ((len = gzipInputStream.read(data, 0, BUFFER)) > 0) { bufferedOutputStream.write(data, 0, len); unzippedBytes += len; gzipInputStream.close(); bufferedOutputStream.flush(); bufferedOutputStream.close();
@Override protected byte[] doDecompress(byte[] compressed) throws IOException { byte[] buffer = new byte[512]; ByteArrayOutputStream outputStream = null; GZIPInputStream gzipInputStream = null; ByteArrayInputStream inputStream = null; try { inputStream = new ByteArrayInputStream(compressed); gzipInputStream = new GZIPInputStream(inputStream); outputStream = new ByteArrayOutputStream(); int read = gzipInputStream.read(buffer); while (read != -1) { outputStream.write(buffer, 0, read); read = gzipInputStream.read(buffer); } return outputStream.toByteArray(); } finally { Objects.nullSafeClose(inputStream, gzipInputStream, outputStream); } }
/** * Un gzip the compressedFile to the given decompressedFile * * @param compressedFile * @param decompressedFile */ public static void unGunzipFile(String compressedFile, String decompressedFile) { byte[] buffer = new byte[1024]; try { FileSystem fs = FileSystem.getLocal(new Configuration()); FSDataInputStream fileIn = fs.open(new Path(compressedFile)); GZIPInputStream gZIPInputStream = new GZIPInputStream(fileIn); FileOutputStream fileOutputStream = new FileOutputStream(decompressedFile); int bytes_read; while((bytes_read = gZIPInputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, bytes_read); } gZIPInputStream.close(); fileOutputStream.close(); } catch(IOException ex) { throw new VoldemortException("Got IOException while trying to un-gzip file: " + compressedFile, ex); } } }
private static byte[] unpackZnodeIfNecessary(byte[] znodeContents) { // Check for gzip header if (znodeContents[0] == 0x1F && znodeContents[1] == (byte) 0x8B) { try { GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(znodeContents)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int byteRead = inputStream.read(); while (byteRead != -1) { outputStream.write(byteRead); byteRead = inputStream.read(); } return outputStream.toByteArray(); } catch (IOException e) { LOGGER.error("Failed to decompress znode contents", e); return znodeContents; } } else { // Doesn't look compressed, just return the contents verbatim return znodeContents; } } }
new GZIPInputStream(new FileInputStream(data)); while ((len = gzipInputStream.read(buffer)) > 0) { out.write(buffer, 0, len); gzipInputStream.close(); out.close(); data.delete();