Click here to Skip to main content
6,595,854 members and growing! (19,196 online)
Email Password   helpLost your password?
General Programming » Bugs & Workarounds » .NET issues     Intermediate

Bug when using the java.util.zip classes to write zip files

By Nishant Sivakumar

Describes an issue when writing a binary file into a zip file using the java.util.zip classes
.NET 1.1, Dev
Posted:8 May 2004
Views:75,647
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 5.04 Rating: 4.19 out of 5
1 vote, 6.3%
1

2
2 votes, 12.5%
3
1 vote, 6.3%
4
12 votes, 75.0%
5

Introduction

I was writing a little .NET app in C# and was looking for a small library that would let me read and write zip files. That's when my first Google search gave me a link to Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C#. Great! - that was my first reaction, but it didn't last too long. You can read zip files alright, but when you write zip files, if you have binary files (as you most often will), the generated zip file will be corrupt and you can't read it back. Funnily, WinZip does open it (ignoring the header errors) and the suggested solution by a Microsoftie in the one of the MS NGs was to extract all files using WinZip and then to zip it back. LOL. Why'd anyone want to use the zipping libraries at all if they had the option of bringing WinZip into the picture?

There is not even a KB article on this bug - bad! I lost about half a day when I was using these classes because I initially thought it was something wrong in my code. Anyway, this is a warning to everyone. Do not use the J# class libraries for zipping/unzipping if you are using the BCL 1.1 version. Heath Stewart (C# MVP and CodeProject Editor) has informed me that the BCL 2.0 includes zipping functionality - so that's really good news. And until that comes out, a very good free alternative to the J# classes is SharpZipLib which I very strongly recommend.

Bug demo

  1. Create a new C# console app project
  2. Add a reference to vjslib (...Microsoft.NET\Framework\v1.1.4322\vjslib.dll)
  3. Copy/paste the following code into your main cs file :-
    using System;
    using System.IO;
    using java.util.zip;
    
    class MainClass
    {
      [STAThread]
      static void Main(string[] args)
      {
        BugDemo();
      }
    
      static void BugDemo()
      {
        //Write zip file
    
        ZipOutputStream os = new ZipOutputStream(
          new java.io.FileOutputStream("test.zip"));
        ZipEntry ze = new ZipEntry("gluk.gif");
        ze.setMethod(ZipEntry.DEFLATED);
        os.putNextEntry(ze);
        java.io.FileInputStream fs = 
          new java.io.FileInputStream("gluk.gif");
        sbyte[] buff = new sbyte[1024];
        int n = 0;
    
        while ((n=fs.read(buff,0,buff.Length))>0)
        {
          os.write(buff,0,n);
        }
        fs.close();
        os.closeEntry();      
        os.close();
    
        Console.WriteLine("Okay, the zip has been written.");
    
        //Read zip file
    
        try
        {
          //Bug will occur here
    
          ZipFile zf = new ZipFile("test.zip");  
        }
        catch
        {
          Console.WriteLine("Bug confirmed!");
        }
      }
    }
  4. Now make sure you have a gluk.gif in the same folder as the executable.

  5. Compile and run.

Workaround

There is no real workaround to the problem. You can use the alternative library I mentioned above or wait for Whidbey.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nishant Sivakumar


Member
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.

Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.
Location: United States United States

Other popular Bugs & Workarounds articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
Generalhow to open and read files from a zip file using jsp or java Pinmembersandhyamn20:56 8 Jun '07  
GeneralJava Zip PinmemberSanjitP23:28 11 Apr '07  
GeneralRe: Java Zip PinmemberWajeehAhmed2:30 16 Apr '07  
NewsAlternative? Pinmemberevoluzion12:02 22 Dec '06  
GeneralRe: Alternative? PinmemberBrad Bruce12:19 22 Dec '06  
GeneralVB convert procedure PinmemberAlessandro Betti7:56 17 May '06  
Generalon text files Pinmemberdvsr14:33 27 Dec '05  
GeneralAlternative (with code) PinmemberElmue3:30 17 Nov '05  
GeneralA fix available from M$ Pinmemberrshearer11:43 31 Aug '05  
GeneralAnother big problem Pinmembernahumtakum1:31 15 May '04  
Generalis it really corrupt? PinmemberSamJost22:11 10 May '04  
GeneralRe: is it really corrupt? PinstaffNishant S22:36 10 May '04  
GeneralRe: is it really corrupt? PinmemberSamJost22:53 10 May '04  
GeneralRe: is it really corrupt? PinstaffNishant S23:40 10 May '04  
GeneralRe: is it really corrupt? PinmemberSamJost23:44 10 May '04  
GeneralTry this code PinmemberValeri6:56 9 May '04  
GeneralRe: Try this code PinstaffNishant S7:18 9 May '04  
GeneralRe: Try this code PinsussRoberto Ballerini22:54 12 May '04  
GeneralRe: Try this code PinmemberValeri23:49 12 May '04  
GeneralWorks fine for me ? PinmemberBee Master6:36 9 May '04  
GeneralRe: Works fine for me ? PinstaffNishant S7:14 9 May '04  
GeneralRe: Works fine for me ? PinmemberBee Master6:58 11 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 May 2004
Editor: Nishant Sivakumar
Copyright 2004 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project