ZIP/RAR解压缩(JAVA)
来源:互联网 发布:网络交友有利有弊 英语 编辑:程序博客网 时间:2024/06/03 15:52
ZIP/RAR解压缩工具类方法,以此作为自己的第一篇博客,也算作为一个记录吧。
/** * 上传打包文件(支持rar和zip),解压到服务器/本地 * @param folderPath 路径,以“/”结尾 * @param file 文件 * @param fileType 文件类型(rar/zip) * @return * @throws Exception */public static String putPackObject(String folderPath,File file,String fileType) throws Exception{ if(folderPath==null || file==null || fileType==null) return null; //文件夹名(返回目录参数) String destDirName = folderPath+file.getName().substring(0,file.getName().lastIndexOf("."))+"/"; try { //解压文件 if("rar".equals(fileType.toLowerCase())){ Archive a = null; FileOutputStream fos = null; try { a = new Archive(file); FileHeader fh = a.nextFileHeader(); while (fh != null) { if (!fh.isDirectory()) {//文件 String fileName = null; if(fh.isUnicode()){//解決中文乱码 fileName = fh.getFileNameW().trim().replace("\\", "/"); }else{ fileName = fh.getFileNameString().trim().replace("\\", "/"); } //文件类型验证 if(fileName.toLowerCase().endsWith(".rar") || fileName.toLowerCase().endsWith(".zip")){ throw new Exception("rar file error"); } //本地文件路径 String destFilePath = destDirName+fileName; //创建本地文件 File f = new File(destFilePath.replace("/", File.separator)); if(!f.exists() && !f.isDirectory()) { //判断目标文件所在的目录是否存在 if(!f.getParentFile().exists()) { //如果目标文件所在的目录不存在,则创建父目录 if(!f.getParentFile().mkdirs()) throw new Exception("create folder error"); } }else{ fh = a.nextFileHeader(); continue; } fos = new FileOutputStream(f); a.extractFile(fh, fos); fos.close(); fos = null; } fh = a.nextFileHeader(); } a.close(); a = null; } catch (Exception e) { throw e; } finally { if (a!=null) { a.close(); a = null; } } }else if("zip".equals(fileType.toLowerCase())){ ZipFile readfile = null; try { //文件 readfile = new ZipFile(file); Enumeration<ZipEntry> takeentrie = readfile.getEntries(); ZipEntry zipEntry = null; while (takeentrie.hasMoreElements()){ zipEntry = (ZipEntry) takeentrie.nextElement(); InputStream in = null; FileOutputStream out = null; try { if(!zipEntry.isDirectory()){//文件 String filePath = zipEntry.getName().replace(File.separator, "/"); //文件类型验证 if(filePath.toLowerCase().endsWith(".zip") || filePath.toLowerCase().endsWith(".rar")){ throw new Exception("zip file error"); } //本地文件路径 String destFilePath = destDirName + filePath; //创建本地文件 File unpackfile = new File(destFilePath.replace("/", File.separator)); if(!unpackfile.exists() && !unpackfile.isDirectory()) { //判断目标文件所在的目录是否存在 if(!unpackfile.getParentFile().exists()) { //如果目标文件所在的目录不存在,则创建父目录 if(!unpackfile.getParentFile().mkdirs()) throw new Exception("create folder error"); } }else{ continue; } in = readfile.getInputStream(zipEntry); out = new FileOutputStream(unpackfile); int c; byte[] by = new byte[1024]; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } out.flush(); } } catch (IOException ex) { throw new IOException("extract file error:" + ex.toString()); } finally { if (in!=null) { in.close(); in=null; } if (out!=null) { out.close(); out=null; } } } } catch (IOException ex) { ex.printStackTrace(); throw new IOException("extract file error:" + ex.toString()); } finally { if (readfile != null) { readfile.close(); } } }else{ throw new Exception("file type error"); } }catch (Exception e) { throw new Exception(e.getMessage()); } return destDirName;}
注:该方法只是简单的解压缩文件,权当做参考,具体功能代码还需根据业务需求来写。
相关JAR包
1、ant-1.9.7.jar(解压zip)
2、junrar-0.7.jar(解压rar)
0 0
- ZIP/RAR解压缩(JAVA)
- java 实现解压缩文件(ZIP/rar)
- Java 解压rar 解压缩zip
- java压缩解压缩rar、zip文件
- 关于java解压缩文件(rar或者zip)的实现示例
- 关于java解压缩文件(rar或者zip)的实现示例
- 用java解压缩zip和rar的工具类
- 用java解压缩zip和rar的工具类
- java压缩解压缩rar、zip 、tar文件 工具类源码
- java 利用系统WinRAR解压缩(zip/rar压缩文件)
- 压缩、解压缩.zip,.rar,.7z格式java工具类
- java实现对rar文件和zip文件的解压缩
- linux rar解压缩 zip压缩
- Java解压缩-Rar文件
- PHP执行zip与rar解压缩方法
- linux下解压缩rar、zip文件方法
- Linux解压缩命令RAR,ZIP,TAR
- java解压zip、rar(多级文件)
- Vim操作摘记-光标移动、Mark、选中
- 分层架构设计思想
- Mac OS上使用串口(serial port)和虚拟机通讯
- java 字符串截取的三种方式
- WebStorm强大的调试JavaScript功能
- ZIP/RAR解压缩(JAVA)
- MVC的开发模式
- ShowSDK一键分享
- 自定义Dialog 去掉标题
- SPOJ LEONARDO Leonardo Notebook
- ArcGIS Server如何以缓存的方式发布地图
- 全面解析微信小程序
- 安卓BroadcastReceiver Kill 掉第三方app服务
- Linux终端监控工具dstat