PNGException "crc corruption" when attempting to create ImageIcon objects from ZIP archive

Posted by Nathan Strong on Stack Overflow See other posts from Stack Overflow or by Nathan Strong
Published on 2010-05-06T03:09:26Z Indexed on 2010/05/06 3:18 UTC
Read the original article Hit count: 380

Filed under:
|
|
|

I've got a ZIP file containing a number of PNG images that I am trying to load into my Java application as ImageIcon resources directly from the archive. Here's my code:

import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;
import javax.swing.ImageIcon;

public class Test {
  public static void main( String[] args )
  {
    if( args.length == 0 )
    {
      System.out.println("usage: java Test.java file.zip");
      return;
    }
    File archive = new File( args[0] );
    if( !archive.exists() || !archive.canRead() )
    {
      System.err.printf("Unable to find/access %s.\n", archive);
      return;
    }

    try {
      ZipFile zip = new ZipFile(archive);
      Enumeration <? extends ZipEntry>e = zip.entries();
      while( e.hasMoreElements() )
      {
        ZipEntry entry = (ZipEntry) e.nextElement();
        int size = (int) entry.getSize();
        int count = (size % 1024 == 0) ? size / 1024 : (size / 1024)+1;
        int offset = 0;
        int nread, toRead;

        byte[] buffer = new byte[size];
        for( int i = 0; i < count; i++ )
        {
          offset = 1024*i;
          toRead = (size-offset > 1024) ? 1024 : size-offset;
          nread = zip.getInputStream(entry).read(buffer, offset, toRead);
        }
        ImageIcon icon = new ImageIcon(buffer); // boom -- why?
      }
      zip.close();      
    } catch( IOException ex ) {
      System.err.println(ex.getMessage());
    }
  }
}

The sizes reported by entry.getSize() match the uncompressed size of the PNG files, and I am able to read the data out of the archive without any exceptions, but the creation of the ImageIcon blows up. The stacktrace:

sun.awt.image.PNGImageDecoder$PNGException: crc corruption
 at sun.awt.image.PNGImageDecoder.getChunk(PNGImageDecoder.java:699)
 at sun.awt.image.PNGImageDecoder.getData(PNGImageDecoder.java:707)
 at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:234)
 at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
 at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
 at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
sun.awt.image.PNGImageDecoder$PNGException: crc corruption
 at sun.awt.image.PNGImageDecoder.getChunk(PNGImageDecoder.java:699)
 at sun.awt.image.PNGImageDecoder.getData(PNGImageDecoder.java:707)
 at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:234)
 at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
 at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
 at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

Can anyone shed some light on it? Google hasn't turned up any useful information.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing