代码之家  ›  专栏  ›  技术社区  ›  ragoolaman

Java GZipInputStream ZLib输入流意外结束

  •  1
  • ragoolaman  · 技术社区  · 9 年前

    作为参考,以下是我得到的完整错误:

        java.io.EOFException: Unexpected end of ZLIB input stream
    at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
    at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:117)
    at java.io.FilterInputStream.read(FilterInputStream.java:107)
    at com.genomedownloader.Main.FTPGet(Main.java:245)
    at com.genomedownloader.Main.access$400(Main.java:28)
    at com.genomedownloader.Main$1.call(Main.java:494)
    at com.genomedownloader.Main$1.call(Main.java:468)
    at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.lang.Thread.run(Thread.java:
    

    我刚刚将我的程序从使用FTP4J作为Java FTP客户端切换到Apache FTPClient。我调整了下载代码,使Apache能够工作,现在当我尝试解压缩*时,出现了这个异常。gz文件程序下载。 以下是相关代码:

     package com.test;
    
     import org.apache.commons.net.ftp.FTPClient;
    
     import java.io.*;
     import java.util.zip.GZIPInputStream;
    
     public class Main {
    public static void main(String[] args) throws IOException {
        FTPClient client;
        client = new FTPClient();
        client.connect("ftp.ncbi.nlm.nih.gov");
        client.login("anonymous", "abc123");
        client.setControlKeepAliveTimeout(300 * 60000);
        client.changeWorkingDirectory("/genomes/all/GCF/000/334/875/GCF_000334875.1_ASM33487v1");
        client.retrieveFile("GCF_000334875.1_ASM33487v1_genomic.fna.gz", new BufferedOutputStream(new FileOutputStream(new File(System.getProperty("user.dir") + "\\GenomicFNA.gz"))));
        GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(System.getProperty("user.dir") + "\\GenomicFNA.gz"));
        OutputStream out = new FileOutputStream(System.getProperty("user.dir") + "\\GenomicFNA.fsa");
        byte[] buf = new byte[1024];
        int len;
        while ((len = gzipInputStream.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        gzipInputStream.close();
        out.close();
        client.logout();
        client.disconnect();
    }
    }
    

    我一辈子都搞不懂的是,为什么代码可以与FTP4J一起工作,但与Apache一起失败了,尽管代码没有使用Apache或FTP4J库中的任何内容……用Apache commons net运行上述代码,它应该可以工作。(在顶部给出错误)

    1 回复  |  直到 9 年前
        1
  •  0
  •   ragoolaman    9 年前

    将BufferedOutputStream更改为separate声明,并同样更改FileOutputStream,然后在GZip声明之前刷新并关闭这两个文件。完成的代码:

      package com.test;
    
       import org.apache.commons.net.ftp.FTPClient;
    
      import java.io.*;
      import java.util.zip.GZIPInputStream;
    
    public class Main {
    public static void main(String[] args) throws IOException {
        BufferedOutputStream streamy;
        FileOutputStream stream;
        FTPClient client;
        client = new FTPClient();
        client.connect("ftp.ncbi.nlm.nih.gov");
        client.login("anonymous", "abc123");
        client.setControlKeepAliveTimeout(300 * 60000);
        client.changeWorkingDirectory("/genomes/all/GCF/000/334/875/GCF_000334875.1_ASM33487v1");
        client.retrieveFile("GCF_000334875.1_ASM33487v1_genomic.fna.gz", streamy = new BufferedOutputStream(stream = new FileOutputStream(new File(System.getProperty("user.dir") + "\\GenomicFNA.gz"))));
        stream.flush();
        streamy.flush();
        stream.close();
        streamy.close();
        client.logout();
        client.disconnect();
        GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(System.getProperty("user.dir") + "\\GenomicFNA.gz"));
        OutputStream out = new FileOutputStream(System.getProperty("user.dir") + "\\GenomicFNA.fsa");
        byte[] buf = new byte[1024];
        int len;
        while ((len = gzipInputStream.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        gzipInputStream.close();
        out.close();
    
    }
    }