如何通过 JDBC 读取 COMPRESS()-ed H2 blob 列?

How to read a COMPRESS()-ed H2 blob column via JDBC?

我有一个基于文件的 H2 数据库(引擎版本 1.4.196),其中 mediumblob 列包含 COMPRESS() 函数返回的数据:

create table foo (compressed_data mediumblob);
...
insert into foo (compressed_data) values (COMPRESS(STRINGTOUTF8('Test'), 'DEFLATE'));

(table由flyway创建和填充。)

我想在 JDBC 客户端中读取此数据,而无需先调用 DECOMPRESS()。 (我想在客户端进行解压以与另一个系统兼容)。我尝试通过 InflaterInputStream 读取数据,它可以解压缩 DEFLATE 数据:

try (InputStream dbStream = rs.getBinaryStream("compressed_data");
     InflaterInputStream inflaterStream = new InflaterInputStream(dbStream);
    ) {
        inflaterStream.read();
        ...

但这会导致错误:

java.util.zip.ZipException: incorrect header check
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
    ...

有什么方法可以从 H2 中的列中获取 InflaterInputStream 兼容的压缩数据?

由于您已经在使用 H2 JDBC 访问数据库,您可以简单地使用 getBytes 检索压缩数据并使用 org.h2.tools.CompressToolexpand 方法解压缩它:

// .java source file is Cp1252 encoded
String sql = "SELECT COMPRESS(STRINGTOUTF8('fermé'), 'DEFLATE') AS foo";
ResultSet rs = st.executeQuery(sql);
rs.next();
byte[] bytesOut = rs.getBytes(1);
byte[] expanded = org.h2.tools.CompressTool.getInstance().expand(bytesOut);
String strOut = new String(expanded, "UTF-8");