Android 压缩解压zip文件
这个可以解压 那些包含子目录的zip文件
/** * 解压缩功能. * 将zipFile文件解压到folderPath目录下. * @throws Exception */ public int upZipFile(File zipFile, String folderPath)throws ZipException,IOException { //public static void upZipFile() throws Exception{ 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()){ Log.d("upZipFile", "ze.getName() = "+ze.getName()); String dirstr = folderPath + ze.getName(); //dirstr.trim(); dirstr = new String(dirstr.getBytes("8859_1"), "GB2312"); Log.d("upZipFile", "str = "+dirstr); File f=new File(dirstr); f.mkdir(); continue; } Log.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(); Log.d("upZipFile", "finishssssssssssssssssssss"); return 0; } /** * 给定根目录,返回一个相对路径所对应的实际文件名. * @param baseDir 指定根目录 * @param absFileName 相对路径名,来自于ZipEntry中的name * @return java.io.File 实际的文件 */ public static File getRealFileName(String baseDir, String absFileName){ String[] dirs=absFileName.split("/"); File ret=new File(baseDir); String substr = null; if(dirs.length>1){ for (int i = 0; i < dirs.length-1;i++) { substr = dirs[i]; try { //substr.trim(); substr = new String(substr.getBytes("8859_1"), "GB2312"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } ret=new File(ret, substr); } Log.d("upZipFile", "1ret = "+ret); if(!ret.exists()) ret.mkdirs(); substr = dirs[dirs.length-1]; try { //substr.trim(); substr = new String(substr.getBytes("8859_1"), "GB2312"); Log.d("upZipFile", "substr = "+substr); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } ret=new File(ret, substr); Log.d("upZipFile", "2ret = "+ret); return ret; } return ret; }
记得要在AndroidManifest.xml里添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2012-01-07 在EditText中插入表情图片 (CharacterStyle&SpannableString)
2012-01-07 Android Toast自定义
2012-01-07 Android TextView结合SpannableString使用大全