我必须解压缩使用以下代码的 .gz 文件:
FileInputStream fis = null;
FileOutputStream fos = null;
GZIPInputStream gin = null;
try {
File file = new File(getPath(), zipName);
fis = new FileInputStream(file);
gin = new GZIPInputStream(fis);
File newFile = // some initialization related to path and name
fos = new FileOutputStream(getPath());
byte[] buf = new byte[1024];
int len;
while ((len = gin.read(buf)) > 0) {
fos.write(buf, 0, len);
}
gin.close();
fos.close();
//newFile is ready
} catch(IOException e){
// exception catch
}
但是,当客户端 gz 文件损坏时,我收到以下错误:
Exception in thread "main" java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.util.zip.GZIPInputStream.read(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
令人惊讶的是,该文件仍在解压缩并存储在本地位置。我根本不希望对损坏的文件进行解压缩或进一步处理。
一种方法是在 catch 子句命中时删除 newFile 对象java.io.EOFException
,但这是正确的方法吗?当文件没有损坏时,可能还有其他一些可能的例外。