我想压缩一些数据,所以我碰到了DeflatorInputStream & DeflatorOutputStream类。但是,下面的示例显示,在使用这些类时,似乎无法重建我的原始数据。DeflatorInputStream和DeflatorOutputStream不重构原始数据
当我切换到ZipInputStream和ZipOutputStream它可以工作,但由于我本身不需要zip文件,所以我认为通用压缩会更好。主要是我对理解为什么这个例子不起作用感兴趣。
//Create some "random" data
int bytesLength = 1024;
byte[] bytes = new byte[bytesLength];
for(int i = 0; i < bytesLength; i++) {
bytes[i] = (byte) (i % 10);
}
//Compress the data, and write it to somewhere (a byte array for this example)
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
DeflaterOutputStream outputStream = new DeflaterOutputStream(arrayOutputStream);
outputStream.write(bytes);
//Read and decompress the data
byte[] readBuffer = new byte[5000];
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
DeflaterInputStream inputStream = new DeflaterInputStream(arrayInputStream);
int read = inputStream.read(readBuffer);
//Should hold the original (reconstructed) data
byte[] actuallyRead = Arrays.copyOf(readBuffer, read);
//Results differ - will print false
System.out.println(Arrays.equals(bytes, actuallyRead));