dongmufen8105 2014-09-30 01:24
浏览 368

用Java解压缩PHP的gzcompress

I'm trying to decompress a json object in Java that was initially compressed in PHP. Here's how it gets compressed into PHP:

function zip_json_encode(&$arr) {
  $uncompressed = json_encode($arr);
  return pack('L', strlen($uncompressed)).gzcompress($uncompressed);
}

and decoded (again in PHP):

function unzip_json_decode(&$data) {
  $uncompressed = @gzuncompress(substr($data,4));
  return json_decode($uncompressed, $array_instead_of_object);
}

That gets put into MySQL and now it must be pulled out of the db by Java. We pull it out from the ResultSet like this:

String field = rs.getString("field");

I then pass that string to a method to decompress it. This is where it falls apart.

private String decompressHistory(String historyString) throws SQLException {
    StringBuffer buffer = new StringBuffer();
    try {
        byte[] historyBytes = historyString.substring(4).getBytes();
        ByteArrayInputStream bin = new ByteArrayInputStream(historyBytes);
        InflaterInputStream in = new InflaterInputStream(bin, new Inflater(true));
        int len;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf)) != -1) {
            // buf should be decoded, right?
        }

    } catch (IOException e) {
        e.getStackTrace();
    }
    return buffer.toString();

}

Not quite sure what's going wrong here, but any pointers would be appreciated!

  • 写回答

3条回答 默认 最新

  • duanqiao1961 2014-09-30 19:27
    关注

    You need to get rid of the true in Inflater(true). Use just Inflater(). The true makes it expect raw deflate data. Without the true, it is expecting zlib-wrapped deflate data. PHP's gzcompress() produces zlib-wrapped deflate data.

    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示