我正在尝试接受一个字符串,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);”
行中HALP !!我做错了什么?