依赖:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>

代码:

package com.lin.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import javax.management.RuntimeErrorException; import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; public class ZipUtils {
private static final String ENCODING = "UTF-8";
private static final String FILE_ZIP = ".zip";
private static final int BUF = 1024;
private static final Log LOG = LogFactory.getLog(ZipUtils.class); /**
* @param f
* @param rootPath
* @return
*/
@SuppressWarnings("unused")
private String getEntryName(File f, String rootPath) {
String entryName;
String fPath = f.getAbsolutePath();
if (fPath.indexOf(rootPath) != -1) {
entryName = fPath.substring(rootPath.length() + 1);
} else {
entryName = f.getName();
}
if (f.isDirectory()) {
entryName += "/";
}
return entryName;
} /**
* 多源文件压缩到指定目录下
*
* @param list
* @param rootPath
* @param zipName
* @author xuewb
* @throws ArchiveException
* ,IOException
*/
public void doFileZips(List<String[]> list, String outPath, String zipName)
throws ArchiveException, IOException {
File fo = new File(outPath);
if (!fo.exists()) {
fo.mkdirs();
}
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(
new FileOutputStream(FilenameUtils.concat(outPath, zipName)));
zos.setEncoding(ENCODING);
ZipArchiveEntry ze = null;
String[] arr = new String[2];
try {
for (int i = 0; i < list.size(); i++) {
arr = list.get(i);
if (StringUtils.isNotEmpty(arr[0])) {
File dir = new File(arr[0]);
if (!dir.exists()) {
continue;
}
ze = new ZipArchiveEntry(
StringUtils.isNotEmpty(arr[1]) ? arr[1]
: getEntryName(dir, outPath));
zos.putArchiveEntry(ze);
if (ze.isDirectory()) {
zos.closeArchiveEntry();
continue;
}
FileInputStream fis = new FileInputStream(dir);
IOUtils.copy(fis, zos, BUF);
fis.close();
zos.closeArchiveEntry();
}
}
zos.close();
} catch (Exception e) {
LOG.error("压缩文件异常", e);
throw new RuntimeException(e);
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
LOG.error("关闭流异常", e);
throw new RuntimeException(e);
}
}
} public static void main(String[] args) throws ArchiveException, IOException {
long beginTime = System.currentTimeMillis();
List<String[]> list = new ArrayList<String[]>();
String[] arr1 = new String[2];
arr1[0] = "D:\\1\\2\\1.docx";
arr1[1] = "指导说明.docx";
list.add(arr1);
String[] arr2 = new String[2];
arr2[0] = "D:\\1\\2\\1.txt";
arr2[1] = "激活码.txt";
list.add(arr2);
String[] arr3 = new String[2];
arr3[0] = "D:\\1\\2\\1.txt";
arr3[1] = "";
list.add(arr3);
new ZipUtils().doFileZips(list, "D:\\1\\3\\", "压缩文件qwer123.zip");
long endTime = System.currentTimeMillis();
System.out.println(endTime - beginTime);
}
}

Java_zip_多源文件压缩到指定目录下的更多相关文章

  1. java压缩指定目录下的所有文件和文件夹的代码

    将代码过程较好的代码段备份一下,下边资料是关于java压缩指定目录下的所有文件和文件夹的代码,希望对码农有帮助. String sourceDir="E:\test";int pa ...

  2. linux复制指定目录下的全部文件到另一个目录中

    linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...

  3. 微软BI 之SSIS 系列 - 在 SSIS 中将指定目录下的所有文件分类输出到不同文件夹

    开篇介绍 比如有这样的一个需求,旧的一个业务系统通常将产出的文件输出到同一个指定的目录下的不同子目录,输出的文件类型有 XML,EXCEL, TXT 这些不同后缀的文件.现在需要在 SSIS 中将它们 ...

  4. linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹

    linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...

  5. [No000073]C#直接删除指定目录下的所有文件及文件夹(保留目录)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本

    摘自:http://blog.csdn.net/forandever/article/details/5711319 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本 ...

  7. python中获取指定目录下所有文件名列表的程序

    http://blog.csdn.net/rumswell/article/details/9818001 # -*- coding: utf-8 -*-#~ #------------------- ...

  8. C# 获取指定目录下所有文件信息、移动目录、拷贝目录

    /// <summary> /// 返回指定目录下的所有文件信息 /// </summary> /// <param name="strDirectory&qu ...

  9. PHP 获取指定目录下所有文件(包含子目录)

    PHP 获取指定目录下所有文件(包含子目录) //glob — 寻找与模式匹配的文件路径 $filter_dir = array('CVS', 'templates_c', 'log', 'img', ...

随机推荐

  1. linux网络编程 IO多路复用 select epoll

    本文以我的小型聊天室为例,对于服务器端的代码,做了三次改进,我将分别介绍阻塞式IO,select,epoll . 一:阻塞式IO 对于聊天室这种程序,我们最容易想到的是在服务器端accept之后,然后 ...

  2. mapper接口绑定异常

    前言 由于MP的代码生成器把mapper接口绑定的写sql语句xml文件创建在java目录下,而Maven加载机制只会将.java文件编译成.class文件,所以在target目录下找不到写xml文件 ...

  3. 用Python实现人像动漫化

    用Python实现人像动漫化 本文章会教你如何使用Python实现人像动漫化,先看看效果实例: 实现过程如下: 本案例是使用百度的API来实现的,首先需要进入百度AI开放平台注册账号,具体流程就不讲了 ...

  4. elasticsearch中query_string的隐藏坑

    elasticsearch查询中使用filter查询添加query_string格式为: {                    "query_string": {       ...

  5. 2021CISCN 华南赛区WEB wp

    CISCN 华南区域赛 太菜了 我躺平了 easy_seri <?php error_reporting(0); highlight_file(__FILE__); class Test{ pu ...

  6. 通过大量实战案例分解Netty中是如何解决拆包黏包问题的?

    TCP传输协议是基于数据流传输的,而基于流化的数据是没有界限的,当客户端向服务端发送数据时,可能会把一个完整的数据报文拆分成多个小报文进行发送,也可能将多个报文合并成一个大报文进行发送. 在这样的情况 ...

  7. Jquery的常用使用方法

    1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$("input:[type='checkbox']: ...

  8. Python | Python常用函数、方法示例总结(API)

    目录 前言 1. 运算相关 2. Sring与数字 3. 列表相关 4. 集合相关 5. 序列化类型 6. 字典相关 7. 输入输出 8. 文件相关 9. json模块 10. unittest测试模 ...

  9. c语言1左移32位(1<<32)是多少,左移-1位呢

    C语言中 << 是逻辑移位,不是循环移位.1 左移 32 位后为 0,左移 -1 位实际是左移 255 位(互补),当然也是0.这种问题可以写一段小程序,单步执行,看一下每一步的结果.先说 ...

  10. [loj3052]春节十二响

    首先可以发现对于两条链来说,显然是对两边都排好序,然后大的配大的,小的配小的(正确性比较显然),最后再加入根(根只能单独选)这个结果其实也可以理解为将所有max构成一条新的链,求出因此,对于每一个结点 ...