java.util.zip package of J# has several bugs. It doesn't work correctly for
a large byte array.
The following code outputs "OK" in Java, but outputs "Failed" in J#.
J# 2005 Beta 2 fixes this bug.
import java.util.*;
import java.util.zip.*;
import java.io.*;
public class ZipBugDemo
{
static byte[] readFull(InputStream is) throws Exception
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int size;
while ((size = is.read(buffer)) > 0)
os.write(buffer, 0, size);
byte[] result = os.toByteArray();
os.close();
return result;
}
/** @attribute System.STAThread() */
public static void main(String[] args) throws Exception
{
byte[] originalImage;
byte[] zippedImage;
byte[] unzippedImage;
{
// create test image
originalImage = new byte[1024*1024*2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 1024; j++)
for (int k = 0; k < 1024; k++)
originalImage[i * 1024*1024 + j * 1024 + k] =
(byte)(i + j * k);
}
{
// zip originalImage
Deflater deflater = new Deflater();
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos,
deflater);
dos.write(originalImage);
dos.close();
zippedImage = baos.toByteArray();
baos.close();
}
}
{
// unzip zippedImage
Inflater inflater = new Inflater();
ByteArrayInputStream bais = new
ByteArrayInputStream(zippedImage);
InflaterInputStream iis = new InflaterInputStream(bais,
inflater);
unzippedImage = readFull(iis);
iis.close();
bais.close();
}
{
// compare originalImage and unzippedImage
for (int i = 0; i < originalImage.length; i++)
if (originalImage[i] != unzippedImage[i])
{
System.out.println("Failed");
return;
}
System.out.println("OK");
}
}
}
Kazuya Ujihara
http://www.yqcomputer.com/