Java生成压缩文件的实例代码

作者:chenssy 时间:2023-02-04 21:11:09 

在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载。所以自己写了一个压缩文件的工具类。该工具类支持单个文件和文件夹压缩。放代码:


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* @project: Test
* @author chenssy
* @date 2013-7-28
* @Description: 文件压缩工具类
*          将指定文件/文件夹压缩成zip、rar压缩文件
*/
public class CompressedFileUtil {
 /**
  * 默认构造函数
  */
 public CompressedFileUtil(){
 }
 /**
  * @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip
  * @param resourePath 源文件/文件夹
  * @param targetPath 目的压缩文件保存路径
  * @return void
  * @throws Exception
  */
 public void compressedFile(String resourcesPath,String targetPath) throws Exception{
   File resourcesFile = new File(resourcesPath);   //源文件
   File targetFile = new File(targetPath);      //目的
   //如果目的路径不存在,则新建
   if(!targetFile.exists()){  
     targetFile.mkdirs();
   }
   String targetName = resourcesFile.getName()+".zip";  //目的压缩文件名
   FileOutputStream outputStream = new FileOutputStream(targetPath+"\\"+targetName);
   ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
   createCompressedFile(out, resourcesFile, "");
   out.close();
 }
 /**
  * @desc 生成压缩文件。
  *         如果是文件夹,则使用递归,进行文件遍历、压缩
  *    如果是文件,直接压缩
  * @param out 输出流
  * @param file 目标文件
  * @return void
  * @throws Exception
  */
 public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
   //如果当前的是文件夹,则进行进一步处理
   if(file.isDirectory()){
     //得到文件列表信息
     File[] files = file.listFiles();
     //将文件夹添加到下一级打包目录
     out.putNextEntry(new ZipEntry(dir+"/"));
     dir = dir.length() == 0 ? "" : dir +"/";
     //循环将文件夹中的文件打包
     for(int i = 0 ; i < files.length ; i++){
       createCompressedFile(out, files[i], dir + files[i].getName());     //递归处理
     }
   }
   else{  //当前的是文件,打包处理
     //文件输入流
     FileInputStream fis = new FileInputStream(file);
     out.putNextEntry(new ZipEntry(dir));
     //进行写操作
     int j = 0;
     byte[] buffer = new byte[1024];
     while((j = fis.read(buffer)) > 0){
       out.write(buffer,0,j);
     }
     //关闭输入流
     fis.close();
   }
 }
 public static void main(String[] args){
   CompressedFileUtil compressedFileUtil = new CompressedFileUtil();
   try {
     compressedFileUtil.compressedFile("G:\\zip", "F:\\zip");
     System.out.println("压缩文件已经生成...");
   } catch (Exception e) {
     System.out.println("压缩文件生成失败...");
     e.printStackTrace();
   }
 }
}

运行程序结果如下:

压缩之前的文件目录结构: 

Java生成压缩文件的实例代码

Java生成压缩文件的实例代码

提示:如果是使用java.util下的java.util.zip进行打包处理,可能会出现中文乱码问题,这是因为java的zip方法不支持编码格式的更改,我们可以使用ant.java下的zip工具类来进行打包处理。所以需要将ant.jar导入项目的lib目录下。

总结

以上所述是小编给大家介绍的Java生成压缩文件的实例代码网站的支持!

来源:https://www.cnblogs.com/chenssy/p/3223902.html

标签:java,压缩文件
0
投稿

猜你喜欢

  • Java实现常用缓存淘汰算法:FIFO、LRU、LFU

    2022-08-26 21:45:19
  • c#中的扩展方法学习笔记

    2023-04-11 10:29:15
  • c# 通过WinAPI播放PCM声音

    2021-10-22 12:35:18
  • Spring Boot如何使用Spring Security进行安全控制

    2022-03-26 03:59:41
  • spring aop action中验证用户登录状态的实例代码

    2021-12-04 19:47:01
  • 用C#获取硬盘序列号,CPU序列号,网卡MAC地址的源码

    2022-01-04 01:19:59
  • Java异常处理操作 Throwable、Exception、Error

    2022-02-19 20:56:13
  • C#实现钟表程序设计

    2023-01-01 06:38:42
  • SpringMVC如何用Post方式重定向

    2021-10-05 21:34:27
  • C# WinForm窗体编程中处理数字的正确操作方法

    2022-11-21 04:57:10
  • [Alibaba-ARouter]浅谈简单好用的Android页面路由框架

    2023-06-28 06:37:22
  • java快速生成数据库文档详情

    2023-11-10 05:25:20
  • 谷歌被屏蔽后如何搭建安卓环境

    2022-10-23 02:07:36
  • Java 自定义动态数组方式

    2022-08-26 01:38:37
  • java基础的详细了解第三天

    2023-10-05 23:47:04
  • Java去重排序之Comparable与Comparator的使用及说明

    2023-04-28 00:49:27
  • SpringBoot 监控管理模块actuator没有权限的问题解决方法

    2022-01-26 21:50:11
  • Android图片加载库Glide用法

    2023-08-11 10:12:01
  • SpringBoot集成elasticsearch使用图文详解

    2021-06-06 16:12:47
  • Java数组动态增加容量过程解析

    2023-06-07 07:35:24
  • asp之家 软件编程 m.aspxhome.com