Java_zip_多源文件压缩到指定目录下

时间:2024-01-09 11:04:44

依赖:

<!-- 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);
}
}