700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java 万能解压zip工具类

java 万能解压zip工具类

时间:2020-10-06 08:06:15

相关推荐

java 万能解压zip工具类

项目中有需要解压zip包,但是网上搜了下,工具类过于局限,故整理一个万能的解压zip的工具类。如果大家有更好的方式欢迎交流!

工具类1:

/*** 解压文件到指定目录** @param zipFile 压缩文件* @param descDir 指定目录*/public static void unZipFiles(File zipFile, String descDir) throws IOException {File pathFile = new File(descDir);if (!pathFile.exists()) {Assert.state(pathFile.mkdirs(), "无法创建目录/文件:" + pathFile);}/** ZipFile类用于从zip文件中读取条目* getEntries()返回ZIP文件条目中的枚举*/ZipFile zip = new ZipFile(zipFile);for (Enumeration entries = zip.getEntries(); entries.hasMoreElements(); ) {ZipEntry entry = (ZipEntry) entries.nextElement();//解决Linux乱码entry.setUnixMode(644);String zipEntryName = entry.getName();log.debug(zipEntryName);InputStream in = zip.getInputStream(entry);String outPath = (descDir + zipEntryName).replaceAll("\\*", "/");//判断路径是否存在,不存在则创建文件路径File file = new File(outPath.substring(0,outPath.lastIndexOf(File.separator)));if (!file.exists()) {Assert.state(file.mkdirs(), "无法创建目录/文件:" + file.getAbsolutePath());}//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压if (new File(outPath).isDirectory()) {continue;}OutputStream out = new FileOutputStream(outPath);byte[] buf = new byte[BUFFER_SIZE];int len;//Java获取byte数值保存到指定目录while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}in.close();out.close();}log.info("*******************解压完毕********************");}

局限点:只支持解压zip包中只包含文件,不包含文件夹的zip

工具类2:

/*** 解压文件到指定目录** @param zipFile 压缩文件* @param descDir 指定目录*/public static void unZipFiles(File zipFile, String descDir) throws IOException {File pathFile = new File(descDir);if (!pathFile.exists()) {Assert.state(pathFile.mkdirs(), "无法创建目录/文件:" + pathFile);}/** ZipFile类用于从zip文件中读取条目* getEntries()返回ZIP文件条目中的枚举*/ZipFile zip = new ZipFile(zipFile);for (Enumeration entries = zip.getEntries(); entries.hasMoreElements(); ) {ZipEntry entry = (ZipEntry) entries.nextElement();//解决Linux乱码entry.setUnixMode(644);String zipEntryName = entry.getName();log.debug(zipEntryName);InputStream in = zip.getInputStream(entry);String outPath = (descDir + zipEntryName).replaceAll("\\*", "/");//判断路径是否存在,不存在则创建文件路径File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));if (!file.exists()) {Assert.state(file.mkdirs(), "无法创建目录/文件:" + file.getAbsolutePath());}//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压if (new File(outPath).isDirectory()) {continue;}OutputStream out = new FileOutputStream(outPath);byte[] buf = new byte[BUFFER_SIZE];int len;//Java获取byte数值保存到指定目录while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}in.close();out.close();}log.info("*******************解压完毕********************");}

局限点:只能解压zip包下为文件夹的zip

工具类3:(推荐)

/*** 解压文件到指定目录,包含解压带文件夹和不带文件夹的zip(万能)** @param srcFile 压缩文件* @param destDirPath 指定目录*/public static void unZipFilesSecond(File srcFile,String destDirPath) throws Exception {if (!srcFile.exists()) {throw new Exception(srcFile.getPath() + "所指文件不存在");}//创建压缩文件对象ZipFile zipFile = new ZipFile(srcFile);log.info("*******************开始解压********************");Enumeration<?> entries = zipFile.getEntries();while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();// 如果是文件夹,就创建个文件夹if (entry.isDirectory()) {String dirPath = destDirPath + "/" + entry.getName();srcFile.mkdirs();} else {// 如果是文件,就先创建一个文件,然后用io流把内容copy过去File targetFile = new File(destDirPath + "/" + entry.getName());// 保证这个文件的父文件夹必须要存在if (!targetFile.getParentFile().exists()) {targetFile.getParentFile().mkdirs();}targetFile.createNewFile();// 将压缩文件内容写入到这个文件中InputStream is = zipFile.getInputStream(entry);FileOutputStream fos = new FileOutputStream(targetFile);int len;byte[] buf = new byte[1024];while ((len = is.read(buf)) != -1) {fos.write(buf, 0, len);}// 关流顺序,先打开的后关闭fos.close();is.close();}}log.info("*******************解压完毕********************");}

可以解压zip包下含有文件和文件夹的zip

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。