unzip error

I'm compressing a byte[] and trying to decompressing it again.

When array is small there're no problems. But when size gets 507 or plus, the unzip returns a lot o 0 (zeros) in the end of the result.

publicvoid testZip()throws Exception{

byte[] b =newbyte[1024];

for(int i = 0; i < b.length; i++)

b[i] = getRandom();

byte[] z = zip(b);

byte[] u = unzip(z);

System.out.println(b.length +": " + byteArrayToHex(b));

System.out.println(u.length +": " + byteArrayToHex(u));

System.out.println(z.length +": " + byteArrayToHex(z));

}

privatestaticbyte getRandom(){

byte b = (byte)(Math.random() * 1000);

if (b == 0)

return getRandom();

return b;

}

privatebyte[] zip(byte[] bData)throws Exception{

long lTam = bData.length;

ByteArrayOutputStream baos =new ByteArrayOutputStream();

ZipOutputStream zip =new ZipOutputStream(baos);

zip. putNextEntry(new ZipEntry("test"));

zip.write(bData);

zip.close();

bData = baos.toByteArray();

byte[] bFinal =newbyte[4 + bData.length];

bFinal[0] = (byte)lTam;

bFinal[1] = (byte)(lTam>>8);

bFinal[2] = (byte)(lTam>>16);

bFinal[3] = (byte)(lTam>>24);

System.arraycopy(bData, 0, bFinal, 4, bData.length);

return bFinal;

}

privatebyte[] unzip(byte[] bFinal)throws Exception{

long lTam = (bFinal[0]&255)|((bFinal[1]&255)<<8)|((bFinal[2]&255)<<16)|((bFinal[3]&255)<<24);

byte[] b =newbyte[(int)lTam];

ByteArrayInputStream bais =new ByteArrayInputStream(bFinal, 4, bFinal.length-4);

ZipInputStream zip =new ZipInputStream(bais);

zip.getNextEntry();

zip.read(b);

zip.close();

return b;

}

any ideas?

thks

[3707 byte] By [pbulgarellia] at [2007-11-27 8:47:09]
# 1
solved.I don't know why but the read() method of ZipInputStream do not full the buffer with the entire bytes he has.So it needs a while read() != -1Using Inflater and Deflater are good hints too.
pbulgarellia at 2007-7-12 20:51:21 > top of Java-index,Java Essentials,Java Programming...