DeflatorInputStream and DeflatorOutputStream do not reconstruct the original data

Diante

New Member
I want to compress some data, so I came across the DeflatorInputStream & DeflatorOutputStream classes. However, the following example shows that I can't seem to reconstruct my original data when using these classes.When I switch to a ZipInputStream and ZipOutputStream it does work, but since I don't need zip files per se, I thought a generic compression would be better. Mainly I'm interested in understanding why this example doesn't work.\[code\]//Create some "random" dataint bytesLength = 1024;byte[] bytes = new byte[bytesLength];for(int i = 0; i < bytesLength; i++) { bytes = (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 databyte[] 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) databyte[] actuallyRead = Arrays.copyOf(readBuffer, read);//Results differ - will print falseSystem.out.println(Arrays.equals(bytes, actuallyRead));\[/code\]
 
Back
Top