我对 GZIPOutputStream 有一个非常奇怪的问题压缩 double 组时。在第 57 个元素处,当我重新加载数据时出现了一个小差异:
57 > 3.003727492141554E7 3.0037273913440887E7
1900: false
57 > -6.110783629228158E7 -6.110783629076558E7
2000: false
1900 和 2000 是两组不同的 double 。左边的值是原来的值。
当我使用一个简单的 FileOutputStream没有 GZIP,我不明白这个问题。为什么? GZIP 输出流是否已知会丢失信息?
编辑
下面是我读取和写入数据的方式:
public static final double[] coefficients = new double[1161289];
...
public static void dump(File f) throws FileNotFoundException, IOException {
OutputStream os = FileUtils.zipContent(f);
byte[] ba = new byte[8];
ByteBuffer BF = ByteBuffer.wrap(ba);
BF.order(ByteOrder.BIG_ENDIAN);
for (int i=0;i<coefficients.length;i++) {
BF.putDouble(0, coefficients[i]);
os.write(ba,0,8);
}
os.close();
}
public static void load(File f) throws FileNotFoundException, IOException {
InputStream is = FileUtils.readZippedContent(f);
byte[] ba = new byte[8];
final ByteBuffer BF = ByteBuffer.wrap(ba);
BF.order(ByteOrder.BIG_ENDIAN);
for (int i=0;i<coefficients.length;i++) {
is.read(ba,0,8);
coefficients[i] = BF.getDouble(0);
}
}
...
public static GZIPOutputStream zipContent(File f)
throws FileNotFoundException, IOException {
return new GZIPOutputStream(new FileOutputStream(f));
}
public static GZIPInputStream readZippedContent(File f)
throws FileNotFoundException, IOException {
return new GZIPInputStream(new FileInputStream(f));
}
最佳答案
是什么让您认为:is.read(ba,0,8);
返回 8
总是?
简而言之:阅读例程是伪造的。
关于java - GZIPOutputStream 是否已知会在压缩过程中丢失数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6719805/