簡體   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