开发者

Is there a Java equivalent of php's gzdeflate?

I'm using Java to constr开发者_Go百科uct a url to a php site. The query string has a parameter that has been compressed using php's gzdeflate. Is there a way in Java that I can do the same thing gzdeflate does?


I believe you are looking for the Deflater class:

http://download.oracle.com/javase/6/docs/api/java/util/zip/Deflater.html

For info about the differences: http://php.net/manual/en/function.gzinflate.php


If you want to read a GZIP compressed stream you should use the GZIPInputStream.

The Inflator/Deflator is a stripped down compression stream which is slightly faster and more compact but isn't compatible with any other file type AFAIK.


public static void testInflate() throws IOException {
byte[] bytes = hexStringToByteArray("73cecf2d284a2d2e56c84d55482c4ec9c9ceca490400");

ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
InflaterInputStream iis = new InflaterInputStream(bis, new Inflater(true));

byte[] buffer = new byte[1024];
int len = -1;
String result = "";

while ((len = iis.read(buffer)) != -1) {
  result += new String(Arrays.copyOf(buffer, len), "utf-8");
}

System.out.println(result);

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜