我正在尝试解压缩 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);
}
}