J# java.util.zip.ZipFile exception

J# java.util.zip.ZipFile exception

Post by admi » Wed, 21 Sep 2005 02:23:55


I have a unit test that is attempting to extract a compressed file
using the java.util.zip.ZipFile object from the visual j# library. When
I attempt to create a ZipFile object in the unit test:
java.util.zip.ZipFile newZip = new
java.util.zip.ZipFile(ConfigurationSettings.AppSettings["zipPathAndNameToDecompress"].ToString());

Exception of type java.util.zip.ZipException was thrown
at java.util.zip.ZipFile.readZIPEntries(String fname)
at java.util.zip.ZipFile..ctor(String fname)

Of interest, WinZip can extract it correctly. Any ideas or solutions
would be greatly appreciated.
 
 
 

J# java.util.zip.ZipFile exception

Post by Ujihara, K » Wed, 21 Sep 2005 19:09:53

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/