java.util.zip.ZipException using ZipUtils from MSDNmag

  • Thread starter Thread starter vijai thoppae
  • Start date Start date
V

vijai thoppae

Hello,

I've developed a application using C# & its saving files in xml format.
Since the size of the file, is huge i'm using Zip Classes from J# in my C#
code, based on the
article in MSDNmag
(http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/default.aspx)

It's able to compress files upto size of 3 mb (xml) . Anything more than
that size it throw's an exception of type util.zip.ZipException, which i'm
catching it & these are the messages.
java.lang.ArrayIndexOutOfBoundsException: 65536
at java.io.FileInputStream.read(SByte[] buffer, Int32 offset, Int32
count)
at java.util.zip.ZipFile.readZIPEntries(String fname)

Is there a workaround for this problem. Even if i Ignore this exception,
when i try to open it back it throws the same exception
Any help will be greatly appreciated.

Thanks,
Vijai
 
Hi vijai,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use the Zip Classes
from J# in C# to compress an xml file larger than 3MB.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Here is my code which will use the Zip Classes to compress a file you may
have a try and let me know the result.
public static void compress_file(String filename, String outfilename)
{
try
{
sbyte[] buf = new sbyte[4096]; //read buffer
int retval; //return value
//set up input stream
FileInputStream fis = new FileInputStream(filename);
//set up output stream
FileOutputStream os = new FileOutputStream(outfilename);
//wrap output stream in ZipOutputStream
ZipOutputStream zos = new ZipOutputStream(os);
//set entry field in zipfile; do this once for each
//file you're compressing
ZipEntry ze = new ZipEntry(filename.Remove(0,
System.IO.Path.GetPathRoot(filename).Length));
ze.setMethod(ZipEntry.DEFLATED);
zos.putNextEntry(ze);
//read the input file in and write it to the zipfile
do
{
//read file in from input stream
retval = fis.read(buf, 0, 4096);
//-1 indicates end of file
if (retval != -1)
{
//write buffer to output stream
zos.write(buf, 0, retval);
}
}while (retval != -1);
//close this ZipEntry
zos.closeEntry();
//close input stream
fis.close();
//close output stream
zos.close();
}
catch(Exception e)
{
Console.WriteLine("Exception occurred: " + e);
}
}

[STAThread]
static void Main(string[] args)
{
//Create a file about 6MB.
string filename = @"C:\Test.xml";
XmlTextWriter w = new XmlTextWriter(filename,System.Text.Encoding.Unicode);
w.WriteStartElement("x","root","urn:1");
double percent1,percent=0;
for(int i=0;i<100000;++i)
{
percent1 = (double)i/1000;
if ((int)percent1 > percent)
{
Console.WriteLine(percent.ToString()+"%");
percent = percent1;
}

w.WriteStartElement("y" + i.ToString(),"item","urn:1");
w.WriteEndElement();
}
w.WriteEndElement();
w.Close();
//compress the file
compress_file(filename,@"C:\test.zip");
}


Please apply my suggestion above and let me know if it helps resolve your
problem.

However, if you still have any concern on this issue, please post a simple
reproduce sample here.
I will appreciate for your efforts.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

Thanks for your suggestion. Whatever you understood is correct. I've done
exactly the same steps to compress a file, with an additional step at the
end by using this class "java.util.zip.ZipFile(_FileName) " to create a new
ZipFile. Also while opening it, i'm using the same class to extract the
contents from it. The real problem is while trying to create a new
java.util.zip.ZipFile(_FileName) its blowing out by throwing an exception.

Do you've any equivalent sample code to uncompress the ZipFile without
using this class "java.util.zip.ZipFile(_FileName)"? Here's my code to
compress & uncompress the Zip File.

public static void ExtractZipFile(ZipFile file, string path,
FilterEntryMethod filter)
{

foreach(ZipEntry entry in new EnumerationAdapter(new
EnumerationMethod(file.entries)))

{

if (!entry.isDirectory())

{

if ((filter == null || filter(entry)))

{

java.io.InputStream s = file.getInputStream(entry);

try

{

string fname =
System.IO.Path.GetFileName(entry.getName());

string newpath = System.IO.Path.Combine(path,
System.IO.Path.GetDirectoryName(entry.getName()));


System.IO.Directory.CreateDirectory(newpath);

java.io.FileOutputStream dest = new
java.io.FileOutputStream(System.IO.Path.Combine(newpath, fname));

try

{

CopyStream(s, dest);

}

finally

{

dest.close();

}

}

finally

{

s.close();

}

}

}

}

}

public static void CopyStream(java.io.InputStream from, java.io.OutputStream
to)

{

sbyte[] buffer = new sbyte[8192];

int got;

while ((got = from.read(buffer, 0, buffer.Length)) > 0)

to.write(buffer, 0, got);

}

public static ZipFile UpdateZipFile(ZipFile file, FilterEntryMethod filter,
string[] newFiles)

{

string prev = file.getName();

string tmp = System.IO.Path.GetTempFileName();

ZipFile _newZipFile = null;

try

{

ZipOutputStream to = new ZipOutputStream(new
java.io.FileOutputStream(tmp));

try

{

CopyEntries(file, to, filter);

// add entries here

if (newFiles != null)

{

foreach(string f in newFiles)

{

ZipEntry z = new ZipEntry(f.Remove(0,
System.IO.Path.GetPathRoot(f).Length));

z.setMethod(ZipEntry.DEFLATED);

to.putNextEntry(z);

try

{

java.io.FileInputStream s = new
java.io.FileInputStream(f);

try

{

CopyStream(s, to);

}

finally

{

s.close();

}

}

finally

{

to.closeEntry();

}

}

}

}

finally

{

to.close();

}

file.close();

// now replace the old file with the new one

System.IO.File.Copy(tmp, prev, true);

System.IO.File.Delete(tmp);

_newZipFile = ZipFile(prev);

}

catch(Exception e)

{

Console.WriteLine("Error during
writing...",e.ToString());

}

return _newZipFile;

}



Check this out & let me know what can be done to UnCompress a ZipFile
without using the ZipClass. Infact i got reply from the author who wrote
this article, he claims that Microsoft has agreed this as a bug. I've
attached his reply too.
From: "Ianier Munoz" <[email protected]>
To: (e-mail address removed)
Subject: RE: java.util.zip.ZipException using ZipUtils from MSDNmag
Date: Wed, 05 May 2004 22:41:42 +0200

Hi Vijai,

This is a known bug of the J# runtime. The current word from MS is
"This is indeed a bug in the J# runtime. If you need a hotfix for
this issue, please call Microsoft Support at (800) 936-5800 to open
an incident. Since this is a bug you will not be charged."

For more information please check the following threads:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=#3ZiW aPpDHA.2848%40TK2MSFTNGP10.phx.gbl&rnum=5&prev=/groups%3Fhl%3Den%26lr%3D%26i
e%3DUTF-8%26oe%3DUTF-8%26q%3Dianier%26meta%3Dgroup%253Dmicrosoft.public.dotn
et.vjsharp
7716002008b2d583&seekm=OORVOn1GDHA.2248%40TK2MSFTNGP10.phx.gbl#link1

You could alternatively use #ZipLib, which is mostly compatible with
the J# libraries, so your code should compile #ZipLib with very
minimal changes.

Regards,

Ianier Munoz
http://www.chronotron.com




Any suggestions or new ideas will be greatly appreciated.

Thanks,

Vijai

"Peter Huang" said:
Hi vijai,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use the Zip Classes
from J# in C# to compress an xml file larger than 3MB.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Here is my code which will use the Zip Classes to compress a file you may
have a try and let me know the result.
public static void compress_file(String filename, String outfilename)
{
try
{
sbyte[] buf = new sbyte[4096]; //read buffer
int retval; //return value
//set up input stream
FileInputStream fis = new FileInputStream(filename);
//set up output stream
FileOutputStream os = new FileOutputStream(outfilename);
//wrap output stream in ZipOutputStream
ZipOutputStream zos = new ZipOutputStream(os);
//set entry field in zipfile; do this once for each
//file you're compressing
ZipEntry ze = new ZipEntry(filename.Remove(0,
System.IO.Path.GetPathRoot(filename).Length));
ze.setMethod(ZipEntry.DEFLATED);
zos.putNextEntry(ze);
//read the input file in and write it to the zipfile
do
{
//read file in from input stream
retval = fis.read(buf, 0, 4096);
//-1 indicates end of file
if (retval != -1)
{
//write buffer to output stream
zos.write(buf, 0, retval);
}
}while (retval != -1);
//close this ZipEntry
zos.closeEntry();
//close input stream
fis.close();
//close output stream
zos.close();
}
catch(Exception e)
{
Console.WriteLine("Exception occurred: " + e);
}
}

[STAThread]
static void Main(string[] args)
{
//Create a file about 6MB.
string filename = @"C:\Test.xml";
XmlTextWriter w = new XmlTextWriter(filename,System.Text.Encoding.Unicode);
w.WriteStartElement("x","root","urn:1");
double percent1,percent=0;
for(int i=0;i<100000;++i)
{
percent1 = (double)i/1000;
if ((int)percent1 > percent)
{
Console.WriteLine(percent.ToString()+"%");
percent = percent1;
}

w.WriteStartElement("y" + i.ToString(),"item","urn:1");
w.WriteEndElement();
}
w.WriteEndElement();
w.Close();
//compress the file
compress_file(filename,@"C:\test.zip");
}


Please apply my suggestion above and let me know if it helps resolve your
problem.

However, if you still have any concern on this issue, please post a simple
reproduce sample here.
I will appreciate for your efforts.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Vijai,

I am sorry that I miss the problem in the J# runtime.
As Ianier, I think you can contact Microsoft PSS for the hotfix, because we
can not provide the hotfix in the newsgroup.

Also you can use the #ZipLib to do the job which is open source.
You may get the in the link below.
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

Otherwise I think we have to use the third-party control.
e.g.
IP*Works! Zip V5 .NET Edition
http://www.nsoftware.com/products/showprod.aspx?part=IZN5-A
Xceed Zip for .NET
http://www.hallogram.com/xzipnet/


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks Peter for the link. I managed to use #ZipLib by making few changes to
mycode & its not throwing any exception. Hope MS will fix this issue in
future release.

~Vijai
 
Back
Top