package com.zh.java.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import static org.springframework.util.StreamUtils.BUFFER_SIZE; /**
* Author: zh
* Desc: 压缩包工具类
* Date: Created in 2018/5/19 13:51
*/
@Slf4j
public class ZipUtil { /**
* 把文件集合打成zip压缩包
* @param srcFiles 压缩文件集合
* @param zipFile zip文件名
* @throws RuntimeException 异常
*/
public static void toZip(List<File> srcFiles, File zipFile) throws RuntimeException {
long start = System.currentTimeMillis();
if(zipFile == null){
log.error("压缩包文件名为空!");
return;
}
if(!zipFile.getName().endsWith(".zip")){
log.error("压缩包文件名异常,zipFile={}", zipFile.getPath());
return;
}
ZipOutputStream zos = null;
try {
FileOutputStream out = new FileOutputStream(zipFile);
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.setComment("我是注释");
zos.closeEntry();
in.close();
out.close();
}
long end = System.currentTimeMillis();
log.info("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
log.error("ZipUtil toZip exception, ", e);
throw new RuntimeException("zipFile error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
log.error("ZipUtil toZip close exception, ", e);
}
}
}
} }

Java打包多文件成zip的更多相关文章

  1. java批量下载文件为zip包

    批量下载文件为zip包的工具类 package com.meeno.trainsys.util; import javax.servlet.http.HttpServletRequest; impor ...

  2. python打包压缩文件夹zip+组装文件夹

    无意间想到的一个需求,然后就顺手写了写,留下来,方便以后用 列表版:(基本没用,仅提供思路,字典版稍微改动可以直接用) 大体需求: 把重复的文件名进行改名,达到浏览器下载相同文件的效果 下载完成后再把 ...

  3. Java生成压缩文件(zip、rar 格式)

    jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...

  4. java 实现Excel压缩成Zip导出

    1 概述 在web项目中常见的一种场景就是将文件导出为Excel,但是当需要导出多个Excel时,使用者将频繁操作,这样就严重降低了项目的友好交互性以及易用性,那么怎么才能优雅的解决这个问题呢?笔者今 ...

  5. pyinstaller打包python文件成exe(原理.安装.问题)

    py文件打包成exe文件的方式一共有三种:py2exe.PyInstaller和cx_Freeze 本文分四个步骤来详讲如何用PyInstaller将py文件打包成exe文件 1. PyInstall ...

  6. Java 图片爬虫,java打包jar文件

    目录 1. Java 图片爬虫,制作 .jar 文件 spider.java 制作 jar 文件 添加执行权限 1. Java 图片爬虫,制作 .jar 文件 spider.java spider.j ...

  7. Java批量下载文件并zip打包

    客户需求:列表勾选需要的信息,点击批量下载文件的功能.这里分享下我们系统的解决方案:先生成要下载的文件,然后将其进行压缩,生成zip压缩文件,然后使用浏览器的下载功能即可完成批量下载的需求.以下是zi ...

  8. java打包压缩文件

    package com.it.simple.util; import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream ...

  9. exe4J打包jar文件成exe可执行文件

    exe4j_6.0下载(x86\x64\注册机):        https://pan.baidu.com/s/1oFzif5ZVswbgbBkKHc8HFQ 打包步骤: 再次偷一下懒,使用别人的内 ...

随机推荐

  1. try catch(java)

    1 try.catch.finally语句中,在如果try语句有return语句,则返回的之后当前try中变量此时对应的值,此后对变量做任何的修改,都不影响try中return的返回值 2 如果fin ...

  2. 聊聊WPF中字体的设置

    1. 今天帮同事调试一个字体的bug:TextBox中的中文显示大小不一致, 比如包含"杰","热". 原因是WPF针对点阵字体需要指定特定字体才能正确渲染, ...

  3. unix下网络编程之I/O复用(一)

    什么是I/O复用? What we need is the capability to tell the kernel that we want to be notified if one or mo ...

  4. qt在linux下使用open,write,close等文件操作

    正常在linux下使用的文件操作函数为: #include <fcntl.h>fd = open(“path”,O_RDWR|O_NONBLOCK); 但是在qt中,需要使用: #incl ...

  5. Qt多选框

    1.获取并显示复选框文本内容 ui->label->setText(ui->comboBox->currentText());

  6. Windows 7 下将 Tomcat Java 程序设置为 Windows Service

    方法: Windows key + r -> Run dialog cmd -> console cd apache-tomcat-[version]/bin service.bat in ...

  7. Monitoring tools that everyone's currently using

    Although a lot of new tools have arrived since 2011, it's clear that older open source tools like Na ...

  8. PHP类(四)-类的继承

    类的继承就是从已经定义的类中继承数据,也可以重新定义或者加入一些数据. 被继承的类称为父类,基类,超类 继承的类称为子类,派生类 在PHP中只能使用单继承,也就是一个类只能从一个类中继承数据,但是一个 ...

  9. 数字调节控件JSpinner的使用

    ---------------siwuxie095                         工程名:TestUI 包名:com.siwuxie095.ui 类名:TestList.java   ...

  10. Java线程池拒绝策略

    Java线程池拒绝策略 相关资料: 线程池的RejectedExecutionHandler(拒绝策略):http://blog.csdn.net/jgteng/article/details/544 ...