【发布时间】:2014-09-30 01:24:02
【问题描述】:
我正在尝试解压缩最初在 PHP 中压缩的 Java 中的 json 对象。以下是它被压缩成 PHP 的方式:
function zip_json_encode(&$arr) {
$uncompressed = json_encode($arr);
return pack('L', strlen($uncompressed)).gzcompress($uncompressed);
}
并解码(再次在 PHP 中):
function unzip_json_decode(&$data) {
$uncompressed = @gzuncompress(substr($data,4));
return json_decode($uncompressed, $array_instead_of_object);
}
它被放入 MySQL,现在它必须被 Java 从数据库中提取出来。我们将它从ResultSet 中拉出来,如下所示:
String field = rs.getString("field");
然后我将该字符串传递给解压缩它的方法。这就是它崩溃的地方。
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();
}
不太确定这里出了什么问题,但任何指针将不胜感激!
【问题讨论】:
标签: java php compression gzip zlib