Java创建ZIP压缩文件的方法
作者:华宰 时间:2022-10-05 05:05:23
本文实例讲述了Java创建ZIP压缩文件的方法。分享给大家供大家参考。具体如下:
这里注意:建议使用org.apache.tools.zip.*包下相关类,否则可能会出现中文乱码问题。
/**
* 压缩文件夹
* @param sourceDIR 文件夹名称(包含路径)
* @param targetZipFile 生成zip文件名
* @author liuxiangwei
*/
public static void zipDIR(String sourceDIR, String targetZipFile) {
try {
FileOutputStream target = new FileOutputStream(targetZipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(target));
int BUFFER_SIZE = 1024;
byte buff[] = new byte[BUFFER_SIZE];
File dir = new File(sourceDIR);
if (!dir.isDirectory()) {
throw new IllegalArgumentException(sourceDIR+" is not a directory!");
}
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(files[i]);
BufferedInputStream origin = new BufferedInputStream(fi);
ZipEntry entry = new ZipEntry(files[i].getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(buff)) != -1) {
out.write(buff, 0, count);
}
origin.close();
}
out.close();
} catch (IOException e) {
throw new MsgException("");
}
}
希望本文所述对大家的java程序设计有所帮助。
标签:Java,ZIP
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java利用自定义注解实现数据校验
2022-12-03 09:56:49
SpringBoot如何获取Kafka的Topic列表
2023-11-26 16:01:52
![](https://img.aspxhome.com/file/2023/2/60242_0s.png)
深入讲解spring boot中servlet的启动过程与原理
2022-08-19 00:18:40
springboot跨域CORS处理代码解析
2022-07-29 21:12:20
![](https://img.aspxhome.com/file/2023/4/69284_0s.png)
C# 获取文件夹里所有文件名的详细代码
2023-01-07 02:05:20
C#实现IP摄像头的方法
2023-12-09 03:42:51
Java面试题冲刺第二十三天--分布式
2023-09-24 07:30:43
![](https://img.aspxhome.com/file/2023/8/58588_0s.png)
Java字符串驼峰与下换线格式转换如何实现
2022-02-18 09:56:14
Android Zxing 转换竖屏扫描且提高识别率的方法
2022-06-19 13:18:12
android保存Bitmap图片到指定文件夹示例
2022-10-11 22:59:14
Kotlin语言编程Regex正则表达式实例详解
2023-06-22 02:06:29
Java如何在临界区中避免竞态条件
2022-11-02 09:39:29
SpringBoot多线程进行异步请求的处理方式
2021-11-10 10:48:30
Java实现企业发放的奖金根据利润提成问题
2023-02-11 06:52:33
WPF实现列表分页控件的示例代码
2023-12-25 06:46:02
![](https://img.aspxhome.com/file/2023/7/92977_0s.png)
Java Spring AOP之PointCut案例详解
2023-05-24 16:46:15
Unity的IPostprocessBuildWithReport实用案例深入解析
2022-07-29 23:30:15
java网络编程基础知识介绍
2023-01-10 20:37:44
![](https://img.aspxhome.com/file/2023/7/77987_0s.jpg)
详解SpringBoot自定义配置与整合Druid
2023-07-24 20:20:28
![](https://img.aspxhome.com/file/2023/9/96269_0s.jpg)
C#中读写INI文件的方法例子
2022-10-21 23:14:48