【发布时间】:2025-12-26 22:00:16
【问题描述】:
public static String compressString(String str) throws IOException{
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
Gdx.files.local("gziptest.gzip").writeString(out.toString(), false);
return out.toString();
}
当我将该字符串保存到文件中并在 unix 中运行 gunzip -d file.txt 时,它会抱怨:
gzip: gzip.gz: not in gzip format
【问题讨论】:
-
为什么不直接使用FileOutputStream (in place of the ByteArrayOutputStream)?你有没有尝试过会发生什么?
-
是libgdx,是一个跨平台的游戏开发库。我只是将其写入文件以进行故障排除。我实际上一直在尝试通过 http POST 请求将字符串发送到我的烧瓶服务器,但服务器端抱怨该字符串不是有效的 gzip。
-
我猜问题是您将压缩数据转换为字符串。我认为您应该将结果视为字节[]。 libgdx 可以将 byte[] 写入文件吗?
-
可以的。试试 Gdx.files.local("gziptest.gzip").writeBytes(out.getBytes(), false)。会发生什么?