如有不对的地方,望各路大神指点,小女子在此谢过O(∩_∩)O
点击此处下载demo,看完整代码,(*^__^*) 嘻嘻……
1.下载文件
/**
* 下载zip文件
*
* @param url
*/
private void downloadFile(final String url) {
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS);
Request request = new Request.Builder().url(url).build();
builder.build().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[4096];
int len = 0;
FileOutputStream fos = null;
// 储存下载文件的目录
String savePath = Tools.isExistDir_html(filePath);
try {
is = response.body().byteStream();
long total = response.body().contentLength();
// File file = new File(savePath, getNameFromUrl(url));
File file = new File(savePath, getNameFromUrl(fileName));
fos = new FileOutputStream(file);
long sum = 0;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
sum += len;
int progress = (int) (sum * 1.0f / total * 100);
// 下载中
}
fos.flush();
} catch (Exception e) {
//
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
}
}
// 下载完成,解压zip
//下载完成,先删除过时数据,并解压最新的热更新数据(必须这样操作 不然会导致无法覆盖旧数据,导致数据更新失败)
deleteOldData();
}
});
}
删除旧数据deleteOldData
private void deleteOldData() {
String dirPath = Constants.BLACKTECH_HOT_UPDATE_FILE_PATH;
List files = CommandUtils.getFilesInDirectory(dirPath);
for (File file : files) {
String fileName = file.getName();
if (fileName.endsWith(Constants.UXUE_TEMP_FILE_SUFFIX)) {
// String type = fileName.substring(0, fileName.indexOf("_"));
// CommandUtils.deleteDirectory(dirPath + fileName);
CommandUtils.deleteDirectory(dirPath);
try {
ZipFileUtil.upZipFile(file, dirPath);
file.delete();
} catch (Exception e) {
e.printStackTrace();
// Logcat.e("解压文件失败:" + file.getPath());
}
}
}
}
/**
* 获取目录中的所有一级文件(不包括目录和嵌套文件)
*
* @param sPath
* @return
*/
public static List getFilesInDirectory(String sPath) {
List result = new ArrayList<>();
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return result;
}
File[] files = dirFile.listFiles();
for (File file : files) {
if (file.isFile()) {
result.add(file);
}
}
return result;
}
/**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
// 删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; files != null && i < files.length; i++) {
if (files[i].getName().equals("YJHtml.zip")){
continue;
}
// 删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
} // 删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
// 删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
/**
* 删除单个文件
*
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
解压zip
/**
* 解压缩功能.
* 将zipFile文件解压到folderPath目录下.
*
* @throws Exception
*/
public static int upZipFile(File zipFile, String folderPath) throws Exception {
try {
ZipFile zfile = new ZipFile(zipFile);
Enumeration zList = zfile.entries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
//Logcat.d("upZipFile", "ze.getName() = " + ze.getName());
String dirstr = folderPath + ze.getName();
//dirstr.trim();
dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
//Logcat.d("upZipFile", "str = " + dirstr);
File f = new File(dirstr);
f.mkdir();
continue;
}
//Logcat.d("upZipFile", "ze.getName() = " + ze.getName());
OutputStream os = new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zfile.close();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
注意:解压时一定要先删除之前本地的文件里的数据,然后再进行解压,不然会导致无法覆盖的情况