Java 多文件压缩与解压

1,写多个文件到压缩包

	import org.apache.tools.zip.ZipEntry;
	import org.apache.tools.zip.ZipFile;
	import org.apache.tools.zip.ZipOutputStream;
 
    public static void main(String[] args) {
        //压缩文件对象
        String zipPath = "D:\\download\\files0719.zip";
        //需要压缩的文件
        String filepath1 = "D:\\download\\yyzz.jpeg";
        String filepath2 = "D:\\download\\yyzz1.png";
        List<String> files = new ArrayList<>();
        files.add(filepath1);
        files.add(filepath2);
        
        try (OutputStream os = new BufferedOutputStream(new FileOutputStream(zipPath));
             ZipOutputStream zos = new ZipOutputStream(os)) {
            byte[] buf = new byte[10240];
            int len;
            for (int i = 0; i < files.size(); i++) {
                File file = new File(files.get(i));
                //判断是否是文件数据
                if (!file.isFile()) {
                    continue;
                }
                zos.putNextEntry(new ZipEntry(file.getName()));
                try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
                    while ((len = bis.read(buf)) > 0) {
                        zos.write(buf, 0, len);
                    }
                }
            }
        } catch (Exception e) {
            log.info("压缩文件{}失败", JSONUtils.toJSONString(files), e);
            throw new BaseException("压缩文件失败", e);
        }
    }
                    

效果图:
在这里插入图片描述

2,压缩文件解压

注意:可以用于递归文件的解压,但是不能对压缩文件里面的压缩文件进行解压

   public static void main(String[] args) {
        //解压文件
        String zipFile = "D:\\download\\filesCompress.zip";
        //解压到哪里
        String destination = "D:\\download\\unzip";
        ArrayList<String> dirList = new ArrayList<>();
        try (ZipFile zip = new ZipFile(zipFile, "GBK")) {
            //解压对象下有几个对象
            Enumeration<ZipEntry> en = zip.getEntries();
            byte[] buffer = new byte[8192];
            int length = -1;
            while (en.hasMoreElements()) {
                //获取文件实体
                ZipEntry entry = en.nextElement();
                //如果是目录
                if (entry.isDirectory()) {
                    dirList.add(entry.getName());
                    continue;
                }

                InputStream input = zip.getInputStream(entry);
                File file = new File(destination, entry.getName());
                //如果文件的父级目录不存在,创建目录
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));){
                    while (true) {
                        //向缓存区读取文件数据
                        length = input.read(buffer);
                        if (length == -1) {
                            break;
                        }
                        bos.write(buffer, 0, length);
                    }
                } finally {
                    if (input != null) {
                        try {
                            input.close();
                        } catch (IOException e) {
                            log.error(e.getMessage() == null ? "" : e.getMessage(), e);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
               

效果图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java调用Zip类批量压缩多个文件,此前有一个是压缩单个文件,也可参考,相关代码中可找到此源码。   public class ZipDemo extends JFrame{   JFileChooser fileChooser; //文件选择器   JList fileList; //待压缩文件列表   Vector files; //文件数据(待压缩文件)   JButton jbAdd; //增加文件按钮   JButton jbDelete; //删除文件按钮   JButton jbZip; //压缩按钮   JTextField target; //目标文件文本域   public ZipDemo(){   super("用ZIP压缩多个文件"); //调用父类构造函数   fileChooser=new JFileChooser(); //实例化文件选择器   files=new Vector(); //实例化文件数据Vector   fileList=new JList(files); //实例化已选择文件列表   jbAdd=new JButton("增加"); //实例化按钮组件   jbDelete=new JButton("删除");   jbZip=new JButton("压缩");   target=new JTextField(18);   JPanel panel=new JPanel(); //实例化面板,用于容纳按钮   panel.add(jbAdd); //增加组件到面板上   panel.add(jbDelete);   panel.add(jbZip);   JPanel panel2=new JPanel();   panel2.add(new JLabel("目标文件"));   panel2.add(target);   JScrollPane jsp=new JScrollPane(fileList);   Container container=getContentPane(); //得到容器   container.add(panel2,BorderLayout.NORTH); //增加组件到容器   container.add(jsp,BorderLayout.CENTER);   container.add(panel,BorderLayout.SOUTH);   jsp.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //设置边界
Java提供了ZipOutputStream类和ZipInputStream类,用于压缩解压文件压缩文件: ```java import java.io.*; import java.util.zip.*; public class ZipFile { public static void main(String[] args) throws Exception { // 创建压缩文件输出流 FileOutputStream fos = new FileOutputStream("test.zip"); ZipOutputStream zos = new ZipOutputStream(fos); // 创建要压缩文件输入流 FileInputStream fis = new FileInputStream("test.txt"); BufferedInputStream bis = new BufferedInputStream(fis); // 开始写入压缩文件 zos.putNextEntry(new ZipEntry("test.txt")); int len; byte[] buffer = new byte[1024]; while ((len = bis.read(buffer)) > 0) { zos.write(buffer, 0, len); } // 关闭流 bis.close(); zos.closeEntry(); zos.close(); fos.close(); } } ``` 解压文件: ```java import java.io.*; import java.util.zip.*; public class UnzipFile { public static void main(String[] args) throws Exception { // 创建压缩文件输入流 FileInputStream fis = new FileInputStream("test.zip"); ZipInputStream zis = new ZipInputStream(fis); // 开始解压文件 ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); File file = new File(fileName); FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); int len; byte[] buffer = new byte[1024]; while ((len = zis.read(buffer)) > 0) { bos.write(buffer, 0, len); } // 关闭流 bos.close(); fos.close(); zipEntry = zis.getNextEntry(); } // 关闭流 zis.closeEntry(); zis.close(); fis.close(); } } ``` 以上是基本的压缩解压文件的实现方法,也可以使用更高级的类库,如Apache Commons Compress。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值