【问题标题】:android how to find version number from file downloaded for FOTA updateandroid如何从为FOTA更新下载的文件中查找版本号
【发布时间】:2014-08-13 15:42:49
【问题描述】:
我只想控制特定版本的 android 根设备的固件升级。
我知道,要更新固件,请在从设备制造商处下载 zip 后,致电RecoverySystem.installPackage(Context context, File packageFile)
在那里,我计划检查将安装在设备上的较新版本,即
设备将升级到的固件版本号。
那么我如何从下载的 zip 文件中获取版本信息。
【问题讨论】:
标签:
android
download
zip
version
firmware
【解决方案1】:
我找到了。
每个 FOTA zip 都包含名为“元数据”的文件,位于“/META-INF/com/android/”位置。该文件的第一行包含版本信息,包括版本号。
您可以通过以下代码获得。
/**
* API to unzip package file and retrieve version no from
* /META-INF/com/android/metadata file inside zip
*
* @param File
*
* @return String versioNo
*/
private static String getVersion(File packageFile) {
String versionNo = null;
if (packageFile != null && packageFile.exists()) {
String firstLine = getFirstLine(packageFile);
Log.d(TAG, "in getVersion. firstLine: " + firstLine);
if (TextUtils.isEmpty(firstLine) == false) {
String[] arrStr = firstLine.split("/");
if (arrStr.length > 1) {
String[] data = arrStr[2].split(":");
if (data.length > 0) {
return data[1];
}
}
}
}
return versionNo;
}
/**
* API to unzip package file and retrieve first line from
* /META-INF/com/android/metadata file
*
* @param File
*
* @return String versioNo
*/
private static String getFirstLine(File packageFile) {
ZipInputStream zin = null;
FileInputStream fin = null;
try {
fin = new FileInputStream(packageFile.getPath());
zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
File f = new File(packageFile.getCanonicalPath());
if (!f.isDirectory()) {
f.mkdirs();
}
} else {
String name = ze.getName();
String[] arr;
if (TextUtils.isEmpty(name)) {
continue;
} else {
arr = name.split("/");
}
if (arr.length < 1) {
continue;
}
if ("metadata".equals(arr[arr.length - 1])) {
InputStreamReader isr = null;
BufferedReader buf = null;
try {
isr = new InputStreamReader(zin, "UTF-8");
buf = new BufferedReader(isr);
return buf.readLine();
} catch (IOException e) {
e.getStackTrace();
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (buf != null) {
try {
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
zin.closeEntry();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}