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. Android获取activity的所有子view(使用到递归)
  2. Android文件递归遍历
  3. Android drawable 目录下 创建子目录问题
  4. devexpress TreeList递归的实例代码
  5. c语言递归算法怎么实现
  6. 【C语言】递归和非递归分别实现strlen
  7. 递归算法的时间复杂度是什么
  8. c语言允许函数的递归调用吗
  9. c语言有哪些递归函数的例子?

随机推荐

  1. Android 高级UI解密 (三) :Canvas裁剪 与
  2. Android中隐藏标题栏和状态栏(电源,信号,时
  3. android studio 使用NDK和swig编译c++示
  4. Android深度定制化TabLayout:圆角,渐变色,背
  5. Android: Android学习的几点建议
  6. Android的数据处理:使用annotation实现JSO
  7. Android Retrofit与Spring后台配合,实现单
  8. Android如何获取APP启动时间
  9. [置顶] Android防止内存溢出浅析
  10. Java OR Android