[英]Unexpected end of ZLIB input stream in java
public class GzExtractor implements Extractor {
Logger logger = LoggerFactory.getLogger(GzExtractor.class);
private static final int BUFFER_SIZE = 1024;
byte[] buff = new byte[BUFFER_SIZE];
private File file;
private String destinationPath;
public GzExtractor(File file, String destinationPath) {
this.file = file;
this.destinationPath = destinationPath;
}
public void extract() {
try {
File destDir = new File(destinationPath);
if (!destDir.exists()) {
destDir.mkdir();
}
GZIPInputStream gZipObj = new GZIPInputStream(new FileInputStream(file));
String extractedFilename = file.getName().split(".gz")[0];
OutputStream fosObj = new FileOutputStream(destinationPath + extractedFilename);
int len;
while ((len = gZipObj.read(buff)) > 0) {
fosObj.write(buff, 0, len);
}
gZipObj.close();
fosObj.close();
} catch (Exception e) {
logger.info("GZ Exception : {}",e.getMessage());
}
}
}
我收到意外 ZLIB stream 的错误,但文件已成功提取。 我尝试了一些解决方案,但没有一个解决了这个问题。 我在阅读之前尝试关闭 gzip stream,因为我从这里的一个答案中发现了这一点。 但这当然会引发另一个错误。
我很困惑为什么我会得到这个,我想基本上消除错误声明。
[pool-1-thread-1] INFO service.ExtractorImpl.GzExtractor - GZ Exception : Unexpected end of ZLIB input stream
好的,可能压缩文件的格式不正确。 我正在使用 FTP 服务器上传不同类型的文件,即 zip、gzip、CSV 等。根据我的逻辑,文件的压缩类型会根据文件进行解压。 从我的 FTP 服务器下载时,我忘了提及必须是二进制文件才能包含 zip 文件的文件类型。
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
所以被解压的文件不能是正确的格式。 也许这就是我收到此错误的原因。
将文件类型设置为此后,它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.