依赖:

<!-- 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. harbor安装高可用

    harbor架构 下载地址https://github.com/goharbor/harbor/ 高可用架构 解压压缩包 tar -xvf harbor-offline-installer-v1.10 ...

  2. 时间处理,类似"xxxx-xx-xxTxx:xx:xx187+0000"格式

    后端返回的时间:"2020-04-24T09:12:51.187+0000" 目标显示时间:2020-04-24   09:12:51 <!DOCTYPE html> ...

  3. ErrorProvider与CheckedListBox

    http://www.cnblogs.com/myshell/archive/2010/09/24/1834184.html 最近因为做WinForm的项目,遇到这个问题,当时以为CheckedLis ...

  4. [loj3032]馕

    (直接贪心会导致分子和分母过大) 令$S_{i}=\sum_{j=1}^{L}V_{i,j}$(即其独吞整个馕的快乐度),对第$i$个人求出$n$个位置$x_{1},x_{2},...,x_{n-1} ...

  5. [bzoj3670]动物园

    首先计算出s数组,s表示可以重复的前缀等于后缀的个数,显然有s[i]=s[next[i]]+1,因为有且仅有next的next满足这个条件. 然后直接暴力枚举所有next,直到它小于i的一半,这个时间 ...

  6. [noi1779]D

    先离散,然后将黑的看成1,白的看成-1,对整个序列差分,所有区间建为$(l,r+1)$的无向边,并标上-1和1,每一个点的前缀和即为该点的值 考虑什么情况下能够使得所有点都是0:当且仅当每一个点的度数 ...

  7. react之react Hooks

    函数组件,没有 class 组件中的 componentDidMount.componentDidUpdate 等生命周期方法,也没有 State,但这些可以通过 React Hook 实现. Rea ...

  8. 【JavaSE】集合

    Java集合 2019-07-05  12:39:09  by冲冲 1. 集合的由来 通常情况下,程序直到运行时,才知道需要创建多少个对象.但在开发阶段,我们根本不知道到底需要多少个数量的对象,甚至不 ...

  9. javascript-初级-day04js数据类型

    day01-js数据类型 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type& ...

  10. 5.基于二进制部署kubernetes(k8s)集群

    1 kubernetes组件 1.1 Kubernetes 集群图 官网集群架构图 1.2 组件及功能 1.2.1 控制组件(Control Plane Components) 控制组件对集群做出全局 ...