[英]How to fix EOF read error with GZIPInputStream
I am trying to read the contents of a gzip file and create a file from it.我正在尝试读取 gzip 文件的内容并从中创建一个文件。 I'm running into an issue that I can't see to figure out.
我遇到了一个我无法弄清楚的问题。 Any ideas of suggestion is appreciated.
任何建议的想法表示赞赏。 Thank you.
谢谢你。
private static String unzip(String gzipFile, String location){
try {
FileInputStream in = new FileInputStream(gzipFile);
FileOutputStream out = new FileOutputStream(location);
GZIPInputStream gzip = new GZIPInputStream(in);
byte[] b = new byte[1024];
int len;
while((len = gzip.read(b)) != -1){
out.write(buffer, 0, len);
}
out.close();
in.close();
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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:116)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
You'll make life much easier on yourself by using Resource Blocks to ensure your files are closed correctly.通过使用资源块来确保正确关闭文件,您将使自己的生活更轻松。 For example:
例如:
private static String unzip(String gzipFile, String location){
try (
FileInputStream in = new FileInputStream(gzipFile);
GZIPInputStream gzip = new GZIPInputStream(in);
FileOutputStream out = new FileOutputStream(location))
{
byte[] b = new byte[4096];
int len;
while((len = gzip.read(b)) >= 0){
out.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
You should also ensure you've got a valid.zip file (of course.) and that your input and output filenames are different.您还应该确保您有一个有效的.zip 文件(当然。)并且您的输入和 output 文件名不同。
And what's going on with "buffer"? “缓冲区”是怎么回事? I assume (as does GPI) you probably meant "b"?
我假设(和 GPI 一样)您可能的意思是“b”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.