首页 > How to use zlib gzip a string in nodejs?

How to use zlib gzip a string in nodejs?

在stackoverflow问了没人鸟我。。。
How to use zlib gzip a string in nodejs?

The result is difference between using GZIPOutputStream in java and using zlib.gzipSync() in nodejs zlib module;

the js code :


    var endata = zlib.gzipSync(stringData);

the java code :

public static byte[] gzipData(String data) throws Exception {
  byte[] b = null;
  if (!StringUtils.isEmpty(data)) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      GZIPOutputStream gzip = new GZIPOutputStream(bos);
      gzip.write(data.getBytes());
      gzip.finish();
      gzip.close();
      b = bos.toByteArray();
      bos.close(); 
  }
    return b;
}

gzip是一种文件编码格式,包括文件头和文件正文,正文部分使用deflate算法来进行运算,这个算法在运算的时候可以指定压缩级别,默认级别应该是6(如果我没记错的话)。node的zlib底层是调用的c的zlib库,deflate算法在运算的时候还有其他参数可以调整,但是node里面的文档说的很简单,所以说看c的文档会了解的更详细些。
再者,你这里用的java是什么类库,最好http://www.zlib.net/ 中给出的http://www.jcraft.com/jzlib/ 实现。

【热门文章】
【热门文章】