3

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

1 回答 1

5

您必须在流的开头跳过 4 个字节:

InputStream in = new InflaterInputStream(
                     new ByteArrayInputStream(bs, 4, bs.length-4));

这是因为该COMPRESS函数返回一个字符串,其中前四个字节给出了压缩数据的长度,后面的字节是实际的压缩数据。

此外,您应该尝试找到一种直接以字节形式获取结果的方法。压缩结果是二进制数据,可能无法安全地转换为字符串。

于 2012-12-13T15:13:27.297 回答