多个文件名称用字符串数组存储传入。
String [] fileNames = {"aa.txt","bb.txt","cc.txt"};
压缩处理方法
/**
* 压缩文件
*
* @param sourceFilePath 资源主路径
* @param outFileName 输出文件名称
* @param allFileNames 文件名称
* @return void
* @author yulisao
* @createDate 2019-03-13
*/
public static void compressFile(String sourceFilePath, String outFileName, String... fileNames ){
File file = new File(sourceFilePath);
if(!file.exists()){
return;
}
ArrayList<String> list = new ArrayList<String>();
//过掉掉不存在的文件
for (String allFileName:allFileNames) {
file = new File(sourceFilePath + "/" + allFileName);
if (file.exists()) {
list.add(allFileName);
}
}
String zipFilePath =Constants.ZIP_PATH; //输出路径
ZipUtils.fileToZip(sourceFilePath, zipFilePath, outFileName, list);
}
ZipUtils工具类如下(部分参考了网上一些前辈的方法):
package common.utils.zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import play.Logger;
/**
* ZIP 压缩工具类
*
* @author yulisao
* @createDate 2019-3-10
*/
public class ZipUtils {
private ZipUtils() {
}
/**
* 文件压缩zip
*
* @param sourceFilePath 目标文件根路径
* @param zipFilePath 压缩包输出路径
* @param outFileName 压缩包输出名称
* @param fileNames 目标文件名称
* @return
* @return File
* @author yulisao
* @createDate 2019-3-13
*/
public static <T> File fileToZip(String sourceFilePath, String zipFilePath, String outFileName, ArrayList<String> list) {
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
File[] sourceFiles = new File[list.size()];
int j = 0;
for(String fileName :list ) {
sourceFiles[j] = new File(sourceFile + "/"+ fileName);
if (!sourceFiles[j].exists()) {
Logger.info(fileName + " 文件不存在");
return null;
}
j ++;
}
if(sourceFile.exists() == false) {
Logger.info("待压缩的文件目录:" + sourceFilePath + " 不存在");
} else {
try {
File zipFile = new File(zipFilePath + "/" + outFileName + ".zip");
/*if(zipFile.exists()) {
Logger.info("正在执行打包压缩输出至 " + zipFilePath + " 文件名:" + outFileName + ".zip");
} else {*/
Logger.info("正在执行打包压缩输出至 " + zipFilePath + " 文件名:" + outFileName + ".zip");
if(null == sourceFiles || sourceFiles.length < 1) {
Logger.info("无文件需压缩");
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++) {
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
if (fis == null) {continue;}
bis = new BufferedInputStream(fis,1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1) {
zos.write(bufs, 0, read);
}
}
flag = true;
}
//}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
// 关闭流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
//读取压缩后的文件返回
String outPath= zipFilePath + "/" + outFileName + ".zip";
File zipFile = new File(outPath);
return zipFile;
}
/**
* 压缩文件夹下全部文件
*
* @param path 文件夹路径
* @param zipFilePath 输出路径
* @param outFileName 压缩包文件名
* @return
* @return File
* @author yulisao
* @createDate 2019-3-13
*/
public static <T> File folderToZip(String path, String zipFilePath, String outFileName) {
boolean flag = false;
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
File f = new File(path);
if (!f.exists()) {
Logger.info("folder is not exist:" + path);
return null;
}
File[] sourceFiles = f.listFiles();
if (sourceFiles == null) {
Logger.info("folder is null ->" + path);
return null;
}
try {
File zipFile = new File(zipFilePath + "/" + outFileName + ".zip");
Logger.info("正在执行打包压缩输出至 " + zipFilePath + " 文件名:" + outFileName + ".zip");
if(null == sourceFiles || sourceFiles.length < 1) {
Logger.info("无文件需压缩");
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++) {
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
if (fis == null) {continue;}
bis = new BufferedInputStream(fis,1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1) {
zos.write(bufs, 0, read);
}
}
flag = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
// 关闭流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
//读取压缩后的文件返回
String outPath= zipFilePath + "/" + outFileName + ".zip";
File zipFile = new File(outPath);
return zipFile;
}
/**
* 解压文件
* @param zipPath 目标文件路径
* @param outRoot 指定解压目录
* @return
* @author yulisao
* @createDate 2019-03-15
*/
public static boolean relieveZip(String zipPath, String outRoot) {
boolean flag = false;
File zipFile = new File(zipPath);
if (!zipFile.exists()) {
return false;
}
File pathFile = new File(outRoot);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = null;
try {
//指定编码,否则压缩包里面不能有中文目录
zip = new ZipFile(zipFile);
for(Enumeration entries = zip.entries(); entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (outRoot+zipEntryName).replace("/", File.separator);
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf(File.separator)));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[2048];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
flag = true;
//必须关闭,否则无法删除该zip文件
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
}