我从互联网上下载了一个zip文件并保存在内部存储中。当我尝试读取zip文件时,我不断收到EOFException。另外,我注意到从下面的代码段中的file.length()获得的长度大于实际文件。
这是我的代码段。
File file = new File(filePath + "/" + zipFile);
long len = file.length();
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zipInputStream = new ZipInputStream(bis);
答案 0 :(得分:1)
您可以使用此方法
public static void unzipFile(ZipFile zipFile, File destinationPath) {
Enumeration files = zipFile.entries();
File f = null;
FileOutputStream fos = null;
while (files.hasMoreElements()) {
try {
ZipEntry entry = (ZipEntry) files.nextElement();
InputStream eis = zipFile.getInputStream(entry);
byte[] buffer = new byte[1024];
int bytesRead = 0;
f = new File(destinationPath.getAbsolutePath() + File.separator + entry.getName());
if (entry.isDirectory()) {
f.mkdirs();
continue;
}
else {
f.getParentFile().mkdirs();
f.createNewFile();
}
fos = new FileOutputStream(f);
while ((bytesRead = eis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
catch (IOException e) {
Log.e(0, DEBUG_TAG, e.getMessage());
continue;
}
finally {
if (fos != null) {
try {
fos.close();
}
catch (IOException e) {
Log.e(0, DEBUG_TAG, e.getMessage());
}
}
}
}
}