繁体   English   中英

Java GZipInputStream ZLib输入流的意外结束

[英]Java GZipInputStream unexpected end of ZLib input stream

供参考,这是我得到的完整错误:

    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运行以上代码,它应该工作。 (按照上面给出的错误进行操作)

将BufferedOutputStream更改为单独的声明,并同样更改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();

}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM