我正在尝试解压缩使用MySQL的COMPRESS函数返回的值:
SQLQuery query = session
.createSQLQuery("SELECT ID, COMPRESS(TEXT_COL) AS TEXT_COL FROM TABLE WHERE ID IN (1,2,3,...);")
.addScalar("ID", Hibernate.INTEGER)
.addScalar("TEXT_COL", Hibernate.TEXT);
List<Object[]> list = query.list();
for (Object[] result : list) {
String text = decompress(((String) result[1]).getBytes());
}
(...)
private String decompress(byte[] bs) {
InputStream in = new InflaterInputStream(new ByteArrayInputStream(bs));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[8192];
int len;
while((len = in.read(buffer))>0)
baos.write(buffer, 0, len);
return new String(baos.toByteArray(), "UTF-8");
} catch (IOException e) {
throw new AssertionError(e);
}
}
我收到java.lang.AssertionError:java.util.zip.ZipException:标题错误检查
解压缩这样的mysql结果的方法是什么?我的代码有什么问题?
提前致谢, 迭。
修改 感谢Joni的建议,我终于将代码更改为
SQLQuery query = session
.createSQLQuery("SELECT ID, COMPRESS(TEXT_COL) AS TEXT_COL FROM TABLE WHERE ID IN (1,2,3,...);")
.addScalar("ID", Hibernate.INTEGER)
.addScalar("TEXT_COL", Hibernate.BINARY);
List<Object[]> list = query.list();
for (Object[] result : list) {
String text = decompress(((byte[]) result[1]));
}
(...)
private String decompress(byte[] bs) {
InputStream in = new InflaterInputStream(new ByteArrayInputStream(bs, 4, bs.length-4));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[8192];
int len;
while((len = in.read(buffer))>0)
baos.write(buffer, 0, len);
return new String(baos.toByteArray(), "UTF-8");
} catch (IOException e) {
throw new AssertionError(e);
}
}
答案 0 :(得分:5)
您必须在流的开头跳过4个字节:
InputStream in = new InflaterInputStream(
new ByteArrayInputStream(bs, 4, bs.length-4));
这是因为COMPRESS
函数返回一个字符串,其中前四个字节给出压缩数据的长度,后面的字节是实际的压缩数据。
此外,您应该尝试找到一种直接以字节形式获取结果的方法。压缩结果是二进制数据,可能无法安全地转换为字符串。