Android 使用ZipInputStream 不解压压缩包的情况下,读取文件内容
程序员文章站
2022-06-04 08:01:53
...
1.压缩包有200mb,正好需要解析的文件在最后,读取文件内容很慢
private String getSystemUpgradeVersion() {
String zipFile = "mnt/sdcard/update.zip";
File f = new File(zipFile);
if (!f.exists()) {
return "";
}
try {
ZipFile zf = new ZipFile(zipFile);
InputStream in = new BufferedInputStream(new FileInputStream(
zipFile));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
if (!ze.isDirectory()) {
if (ze.getName().equals("version")) {
break;
}
}
}
if (zf == null || ze == null) {
return "";
}
BufferedReader br = new BufferedReader(new InputStreamReader(
zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
LogUtils.d(TAG, "======line: " + line);
return line;
}
br.close();
zin.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
2.如下这种方法 可以快速的遍历 所有文件,读取文件内容
private String getSystemUpgradeVersion() {
String zipFile = "mnt/sdcard/update.zip";
File f = new File(zipFile);
if (!f.exists()) {
return "";
}
try {
ZipFile zf = new ZipFile(zipFile);
InputStream in = new BufferedInputStream(new FileInputStream(
zipFile));
ZipInputStream zin = new ZipInputStream(in);
Enumeration enumeration = zf.entries();
ZipEntry ze = null;
while (enumeration.hasMoreElements()) {
ze = (ZipEntry) enumeration.nextElement();
if (!ze.isDirectory()) {
if (ze.getName().equals("version")) {
break;
}
}
}
if (zf == null || ze == null) {
return "";
}
BufferedReader br = new BufferedReader(new InputStreamReader(
zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
LogUtils.d(TAG, "======line: " + line);
return line;
}
br.close();
zin.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}