java zip压缩_压缩工具
由于rar4版本以上的无法通过java代码进行解压,采用调用本地程序来进行解压
第一步:下载winrar
第二步:配置环境变量,只需要配置到安装目录即可,,重新打开命令窗口
第三步:单个文件进行测试
解压:
unrar e a.rar //其实我这里没有写保存到哪里,就是解压到当前目录,可以解压到具体的位置( eg: unrar e a.rar D:/a)
第四步:多个文件
第五步:java程序进行调用(在window上测试没有问题)
public class Text { private static final String REAL_PATH = "D:\\WINRAR\\WinRAR.exe"; public static boolean winrar(String srcPath, String dirPath,boolean flag) { String cmd = flag ? REAL_PATH + " a -r " + srcPath + " " + dirPath : REAL_PATH + " e -r " + srcPath + " " + dirPath; try { Process proc = Runtime.getRuntime().exec(cmd); return true; } catch (Exception e) { e.printStackTrace(); } return false; } public static void main(String[] args) { boolean winrar = winrar("E:\\text\\a.rar", "E:\\text\\",false); System.out.println(winrar); } |
public boolean winrar(String srcPath, String dirPath, boolean flag) { String cmd = null; if (windowSystem) { cmd = flag ? WINRAR_REAL_PATH + " a -r " + srcPath + " " + dirPath : WINRAR_REAL_PATH + " e -r " + srcPath + " " + dirPath; }else { cmd = flag ? "rar" + " a -r " + srcPath + " " + dirPath : "rar" + " e -r " + srcPath + " " + dirPath; } try { Process proc = Runtime.getRuntime().exec(cmd); return true; } catch (Exception e) { e.printStackTrace(); } return false; } |
第六步:在linux上测试
在线安装
由于linux中系统出现问题,重新安装。。。。如果出现启动失败,就把自动生成的镜像移除或者禁止连接,修改配置:vi /etc/sysconfig/network-scripts/ 将值改为onboot=yes
解压tar -zxvf rarlinux-5.9.1.tar.gz
如果linux中出现没有这个make的命令
yum -y install gcc automake autoconf libtool make
Make
Make install
如果报错:rar不是处理的命令的语法
yum install libstdc++.so.6
如果安装出现版本的问题执行下面的命令在来执行上面的命令
yum install --setopt=protected_multilib=false libstdc++
Rar的命令正常了
完美解决:
使用springboot进行测试:
增加插件
org.springframework.boot spring-boot-maven-plugin com.spring.rar.rar_zip.App |
Clean package
到linux环境下测试:
开发端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --query-port=8080/tcp
重启服务
完美解决
核心代码:
package com.spring.rar.rar_zip.util; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class WinRarUtil { @Value("${WINRAR_REAL_PATH}") private String WINRAR_REAL_PATH; @Value("${windowSystem}") private boolean windowSystem; public boolean winrar(String srcPath, String dirPath, boolean flag) { String cmd = null; if (windowSystem) { cmd = flag ? WINRAR_REAL_PATH + " a -r " + srcPath + " " + dirPath : WINRAR_REAL_PATH + " e -r " + srcPath + " " + dirPath; }else { cmd = flag ? "rar" + " a -r " + srcPath + " " + dirPath : "rar" + " e -r " + srcPath + " " + dirPath; } try { Process proc = Runtime.getRuntime().exec(cmd); return true; } catch (Exception e) { e.printStackTrace(); } return false; } } |
使用zip来进行压缩和解压
出现这个java.lang.IllegalArgumentException: MALFORMED
这是文件编码的问题
public class ZipUtil { /** * @Description: * @date: 2020年10月24日 下午4:54:36 * @param inputFile 待压缩文件夹/文件名 * @param outputFile 生成的压缩包名字 * @throws Exception */ public static void ZipCompress(String inputFile, String outputFile) throws Exception { // 创建zip输出流 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile)); // 创建缓冲输出流 BufferedOutputStream bos = new BufferedOutputStream(out); File input = new File(inputFile); compress(out, bos, input, null); bos.close(); out.close(); } /** * @param name 压缩文件名,可以写为null保持默认 */ // 递归压缩 private static void compress(ZipOutputStream out, BufferedOutputStream bos, File input, String name) throws IOException { if (name == null) { name = input.getName(); } // 如果路径为目录(文件夹) if (input.isDirectory()) { // 取出文件夹中的文件(或子文件夹) File[] flist = input.listFiles(); if (flist.length == 0)// 如果文件夹为空,则只需在目的地zip文件中写入一个目录进入 { out.putNextEntry(new ZipEntry(name + "/")); } else// 如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩 { for (int i = 0; i < flist.length; i++) { compress(out, bos, flist[i], name + "/" + flist[i].getName()); } } } else// 如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中 { out.putNextEntry(new ZipEntry(name)); FileInputStream fos = new FileInputStream(input); BufferedInputStream bis = new BufferedInputStream(fos); int len = -1; // 将源文件写入到zip文件中 byte[] buf = new byte[1024]; while ((len = bis.read(buf)) != -1) { bos.write(buf, 0, len); } bis.close(); fos.close(); } } /** * @Description: 校验文件夹中的文件格式 * @date: 2020年10月24日 下午4:46:17 * @param srcPath * @param suffixs * @return */ public static boolean verifyFileName(String srcPath, Listsuffixs) { { File file = new File(srcPath); if (!file.exists()) { return false; } try { InputStream fileInputStream = new FileInputStream(file); byte[] bendoce = new byte[3]; fileInputStream.read(bendoce); fileInputStream.close(); // 判断文件的编码 if (bendoce[0] == -17 && bendoce[1] == -69 && bendoce[2] == -65) { // UTF-8的编码 return verifyFileName(file, suffixs, "utf-8"); } else { return verifyFileName(file, suffixs, "gbk"); } } catch (Exception e) { e.printStackTrace(); } } return false; } private static boolean verifyFileName(File file, Listsuffixs, String encode) throws IOException { String regex = null; if (suffixs.size() > 0) { regex = "[\\s\\S]*.(" + String.join("|", suffixs) + ")"; } FileInputStream fileInputStream = null; ZipInputStream zipInputStream = null; ZipEntry entry = null; try { fileInputStream = new FileInputStream(file); zipInputStream = new ZipInputStream(fileInputStream, Charset.forName(encode)); while (((entry = zipInputStream.getNextEntry()) != null)) { // 如果该文件时目录 if (entry.isDirectory()) { continue; } // 校验压缩包中文件后缀 String pathName = entry.getName(); if (regex != null) { boolean matches = Pattern.matches(regex, pathName); if (matches) { return false; } } } return true; } finally { if (zipInputStream != null) { try { zipInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * @Description: 解压文件 * @date: 2020年10月24日 下午4:12:18 * @param srcPath * @param dicPath */ public static void zipUncompress(String srcPath, String dicPath) { { File file = new File(srcPath); if (!file.exists()) { return; } try { InputStream fileInputStream = new FileInputStream(file); byte[] bendoce = new byte[3]; fileInputStream.read(bendoce); fileInputStream.close(); // 判断文件的编码 if (bendoce[0] == -17 && bendoce[1] == -69 && bendoce[2] == -65) { // UTF-8的编码 zipUncompress(file, dicPath, "utf-8"); } else { zipUncompress(file, dicPath, "gbk"); } } catch (Exception e) { e.printStackTrace(); } } } /** * @Description: * @date: 2020年10月19日 下午8:17:51 * @param srcPath * 文件路径 * @param dicPath * 解压目录 * @throws IOException */ private static void zipUncompress(File file, String dicPath, String encode) throws IOException { FileInputStream fileInputStream = null; ZipInputStream zipInputStream = null; ZipEntry entry = null; try { fileInputStream = new FileInputStream(file); zipInputStream = new ZipInputStream(fileInputStream, Charset.forName(encode)); File tmp = null; while (((entry = zipInputStream.getNextEntry()) != null)) { System.out.println(entry.getName()); tmp = new File(dicPath + File.separator + entry.getName()); // 如果该文件时目录 if (entry.isDirectory()) { if (!tmp.exists()) { tmp.mkdirs(); } continue; } try (OutputStream os = new FileOutputStream(tmp); BufferedOutputStream bos = new BufferedOutputStream(os);) { int len = 0; byte[] b = new byte[1024 * 512]; while ((len = zipInputStream.read(b)) != -1) { bos.write(b, 0, len); } } catch (Exception e) { e.printStackTrace(); } } } finally { if (zipInputStream != null) { try { zipInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
java zip压缩_压缩工具相关推荐
- java bzip2 压缩_压缩工具之gzip-bzip2-xz
win下常见压缩工具:rar zip 7z linux下常见压缩工具:zip gz bz2 xz tar.gz tar.bz2 tar.xz gzip 不支持目录压缩 gzip 1.t ...
- Java Zip压缩实现(亲测)
最近在自学javaWeb,先复习一下java,把还给老师的东西再找回来(知识如果不用很快就会忘记啊).. 今天看到了zip压缩,决定要整理一下. java将有关zip压缩的内容都封装在java.uti ...
- java zip压缩
系列文章目录 第一章 万事万物皆对象 第二章 java构造器 第三章 java访问权限 第四章 java初始化 第五章 java继承 第六章 java重载与重写 第七章 java接口和抽象类 第八章 ...
- Zip压缩和解压缩工具类
依赖第三方jar包:ant-1.7.1.jar /** * @Title: ZipUtils.java * @Package com.sz.mt.test.zip * @Description: TO ...
- Java zip 压缩 文件夹删除,移动,重命名,复制
FileUtil.java import java.io.*; import java.util.List; import java.util.zip.ZipEntry; import java.ut ...
- java zip压缩解压_JAVA实现实用的ZIP压缩与解压
程序实现了ZIP压缩.共分为2部分 : 压缩(compression)与解压(decompression) 大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压. ...
- java zip 压缩乱码_java实现zip压缩中文文件名乱码怎么办?
java实现zip压缩中文文件名乱码怎么办? java实现zip压缩中文文件名乱码的解决办法: 一.文件压缩的中文乱码问题 1.中文文件名的乱码解决 对于压缩的文件,当文件名称是中文时,若使用JDK ...
- Java ZIP压缩输入输出流
ZIP是一种较为常见的压缩形式,在Java中要想实现ZIP的压缩需要导入java.util.zip包,可以使用此包中的ZipFile.ZipOutputStream.ZipInputStream.Zi ...
- java zip压缩文件
java 生成zip压缩文件 1.zip压缩文件 文章目录 java 生成zip压缩文件 一.引入jar包 二.工具类 1.有密码压缩 2.无密码压缩 3. 解压方法 三.示例 3.1.压缩前 3.2 ...
最新文章
- 如何用sqlyog实现远程连接mysql
- 作者:李明,男,中兴通讯股份有限公司产品经理。
- 《2015中国移动应用性能管理白皮书》欢迎来看
- 全局系统性地把握客户感知-建立VOC
- PostgreSQL视图使用特殊名称作字段时的处理
- mysql内存淘汰_mysql内存数据淘汰机制和大查询会不会把内存打爆?
- 好用的linux连接工具
- 第三方支付接口搜集(附下载)
- 域名泛解析,二级域名转向问题- -完美解决
- 程序员平时如何学习提高技术
- java String类型的处理
- windows10 LTSC版本 安装应用商店及聚焦屏保
- 有个程序媛上司是什么体验
- Windows IIS IUSR IWAM 帐户密码同步
- python windows 下设置文件锁、多线程
- 2022-2028全球与中国不锈钢毛细管柱市场现状及未来发展趋势
- 湖北刷脸支付:中国银联联合商业银行推出“刷脸付”产品
- 计算机面试工作计划,信息技术部面试自我介绍
- iOS触动精灵模拟触控类外挂原理分析
- 分式相乘转换成分式加减的一般性方法的简单讨论
热门文章
- it精英挑战赛的规则 校区内部评选 2020
- 字符串数值的比较 java
- dj电商-需求分析开始-静态资源-用户模块
- MySQL查询冗余索引和未使用过的索引
- 使用pt-query-digest时遇到报错can't locate Digest/MD5.pm in @INC (@INC contains: /usr/local/lib64/perl5
- 【Spark Summit East 2017】管道泄漏问题:像女士一样在大数据中做个的标记
- 异步编程系列第05章 Await究竟做了什么?
- iOS开发小技巧--学会包装控件(有些view的位置由于代码或系统原因,位置或者尺寸不容易修改或者容易受外界影响)...
- 分享个 之前写好的 android 文件流缓存类,专门处理 ArrayList、bean。
- hdu 5017 Ellipsoid(西安网络赛 1011)