未找到 Java 9 Zip End 标头异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44899421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:24:25  来源:igfitidea点击:

Java 9 Zip End Header Not Found Exception

javahttpdownloadubuntu-16.04java-9

提问by gansub

Edit for Googlers: this turned out to be caused by using an out-of-date beta release of Java 9.

为 Google 员工编辑:结果证明这是由使用过时的 Java 9 测试版引起的。

I am trying to batch download zip files from this URL using java - SRTM filesand it requires a username/password to download and I am using the following java code and it gives me the following exception

我正在尝试使用 java - SRTM文件从这个 URL 批量下载 zip 文件,它需要用户名/密码才能下载,我正在使用以下 java 代码,它给了我以下异常

java.util.zip.ZipException: zip END header not found
at java.util.zip.ZipFile$Source.zerror(java.base@9-internal/ZipFile.java:1210)
at java.util.zip.ZipFile$Source.findEND(java.base@9-internal/ZipFile.java:1119)
at java.util.zip.ZipFile$Source.initCEN(java.base@9-internal/ZipFile.java:1126)
at java.util.zip.ZipFile$Source.<init>(java.base@9-internal/ZipFile.java:963)
at java.util.zip.ZipFile$Source.get(java.base@9-internal/ZipFile.java:933)
at java.util.zip.ZipFile.<init>(java.base@9-internal/ZipFile.java:213)
at java.util.zip.ZipFile.<init>(java.base@9-internal/ZipFile.java:145)
at java.util.zip.ZipFile.<init>(java.base@9-internal/ZipFile.java:159)
at toposwapper.rules.ZipFileDownloadAction.execute(ZipFileDownloadAction.java:29)

This is my version of java

这是我的java版本

 java openjdk version "9-internal"
 OpenJDK Runtime Environment (build 9-internal+0-2016-04-14-195246.buildd.src)
 OpenJDK 64-Bit Server VM (build 9-internal+0-2016-04-14-195246.buildd.src, mixed mode)

This is the code that I am using to download -

这是我用来下载的代码 -

    URL url1 = null;
    URLConnection conn = null;
    InputStream inputs = null;
    FileOutputStream out = null;
    try 
    {
        url1 = new URL(url);
        conn = url1.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.setRequestProperty("file-name", output.getName());
        conn.setRequestProperty("content-type","application/zip");
        String userpass = this.username + ":" + this.password;
        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
        conn.setRequestProperty("Authorization",basicAuth);
    } 
  catch (MalformedURLException ex) {
           Logger.getLogger(SrtmDownloadManager.class.getName()).log(Level.SEVERE, "", ex);
    throw new TopoSwapperException(ex.getMessage());
    }
  catch (IOException ioe)
    {
    Logger.getLogger(SrtmDownloadManager.class.getName()).log(Level.SEVERE, "", ioe);
    throw new TopoSwapperException(ioe.getMessage());
    }

    try 
      {
         inputs = conn.getInputStream();
         out = new FileOutputStream(output);
         byte[] b = new byte[1024];
         int count;
         while ((count = inputs.read(b)) > -1)
          {
            out.write(b,0,count);
           }
         out.flush();
         inputs.close();
         out.close();

      } 
    catch (FileNotFoundException ex) 
    {
        throw new TopoSwapperException(ex.getMessage());
    } 
    catch (IOException ex) 
    {
    Logger.getLogger(SrtmDownloadManager.class.getName()).log(Level.SEVERE, "", ex);
    throw new TopoSwapperException(ex.getMessage());
    }
finally
    {
        close(inputs);
        close(out);
    }

Can somebody help me why this fails ?

有人可以帮助我为什么会失败吗?

采纳答案by Adam Michalik

There are a few (already closed) bugs for Java 9 that mention this exception (eg. JDK-8170276, JDK-8172872). Since Java 9 is still in beta and you're using a version from over a year ago (2016-04-14 vs. July 2017 of the time of writing) you should upgrade to the newest Java 9 EA release or stick to Java 8 until a public release of Java 9.

Java 9 有一些(已经关闭的)错误提到了这个异常(例如 JDK-8170276JDK-8172872)。由于 Java 9 仍处于测试阶段,并且您使用的是一年多以前的版本(2016 年 4 月 14 日与撰写本文时的 2017 年 7 月),您应该升级到最新的 Java 9 EA 版本或坚持使用 Java 8直到 Java 9 公开发布。