java解压和压缩cab包 附jar

 

一、压缩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();
        }
    }
    //这个类 可以加一些进度显示什么的
    @Override
    public 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() {
            @Override
            public 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();
        }
    }

    @Override
    public Object progress(int arg0, long arg1, long arg2, Object[] arg3) {
        return null;
    }

    @Override
    public boolean closeOutputStream(OutputStream out, CabFileEntry cfe, boolean arg2) {
        //获取输出的数据结果out
        FileOutputStream 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;
    }

    @Override
    public InputStream openCabinet(String arg0, String arg1) {
        return null;
    }


    @Override
    public OutputStream openOutputStream(CabFileEntry cfe) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        return bos;
    }

    @Override
    public boolean reservedAreaData(int arg0, byte[] arg1, int arg2, byte[] arg3, int arg4) {
        return false;
    }
}

jar包

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值