Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PNGEException“;“crc腐败”;尝试从ZIP存档创建ImageIcon对象时_Java_Swing_Zip_Awt - Fatal编程技术网

Java PNGEException“;“crc腐败”;尝试从ZIP存档创建ImageIcon对象时

Java PNGEException“;“crc腐败”;尝试从ZIP存档创建ImageIcon对象时,java,swing,zip,awt,Java,Swing,Zip,Awt,我有一个包含大量PNG图像的ZIP文件,我正试图将这些图像作为ImageIcon资源直接从存档加载到Java应用程序中。这是我的密码: 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

我有一个包含大量PNG图像的ZIP文件,我正试图将这些图像作为ImageIcon资源直接从存档加载到Java应用程序中。这是我的密码:

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());
    }
  }
}

有人能解释一下吗?谷歌还没有找到任何有用的信息。

你可能必须退出内部循环,而不是对每个块重复调用它。

这就解决了它!(嗯,不完全是这样,但至少我的测试用例现在没有崩溃)。我想知道它为什么起作用?我怀疑每次调用getInputStream()时都会重置指针,所以我实际上是在反复读取PNG的前1024个字节。谢谢我认为这是“你必须拉”,而不仅仅是“你可能”。看起来getInputStream()每次调用都返回不同的流,因此每次调用read()时,它都从流的开头开始读取!在结尾,缓冲区只包含PNG文件的前1KB,重复了很多次@jfpoilpret&@Nathan Strong:看一下
getInputStream()
的源代码,你们都是对的!
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)