一、压缩cab文件

import com.ms.util.cab.CabCreator;
import com.ms.util.cab.CabFileEntry;
import com.ms.util.cab.CabFolderEntry;
import com.ms.util.cab.CabProgressInterface;import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;/*** @description 压缩cab文件* @author sun*/
public class CreatorTool implements CabProgressInterface {public static void main(String[] args) {try {//输出目录String outCabPath = "D:\\test888.cab";//输入文件目录String filePath = "D:\\file\\";//创建CabProgressInterface实现类CreatorTool test = new CreatorTool();CabCreator cab = new CabCreator(test);CabFolderEntry cabFolderEntry = new CabFolderEntry();cabFolderEntry.setCompression(1, 0);FileOutputStream fileOutputStream = new FileOutputStream(outCabPath);cab.create(fileOutputStream);cab.newFolder(cabFolderEntry);FileInputStream fileInputStream = null;//用来接受遍历出来的文件ArrayList<String> list = new ArrayList<>();CreatorTool.getAllFile(filePath, list);for (String s : list) {File file = new File(s);fileInputStream = new FileInputStream(file);CabFileEntry cabFileEntry = new CabFileEntry();cabFileEntry.setDate(new Date());if (!filePath.equals(file.getParent())) {//操作多级目录File file1=new File(filePath);String absolutePath = file.getAbsolutePath();String absoluteNewPath = absolutePath.replace(file1.getAbsolutePath(), "");if(absoluteNewPath.substring(0,1).equals("\\")){absoluteNewPath=absoluteNewPath.replaceFirst("\\\\","");}cabFileEntry.setName(absoluteNewPath);} else {cabFileEntry.setName(file.getName());}cabFileEntry.setSize(file.length());cab.addStream(fileInputStream, cabFileEntry);}//完成cab文件压缩包完成 一定调用complete 不然是0字节cab.complete();fileOutputStream.close();fileInputStream.close();} catch (Exception e) {e.printStackTrace();}}//这个类 可以加一些进度显示什么的@Overridepublic Object progress(int i, long l, long l1, Object[] objects) {return null;}/*** @description 获取文件夹下的所有文件* @param path 输入路径* @param list 存文件AbsolutePath*/public static void getAllFile(String path, ArrayList<String> list) {File file = new File(path);//获取全部File//返回目录名加文件名//添加过滤器//这些路径名表示此抽象路径名所表示目录中的文件。File[] files = file.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {return true;}});for (int i = 0; i < files.length; i++) {//判断是否是目录,是的话继续递归if (files[i].isDirectory()) {getAllFile(files[i].getAbsolutePath(), list);} else {//否则添加到list//获取全部包+文件名list.add(files[i].getAbsolutePath());}}}
}

二、解压cab文件

import com.ms.util.cab.CabDecoder;
import com.ms.util.cab.CabDecoderInterface;
import com.ms.util.cab.CabFileEntry;import java.io.*;/*** @description 解压cab文件* @author sun*/
public class DecoderTool implements CabDecoderInterface {public static void main(String[] args) {try {//输入cab压缩文件FileInputStream fin = new FileInputStream("D:\\test.cab");DecoderTool test = new DecoderTool();//创建CabDecoder 传人CabDecoderInterface实现类CabDecoder cab = new CabDecoder(fin, test);cab.extract();fin.close();} catch (Exception e) {e.printStackTrace();}}@Overridepublic Object progress(int arg0, long arg1, long arg2, Object[] arg3) {return null;}@Overridepublic boolean closeOutputStream(OutputStream out, CabFileEntry cfe, boolean arg2) {//获取输出的数据结果outFileOutputStream fos = null;ByteArrayOutputStream bos = (ByteArrayOutputStream) out;byte[] bytes = bos.toByteArray();try {//将流输出到文件中InputStream in = new ByteArrayInputStream(bytes);String path = "D:\\test888\\"+cfe.getName();File file = new File(path);if (!file.exists()) {if(!file.getParentFile().exists()){file.getParentFile().mkdirs();}file.createNewFile();}fos = new FileOutputStream(file);int len = 0;byte[] buf = new byte[1024];while ((len = in.read(buf)) != -1) {fos.write(buf, 0, len);}fos.flush();} catch (Exception e) {e.printStackTrace();} finally {if (null != fos) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println(bytes.length);return false;}@Overridepublic InputStream openCabinet(String arg0, String arg1) {return null;}@Overridepublic OutputStream openOutputStream(CabFileEntry cfe) {ByteArrayOutputStream bos = new ByteArrayOutputStream();return bos;}@Overridepublic boolean reservedAreaData(int arg0, byte[] arg1, int arg2, byte[] arg3, int arg4) {return false;}
}

jar包

https://download.csdn.net/download/qq_21101587/12853992

java解压和压缩cab包 附jar相关推荐

  1. java util zip.zipexc,JAVA解压zip压缩文件的实例

    今天在弄一个东西,需要在PL/SQL中解压zip的压缩包,刚开始的时候是想着直接在PLSQL中调用java,在java里面调用unzip的shell命令来解析压缩文件,但是比较悲剧,一直老是失败,在尝 ...

  2. java 解压与压缩代码_Java实现多文件压缩和解压缩代码详解

    Java实现多文件压缩和解压缩代码 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...

  3. java解压,压缩.gz文件

    /*      * gz文件是linux下常见的压缩格式.使用 java.util.zip.GZIPInputStream即可,压缩是 java.util.zip.GZIPOutputStream   ...

  4. linux下,解压和压缩tgz包

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) .gz 解压1:gunzip FileNam ...

  5. Java解压rar5压缩文件

    使用代码解压压缩文件,并指定解压后路径 导入依赖 <dependency><groupId>com.github.axet</groupId><artifac ...

  6. java压缩文件详解_Java解压和压缩带密码的zip文件过程详解

    前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...

  7. java zip加密压缩_Java解压和压缩带密码的zip文件过程详解

    前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...

  8. java zip 解压 密码_Java解压和压缩带密码的zip文件过程详解

    前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...

  9. java 压缩解压密码zip_Java解压和压缩带密码的zip文件过程详解|chu

    前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...

  10. 解压后java文字乱码_怎么解决java解压zip包出现乱码

    怎么解决java解压zip包出现乱码 发布时间:2020-06-23 09:02:42 来源:亿速云 阅读:107 作者:Leah 怎么解决java解压zip包出现乱码?相信很多没有经验的人对此束手无 ...

最新文章

  1. 通过define _CRTDBG_MAP_ALLOC宏来检测windows上的code是否有内存泄露
  2. Android中的约束布局
  3. PHP登录带图片,PHP登录注册完整图片验证码实现
  4. oracle右连接失效,oracle 右连接
  5. Codeup-问题 A: 问题 A: 矩形嵌套
  6. Jupyter Notebook 常用的快捷键
  7. 楼继伟:现有5G技术很不成熟
  8. 2018-09-18
  9. Emmet 语法 速查表
  10. jquery.cookie中的操作
  11. 线性表—线性表的合并
  12. linux fork脚本,在Shell脚本中调用另一个脚本的三种方式讲解
  13. Mysql学习总结(47)——MySQL大表优化方案
  14. 震惊!99%的人不知道的Linux权限问题细节
  15. pythonpm2.5空气质量提醒_Python实现抓取城市的PM2.5浓度和排名
  16. Qt实战案例(13)——Qt的界面外观详细介绍
  17. 通过Adobe Acrobat DC和iText.jar完成通过pdf模板生成pdf
  18. dmg文件 linux,Linux_dmg文件是什么Linux如何通过命令行建立dmg文件,  Linux系统操作中,很多人 - phpStudy...
  19. 同期群分析(Cohort Analysis)
  20. 冈萨雷斯图像处理---非锐化掩蔽和高提升滤波

热门文章

  1. linux装回windows系统,装linux后怎样装回windows?(Linux系统清除Grub的几种方法)
  2. 如何搭建清晰易懂的数据看板?
  3. 2020年十大数字客户体验(CX)软件平台
  4. MyBatis入门到精通
  5. hexo+yilia添加背景图片
  6. echarts 画四川省地图 点击高亮并获取各市区参数
  7. 课程设计题七:交通灯控制器
  8. Linux—RAID磁盘阵列与阵列卡
  9. 【运维心得】只有百度能打开,其他页面打不开怎么办?
  10. 想变好却不能坚持,我告诉你怎么办