2017-07-07 180 views
1

僅供參考,以下是完整的錯誤我得到:的Java GZipInputStream zlib的輸入流意外結束

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運行上面的代碼,它應該可以工作。 (如在頂部出錯一樣工作)

+0

對不起,我derped,所述錯誤是在同時特別發生((LEN = gzipInputStream.read(BUF))> 0)行中,所有三個/四個地方它們。 – ragoolaman

+0

創建一個[Minimal,Complete,and Verifiable](https://stackoverflow.com/help/mcve)示例,不要轉儲1000 LOC。 –

+0

道歉,花了我一分鐘來弄清楚你的意思,我做了一個獨立的例子顯示它 – ragoolaman

回答

0

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

} 
}