版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ToOak_And/article/details/51163530
原先写删除文件及清空文件夹的代码如下:
/**
* 删除文件夹
* @param filderPath 文件夹完整绝对路径
*/
public static void delFolder(String filderPath){
delAllFile(filderPath);//清空文件夹
File myFilePath = new File(filderPath);
myFilePath.delete();
}
/**
* 删除指定文件夹下所有文件 或者说:清空文件夹
* @param path 文件夹完整绝对路径
* @return
*/
public static boolean delAllFile(String path){
boolean flag = false;
File file = new File(path);
if(!file.exists()){
return flag;
}
if(!file.isDirectory()){
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0;i < tempList.length;i++){
if(path.endsWith(File.separator)){
temp = new File(path + tempList[i]);
}else {
temp = new File(path + File.separator + tempList[i]);
}
if(temp.isFile()){
temp.delete();
}
if (temp.isDirectory()){
delAllFile(path + File.separator + tempList[i]);//先删除文件夹里的文件
delFolder(path + File.separator + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
打算实现的效果是,先判断zip包中是否包含指定的文件夹,如果包含就把本地的文件夹删除,再解压压缩包。
判断zip包中是否有特定文件/文件夹的代码如下:
/**
* 判断压缩包中是否包含文件或文件夹(文件夹以“/”结尾)
* @param zipPath
* @param relativePath like:dist/
* or version.txt
* @return
*/
public static boolean isZipContain(String zipPath,String relativePath){
ZipFile zip = null;
try {
zip = new ZipFile(new File(zipPath),"GBK");
for(Enumeration entries = zip.getEntries();entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
String outPath = zipEntryName.replaceAll("\\*", "/");
Log.d("updatezip","outPath: "+outPath);
if ((outPath.toString()).equals(relativePath)) {
Log.d("updatezip", "bingo!!!!!!!!!!");
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
解压zip包的代码如下(解压引入的jar包为:apache-ant-zip.jar):
/**
* 解压文件到指定目录
* @param zipFile
* @param desDir
*/
private static boolean unZipFiles(File zipFile, String desDir) {
boolean result = false;
File pathFile = new File(desDir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
InputStream in = null;
OutputStream out = null;
try {
ZipFile zip = new ZipFile(zipFile,"GBK");
for(Enumeration entries = zip.getEntries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
in = zip.getInputStream(entry);
String outPath = (desDir + File.separator +zipEntryName).replaceAll("\\*", "/");
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
Log.d("updatezip","direc: "+outPath);
if ((outPath.toString()).equals(Environment.getExternalStorageDirectory().getPath() + "/cookie_test/jk/dist/")){
Log.d("updatezip","bingo!!!!!!!!!!");
// if (true){
// throw new NullPointerException();
// }
}
continue;
}
//输出文件路径信息
Log.d("updatezip","file: "+outPath);
out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
}
result = true;
} catch (Exception e) {
e.printStackTrace();
Log.d("updatezip","erro: "+e.getMessage());
}finally {
if (null != in){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
直接引用大神的代码,删除的逻辑现改为如下:
public static void deleteFile(File file) {
if (file.isFile()) {
deleteFileSafely(file);
return;
}
if (file.isDirectory()) {
File[] childFiles = file.listFiles();
if (childFiles == null || childFiles.length == 0) {
deleteFileSafely(file);
return;
}
for (int i = 0; i < childFiles.length; i++) {
deleteFile(childFiles[i]);
}
deleteFileSafely(file);
}
}
/**
* 安全删除文件.
* @param file
* @return
*/
private static boolean deleteFileSafely(File file) {
if (file != null) {
String tmpPath = file.getParent() + File.separator + System.currentTimeMillis();
File tmp = new File(tmpPath);
file.renameTo(tmp);
return tmp.delete();
}
return false;
}
现主流程改为:
/**
* 解压文件到指定目录
* @param zipPath 待解压的zip文件路径
* @param desDir 解压后的文件路径
*/
public static boolean UnZipFiles(String zipPath,String desDir){
final boolean isHasdist = isZipContain(zipPath, "dist/");
boolean isDeleSuccess;
boolean isUnzipSuccess;
Log.d("updatezip",isHasdist+"");
if (isHasdist){
// isDeleSuccess = FileDeleUtil.delAllFile(desDir+File.separator+"dist");
// Log.d("updatezip","isDeleSuccess:"+isDeleSuccess);
FileDeleUtil.deleteFile(new File(desDir+File.separator+"dist"));
}
isUnzipSuccess = unZipFiles(new File(zipPath), desDir);
Log.d("updatezip", "isUnzipSuccess:" + isUnzipSuccess);
return isUnzipSuccess;
}