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配置2个Tomcat同时运行
先说一下怎么遇到的这个问题,在练习linux中部署web项目时,linux系统安装了两个tomcat. 操作步骤: 1.配置profile#vi /etc/profile 输入以下内容: 这是两个to ...
- jmeter压测IP欺骗绕过服务端限流
1.环境声明 jmeter3.0 后端为内网环境 2.检查内网闲置的ip 工具地址,无需复杂安装,解压点击就可以用啦~~ https://pan.baidu.com/s/1Yzs1vezfFMoy-m ...
- Java学习(十三)
今天学习了Java中的继承,Java的继承和c++的差别很大. 继承的基本作用是代码复用,但最重要的作用是为了以后的"方法覆盖"和"多态机制". 继承的语法是: ...
- 大爽Python入门教程 3-1 布尔值: True, False
大爽Python入门公开课教案 点击查看教程总目录 1 布尔值介绍 从判断说起 回顾第一章介绍的简单的判断 >>> x = 10 >>> if x > 5: ...
- Python 爬取 猫眼
1. import requests import re import pymongo MONGO_URL='localhost'#建立连接 MONGO_DB='Maoyan'#创建数据库 clien ...
- Vue安装Vue Devtools调试工具提示 Error: Cannot find module '@vue-devtools/build-tools' 解决办法
我看网络上面安装Vue Devtools 调试工具的步骤几乎都是按照文章链接里的步骤进行安装, 但是在最终执行编译命令的时候 :npm run build ,提示如下错误: 尝试了很多方法,都不能解决 ...
- java获取CPU核心数
package ThreadTest; public class ThreadTest05 { public static void main(String[] args) { //获取CPU核心 S ...
- GoF23种(部分)软件设计模式【核心理解】
设计模式复习 1. 面向对象设计原则 1.1 可维护性较低的软件设计 过于僵硬 过于脆弱 复用率低 黏度过高 1.2 一个好的系统设计 可扩展性 灵活性 可插入性 复用:一个软件的组成部分可以在同一个 ...
- DP 优化方法大杂烩 & 做题记录 I.
标 * 的是推荐阅读的部分 / 做的题目. 1. 动态 DP(DDP)算法简介 动态动态规划. 以 P4719 为例讲一讲 ddp: 1.1. 树剖解法 如果没有修改操作,那么可以设计出 DP 方案 ...
- Secant 方法求方程多个根
Secant 方法介绍 Secant Method 函数 Secant_Methods 简介 1.函数定义 [c, errColumn] = Secant_Method(f, a, b, N, con ...