gpt4 book ai didi

Java GZipInputStream ZLib 输入流意外结束

转载 作者:太空宇宙 更新时间:2023-11-04 11:20:28 25 4
gpt4 key购买 nike

作为引用,这是我收到的完整错误:

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

}
}

关于Java GZipInputStream ZLib 输入流意外结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44976532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com