Java_zip_多源文件压缩到指定目录下
依赖:
<!-- 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_多源文件压缩到指定目录下的更多相关文章
- java压缩指定目录下的所有文件和文件夹的代码
将代码过程较好的代码段备份一下,下边资料是关于java压缩指定目录下的所有文件和文件夹的代码,希望对码农有帮助. String sourceDir="E:\test";int pa ...
- linux复制指定目录下的全部文件到另一个目录中
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
- 微软BI 之SSIS 系列 - 在 SSIS 中将指定目录下的所有文件分类输出到不同文件夹
开篇介绍 比如有这样的一个需求,旧的一个业务系统通常将产出的文件输出到同一个指定的目录下的不同子目录,输出的文件类型有 XML,EXCEL, TXT 这些不同后缀的文件.现在需要在 SSIS 中将它们 ...
- linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
- [No000073]C#直接删除指定目录下的所有文件及文件夹(保留目录)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本
摘自:http://blog.csdn.net/forandever/article/details/5711319 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本 ...
- python中获取指定目录下所有文件名列表的程序
http://blog.csdn.net/rumswell/article/details/9818001 # -*- coding: utf-8 -*-#~ #------------------- ...
- C# 获取指定目录下所有文件信息、移动目录、拷贝目录
/// <summary> /// 返回指定目录下的所有文件信息 /// </summary> /// <param name="strDirectory&qu ...
- PHP 获取指定目录下所有文件(包含子目录)
PHP 获取指定目录下所有文件(包含子目录) //glob — 寻找与模式匹配的文件路径 $filter_dir = array('CVS', 'templates_c', 'log', 'img', ...
随机推荐
- Linux&C open creat read write lseek 函数用法总结
一:五个函数的参数以及返回值. 函数 参数 返回值 open (文件名,打开方式以及读 ...
- 『与善仁』Appium基础 — 7、ADB Shell命令的使用
目录 1.查看进程 2.查看实时资源占用情况 3.查看进程 UID 4.其它ADB Shell命令说明 Android系统是基于 Linux 内核的,也就是说Android系统的底层是Linux系统. ...
- 树莓派4b安装Ubuntu20.04
树莓派4b安装Ubuntu20.04 下载Ubuntu20.04镜像 下载地址 安装Raspberry Pi Imager 下载地址 烧录系统 打开Raspberry Pi Imager,选择自己刚刚 ...
- Typora下载安装教程
Typoa下载和安装 Typora---程序员记事本!!! 这里我们选择Typora作为我们的编辑器,功能的强大需要各位自己去体会. Typora下载地址 点击链接打开,然后选择Download! 根 ...
- 菜鸡的Java笔记第三 - java 自动转换原则
自动转换原则 数据范围保存大的数据类型要转换为数据范围保存小的数据类型,使用强制转换(强制转型就是在变量的前面加括号,在括号里写上需要强制要转的类型.) 数据范围保存小的数据类型可以自动转换为数据范围 ...
- 设计模式学习-使用go实现观察者模式
观察者模式 定义 适用场景 优点 缺点 代码实现 不同场景的实现方式 观察模式和发布订阅模式 参考 观察者模式 定义 观察者模式(Observer Design Pattern)定义了一种一对多的依赖 ...
- soname and real name
[1] https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes#Backward_compatibility [2] https://akka ...
- [hdu7074]Little prince and the garden of roses
显然每种颜色的花是独立的,可以分别求出答案后取$\max$ 对于某种颜色$C$,建立一张二分图,左右分别为$n$行$n$列,且第$i$行和第$j$列有边当且仅当$c_{i,j}=C$ 此时,问题即对边 ...
- [atAGC106E]Medals
暴力二分答案+网络流,点数为$o(nk)$,无法通过 考虑Hall定理,即有完美匹配当且仅当$\forall S\subseteq V_{left}$,令$S'=\{x|\exists y\in V_ ...
- [atARC105D]Let's Play Nim
先对$n$分奇偶两种情况考虑-- $n$为奇数,显然先手希望最终产生的$x_{1}\oplus x_{2}\oplus...\oplus x_{n}=0$ 对于后手,考虑构造:将最大的未被选择的$a_ ...