/** * 解压缩zip包 * * @param zipFilePath zip文件路径 * @param targetPath 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下 * @throws IOException * @author yayagepei * @date 2008-9-28 */ public String unZip(String zipFilePath, String targetPath) throws IOException { OutputStream os = null; InputStream is = null; ZipFile zipFile = null; String resultPath = null; try { zipFile = new ZipFile(zipFilePath,"GBK"); String directoryPath = ""; if (null == targetPath || "".equals(targetPath)) { directoryPath = zipFilePath.substring(0, zipFilePath.lastIndexOf(".")); } else { directoryPath = targetPath; } Enumeration<ZipEntry> entryEnum = zipFile.getEntries(); if (null != entryEnum) { ZipEntry zipEntry = null; while (entryEnum.hasMoreElements()) { zipEntry = (ZipEntry) entryEnum.nextElement(); if (zipEntry.isDirectory()) { continue; } if (zipEntry.getSize() > 0) { // 文件 File targetFile = buildFile(directoryPath + File.separator + zipEntry.getName(), false); if (zipEntry.getName().contains("webapps") && "index.html".equals(targetFile.getName())) { resultPath = targetFile.getAbsolutePath(); } os = new BufferedOutputStream(new FileOutputStream(targetFile)); is = zipFile.getInputStream(zipEntry); byte[] buffer = new byte[4096]; int readLen = 0; while ((readLen = is.read(buffer, 0, 4096)) >= 0) { os.write(buffer, 0, readLen); } os.flush(); os.close(); } else { } } } } catch (IOException ex) { throw ex; } finally { if (null != zipFile) { zipFile = null; } if (null != is) { is.close(); } if (null != os) { os.close(); } } return resultPath; } /** * 解压rar格式压缩包。 对应的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar */ public String unrar(String sourceRar, String destDir) throws Exception { String resultPath = null; Archive a = null; FileOutputStream fos = null; try { a = new Archive(new File(sourceRar)); if (null == destDir || "".equals(destDir)) { destDir = a.getFile().getParentFile().getAbsolutePath(); } FileHeader fh = a.nextFileHeader(); while (fh != null) { if (!fh.isDirectory()) { // 1 根据不同的操作系统拿到相应的 destDirName 和 destFileName String compressFileName = ""; if (fh.isUnicode()) { compressFileName = fh.getFileNameW().trim(); } else { compressFileName = fh.getFileNameString().trim(); } String destFileName = ""; String destDirName = ""; // 非windows系统 if (File.separator.equals("/")) { destFileName = destDir +File.separator+ compressFileName.replaceAll("\\\\", "/"); destDirName = destFileName.substring(0, destFileName.lastIndexOf("/")); // windows系统 } else { destFileName = destDir + compressFileName.replaceAll("/", "\\\\"); destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\")); } // 2创建文件夹 File dir = new File(destDirName); if (!dir.exists() || !dir.isDirectory()) { dir.mkdirs(); } File destFile=new File(destFileName); // 3解压缩文件 fos = new FileOutputStream(destFile); if ("index.html".equals(destFile.getName())) { resultPath = destFileName; } a.extractFile(fh, fos); fos.close(); fos = null; } fh = a.nextFileHeader(); } a.close(); a = null; } catch (Exception e) { throw e; } finally { if (fos != null) { try { fos.close(); fos = null; } catch (Exception e) { e.printStackTrace(); } } if (a != null) { try { a.close(); a = null; } catch (Exception e) { e.printStackTrace(); } } } return resultPath; } /** * 生产文件 如果文件所在路径不存在则生成路径 * * @param fileName 文件名 带路径 * @param isDirectory 是否为路径 * @return * @author yayagepei * @date 2008-8-27 */ public File buildFile(String fileName, boolean isDirectory) { File target = new File(fileName); if (isDirectory) { target.mkdirs(); } else { if (!target.getParentFile().exists()) { target.getParentFile().mkdirs(); if (target.exists()) { target.delete(); } target = new File(target.getAbsolutePath()); } } return target; }
相关推荐
java解压缩 rar 以及 zip 格式文件
java实现的解压与压缩的类 其中有zip和rar类型的 还包括了所需的架包
java rar解压 zip解压 工具
使用Java解压文件 支持RAR5,Zip,7z ,代码只实现了这个三种,但是要想支持更多的可以查看代码。 支持跨平台。 以前的通用java解压都是坑人的,无语了。
java 压缩/解压 .zip/.rar/.tar 文件,大家一起讨论,学习 已实现zip压缩/解压,rar解压
java上传文件 实时获取上传进度 上传RAR,ZIP 并指定解压路劲 上传之后会按照路劲进行解压
Java批量解压RAR或zip文件,压缩包内包含包括源码和jar包。 如发现其他人或组织有商用目的,必追究其法律责任。
NULL 博文链接:https://hogwartsrow.iteye.com/blog/1853355
java ZIP和RAR 压缩包 目录结构。 1.ant.jar 解决java自带zip不能读取中文压缩包的问题; 2.需要安装WINRAR软件,以便解压rar文件,然后获取对应目录; 3.实现在线预览压缩包目录结构的功能;
java解压缩rar原码
javaandroid可用的ziprar解压缩代码实现.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
我这个资源是也是从网上下的,下下来之后在单个测试类里面解压rar是没问题的,但是在Action里面调就抛java.lang.OutOfMemoryError: Java heap space,所以自己换了种解压rar的方法,以前的方法注释掉了。action里面...
java项目中常使用到的工具类:zip压缩解压缩、rar解压(有密码的文件)、gz解压、FTP上传与下载
java 解压zip,rar文件 ,java 解压缩包,把压缩包的文件及文件夹的子文件全部压到指定目录
实现JAVA解压缩带密码的RAR压缩包,支持中文,支持Window、Linux平台
沙漏哦2022-09-16 16:00:57原证Java解压缩文件,尤其对于Rar5.0版本的解压实现 项目描述 junrar已经不再支持Rar5版本的压缩文件。sevenzipjbinding对于Rar5压缩包的解压是调用本地7z软件进行解压的,如果本机环境...
Java解压缩文件,并以ZIP格式压缩文件,主要是使用java.util.zip 包中的类来实现解压、压缩文件功能,如果你对这个类并不太熟悉,你正好可以参考一下这个类是如何用的。
绝对实用的封装,自己写了一下午的代码,希望对大家有所帮助
本篇文章主要介绍了详解Java无需解压直接读取Zip文件和文件内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java本身自带有zip格式的压缩解压api,但是对于含有中文的压缩文件无能为力,好在还有apache的ant可以解决zip文件的中文乱码问题。mucommander是一个可以支持zip、gzip、rar、tar、iso等格式的全能工具,这个包是...