// obb
public static String getObbFilePath(Context context) {
try {
Log.d("bbt", "getObbFilePath : packetName=" + context.getPackageName());
Log.d("bbt", "getObbFilePath : absolutePath=" + Environment.getExternalStorageDirectory().getAbsolutePath());
Log.d("bbt", "getObbFilePath : versionCode=" + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/Android/obb/"
+ context.getPackageName()
+ File.separator
+ "main."
+ context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode
+ "."
+ context.getPackageName()
+ ".obb";
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void createDirectoryIfNeeded(String folderPath) {
File folder = new File(folderPath);
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdirs();
}
}
public static void unZip(File zipFile, String outPathString) {
Log.d("bbt", "unZip " + zipFile.getName() + " to " + outPathString);
try {
createDirectoryIfNeeded(outPathString);
ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry zipEntry;
String szName;
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPathString + File.separator + szName);
folder.mkdirs();
} else {
File file = new File(outPathString + File.separator + szName);
createDirectoryIfNeeded(file.getParent());
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
while ((len = inZip.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
inZip.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void unZipObb(String srcFilePath, String dstFolderPath) {
Log.d("bbt", "unZipObb : srcFilePath=" + srcFilePath + ", dstFolderPath="+dstFolderPath);
Context context = m_pActivity;
if (context == null) {
Log.d("bbt", "unZipObb error : context == null");
return;
}
try {
String obbFilePath = srcFilePath;
if (obbFilePath.equals("")) {
obbFilePath = getObbFilePath(context);
}
if (obbFilePath == null) {
Log.d("bbt", "unZipObb error : obbFilePath == null");
return;
} else {
File obbFile = new File(obbFilePath);
if (!obbFile.exists()) {
//下载obb文件
Log.d("bbt", "unZipObb error : !obbFile.exists()");
} else {
File outputFolder = new File(dstFolderPath);
if (!outputFolder.exists()) {
//目录未创建 没有解压过
outputFolder.mkdirs();
unZip(obbFile, outputFolder.getAbsolutePath());
} else {
//目录已创建 判断是否解压过
if (outputFolder.listFiles() == null) {
//解压过的文件被删除
unZip(obbFile, outputFolder.getAbsolutePath());
} else {
//此处可添加文件对比逻辑
Log.d("bbt", "unZipObb error : outputFolder.listFiles() != null");
}
}
}
}
} catch (Exception e) {
Log.d("bbt", "unZipObb error : Exception");
e.printStackTrace();
}