Android解压文件包:

importandroid.content.Context;importandroid.os.Environment;importorg.json.JSONArray;importorg.json.JSONObject;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.Enumeration;importjava.util.List;importjava.util.zip.ZipEntry;importjava.util.zip.ZipFile;/***此工具类可提供压缩与解压缩功能**/publicclassZipTool{@SuppressWarnings("unchecked")publicstaticvoidunzip(StringzipFileName,StringoutputDirectory)throwsIOException{ZipFilezipFile=null;try{zipFile=newZipFile(zipFileName);Enumeratione=zipFile.entries();ZipEntryzipEntry=null;Filedest=newFile(outputDirectory);dest.mkdirs();while(e.hasMoreElements()){zipEntry=(ZipEntry)e.nextElement();StringentryName=zipEntry.getName();InputStreamin=null;FileOutputStreamout=null;try{if(zipEntry.isDirectory()){Stringname=zipEntry.getName();name=name.substring(0,name.length()-1);Filef=newFile(outputDirectory+File.separator+name);f.mkdirs();}else{intindex=entryName.lastIndexOf("\\");if(index!=-1){Filedf=newFile(outputDirectory+File.separator+entryName.substring(0,index));df.mkdirs();}index=entryName.lastIndexOf("/");if(index!=-1){Filedf=newFile(outputDirectory+File.separator+entryName.substring(0,index));df.mkdirs();}Filef=newFile(outputDirectory+File.separator+zipEntry.getName());//f.createNewFile();in=zipFile.getInputStream(zipEntry);out=newFileOutputStream(f);intc;byte[]by=newbyte[1024];while((c=in.read(by))!=-1){out.write(by,0,c);}out.flush();}}catch(IOExceptionex){ex.printStackTrace();thrownewIOException("解压失败:"+ex.toString());}finally{if(in!=null){try{in.close();}catch(IOExceptionex){}}if(out!=null){try{out.close();}catch(IOExceptionex){}}}}}catch(IOExceptionex){ex.printStackTrace();thrownewIOException("解压失败:"+ex.toString());}finally{if(zipFile!=null){try{zipFile.close();}catch(IOExceptionex){}}}}publicstaticStringgetPath(Contextcontext){booleanhasSD=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);StringrootPath=hasSD?(Environment.getExternalStorageDirectory().getPath()+File.separator+"Android"+File.separator+"data"+File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+"download"+File.separator):(context.getFilesDir().getPath()+File.separator+"Android"+File.separator+"data"+File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+"download"+File.separator);Filedownload=newFile(rootPath);if(!download.exists()){download.mkdir();}returnrootPath;}publicstaticStringgetPath(Contextcontext,Stringstr){booleanhasSD=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);StringrootPath=hasSD?(Environment.getExternalStorageDirectory().getPath()+File.separator+"Android"+File.separator+"data"+File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+str+File.separator):(context.getFilesDir().getPath()+File.separator+"Android"+File.separator+"data"+File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+str+File.separator);Filedownload=newFile(rootPath);if(!download.exists()){download.mkdir();}returnrootPath;}publicstaticStringgetSrcPath(Contextcontext,Stringpark){booleanhasSD=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);StringrootPath=hasSD?(Environment.getExternalStorageDirectory().getPath()+File.separator+"Android"+File.separator+"data"+File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+"download"+File.separator+"park"+park+File.separator):(context.getFilesDir().getPath()+File.separator+"Android"+File.separator+"data"+File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+"download"+File.separator+"park"+park+File.separator);Filedownload=newFile(rootPath);if(!download.exists()){download.mkdir();}returnrootPath;}publicstaticbooleanisFileExist(StringfileName){Filefile=newFile(fileName);file.isFile();returnfile.exists();}/***清空压缩包资源列表*/publicstaticbooleanclearZip(Contextcontext,Stringpark){Stringpath=getSrcPath(context,park);FilesrcDir=newFile(path);String[]children=srcDir.list();for(inti=0;i<children.length;i++){if(!newFile(srcDir,children[i]).delete()){returnfalse;}}returntrue;}publicstaticJSONArraygetSrcInfo(Contextcontext){Stringpath=getPath(context);FilesrcDir=newFile(path);JSONArrayjary=newJSONArray();if(srcDir.list().length!=0){String[]arg=srcDir.list();for(Stringt:arg){try{JSONObjectjo=newJSONObject();Filefile=newFile(path+t+File.separator);doublesize=getFileSizes(file);if(t.startsWith("park")){jo.put("name",t);jo.put("size",size);jary.put(jo);}}catch(Exceptione){}}}returnjary;}publicstaticList<String>getSrcList(Contextcontext){Stringpath=getPath(context);FilesrcDir=newFile(path);String[]arg=srcDir.list();List<String>list=newArrayList<String>();for(Stringt:arg){list.add(t);}returnlist;}publicstaticvoiddelZip(Contextcontext){Stringpath=getPath(context)+"temp"+File.separator;FilesrcDir=newFile(path);srcDir.delete();}/***递归删除掉文件夹及旗下子目录**@paramfile*/publicstaticvoiddelSrc(Filefile){if(file.isFile()){file.delete();return;}if(file.isDirectory()){File[]childFile=file.listFiles();if(childFile==null||childFile.length==0){file.delete();return;}for(Filef:childFile){delSrc(f);}file.delete();}}publicstaticlonggetFileSizes(Filef)throwsException{longsize=0;Fileflist[]=f.listFiles();for(inti=0;i<flist.length;i++){if(flist[i].isDirectory()){size=size+getFileSizes(flist[i]);}else{size=size+getFileSize(flist[i]);}}returnsize;}publicstaticlonggetFileSize(Filefile)throwsException{longsize=0;if(file.exists()){FileInputStreamfis=null;fis=newFileInputStream(file);size=fis.available();}else{file.createNewFile();}returnsize;}}


更多相关文章

  1. Ubuntu搭建Eclipse+JDK+SDK的Android
  2. Ubuntu搭建Eclipse+JDK+SDK的Android
  3. Android(安卓)Studio ADB响应失败解决方法
  4. Ubuntu搭建Eclipse+JDK+SDK的Android
  5. [camera drv]工厂模式下测试camera fail
  6. Qt for android 安装APP出现Failure [INSTALL_FAILED_USER_RESTR
  7. mac 上 配置 Android(安卓)NDK 环境
  8. android中ramdisk解压与打包
  9. [置顶] 自定义的解压进度条 关于ProgressBar的使用

随机推荐

  1. AIDL的实例,看一看结构
  2. 如何选好Android开发书籍和教程[总结]
  3. Delphi XE5 Android 运行黑屏卡死的解决
  4. Android客户端与java服务端AES加解密
  5. Android地图应用新视界--mapbox的应用开
  6. EventBus 《三》 事件的具体用法及其讲解
  7. Android(安卓)Studio JNI开发
  8. android 逆向 去广告
  9. android mp3播放器学习之预备知识1:activi
  10. Android设置TextView的行间距、行高。