0

流媒体让我感到困惑 - 就在我认为我掌握了这个想法时,我的代码就崩溃了。

我正在尝试接收一个字符串,gzip 压缩它,并将其存储在 PostGreSQL 数据库中。然后我想把它拿回来并重新转换成输出流。当我尝试将流读入 GZIPstream 时,错误发生在我的“get”函数中。错误是:

java.util.zip.ZipException:不是 GZIP 格式

这是用于压缩字符串然后将其放入数据库的函数:

public Jstruct_FileImportRun setSnapshotXml(String inValue) throws IOException
{
    return setSnapshotXml(new BufferedInputStream(new ByteArrayInputStream(inValue.getBytes())));
}

public Jstruct_FileImportRun setSnapshotXml(BufferedInputStream inValue)
{
    try
    {
        ByteArrayOutputStream compressedBytes = new ByteArrayOutputStream(30000 * 1024);
        GZIPOutputStream gzipStream = new GZIPOutputStream(compressedBytes);

        byte[] transferBuffer = new byte[32 * 1024];
        int readSize;
        while ((readSize = inValue.read(transferBuffer)) != -1)
        {
            gzipStream.write(transferBuffer, 0, readSize);
        }
        inValue.close();
        gzipStream.close();

        getField(SNAPSHOT_XML).setValue(compressedBytes.toByteArray());
    }
    catch (IOException e)
    {
        throw new JstructException(e);
    }

    return this;
}

这是我尝试检索它时的代码(引发错误):

public static void getSnapshotXml(Connection inConn, Long inRunId, OutputStream inOutputStream) throws Exception
{
    SQLQuery query = new SQLQuery()
            .addSelect(SNAPSHOT_XML)
            .addFrom(getTable())
            .addWhereClause(FILE_IMPORT_RUN_ID + " = " + inRunId);

    ResultSet rs = null;
    try
    {
        rs = query.execute(inConn);
        if (rs.next())
        {
            InputStream stream = rs.getBinaryStream(SNAPSHOT_XML.name());
            GZIPInputStream gzipStream = new GZIPInputStream(stream);

            byte[] transferBuffer = new byte[32 * 1024];
            int readSize;
            while ((readSize = gzipStream.read(transferBuffer)) != -1)
            {
                inOutputStream.write(transferBuffer, 0, readSize);
            }

            gzipStream.close();
            inOutputStream.close();
        }
    }
    finally
    {
        SQLUtil.closeResultSetAndStatement(rs);
    }

}

再次,错误“java.util.zip.ZipException: Not in GZIP format”发生在“GZIPInputStream gzipStream = new GZIPInputStream(stream);”行中

哈!我究竟做错了什么?

4

0 回答 0