java.util.zip.GZIPInputStream.read()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(245)

本文整理了Java中java.util.zip.GZIPInputStream.read()方法的一些代码示例,展示了GZIPInputStream.read()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GZIPInputStream.read()方法的具体详情如下:
包路径:java.util.zip.GZIPInputStream
类名称:GZIPInputStream
方法名:read

GZIPInputStream.read介绍

[英]Reads uncompressed data into an array of bytes. Blocks until enough input is available for decompression.
[中]将未压缩的数据读入字节数组。块,直到有足够的输入可用于解压缩。

代码示例

代码示例来源:origin: apache/storm

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);
  }
}

代码示例来源:origin: aragozin/jvm-tools

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;
}

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 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;
}

代码示例来源:origin: apache/cloudstack

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;
}

代码示例来源:origin: benmfaul/XRTB

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;
}

代码示例来源:origin: weibocom/motan

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();
      }
    }

  }
}

代码示例来源:origin: apache/ignite

/**
 * 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();
}

代码示例来源:origin: loklak/loklak_server

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();
}

代码示例来源:origin: apache/storm

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;
}

代码示例来源:origin: deeplearning4j/dl4j-examples

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();
  }
}

代码示例来源:origin: alibaba/jstorm

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);
  }
}

代码示例来源:origin: wangdan/AisenWeiBo

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;
}

代码示例来源:origin: loklak/loklak_server

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;
}

代码示例来源:origin: cymcsg/UltimateAndroid

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) {

代码示例来源:origin: alibaba/jstorm

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;
}

代码示例来源:origin: rmtheis/android-ocr

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();

代码示例来源:origin: jwtk/jjwt

@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);
  }
}

代码示例来源:origin: voldemort/voldemort

/**
   * 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);
    }
  }
}

代码示例来源:origin: apache/incubator-pinot

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;
  }
 }
}

代码示例来源:origin: rakam-io/rakam

new GZIPInputStream(new FileInputStream(data));
while ((len = gzipInputStream.read(buffer)) > 0) {
  out.write(buffer, 0, len);
gzipInputStream.close();
out.close();
data.delete();

相关文章