我必须解压缩我正在使用以下代码的.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)
令人惊讶的是,该文件仍然被解压缩并存储在本地位置。我不希望将损坏的文件解压缩或进一步处理。
一种方法是在使用java.io.EOFException
命中catch子句时删除newFile对象,但这是正确的方法吗?当文件没有损坏时,也可能存在一些其他可能的异常。