package com.vci.ubcs.resource.utils;
|
|
import com.vci.ubcs.starter.exception.VciBaseException;
|
import com.vci.ubcs.starter.web.util.LangBaseUtil;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.StringUtils;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.InputStream;
|
import java.nio.charset.Charset;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipOutputStream;
|
|
/**
|
* zip相关的工具类
|
* @author wanghong
|
* @date 2021-02-02
|
*/
|
@Component
|
public class ZipUtil {
|
|
/**
|
* 获取zip的字符集
|
* @return 默认为GBK,否则需要配置
|
*/
|
private String getZipCharset(){
|
return (File.separatorChar=='\\')?"GBK":"UTF-8";
|
}
|
|
/**
|
* 将文件夹压缩成压缩包
|
* @param folder 文件夹路径
|
* @param zipFileName zip文件名
|
* @throws VciBaseException 参数为空或者添加出错的会抛出异常
|
*/
|
public void folderToZipFile(String folder, String zipFileName) throws VciBaseException {
|
folderToZipFile(folder,zipFileName,getZipCharset());
|
}
|
|
/**
|
* 将文件夹压缩成压缩包
|
* @param folder 文件夹路径
|
* @param zipFileName zip文件名
|
* @param zipEncode zip的字符集,windows应该为GBK
|
* @throws VciBaseException 参数为空或者添加出错的会抛出异常
|
*/
|
public void folderToZipFile(String folder, String zipFileName,String zipEncode) throws VciBaseException{
|
File sourceFile = new File(folder);
|
if(!sourceFile.exists()){
|
throw new VciBaseException("文件{0}没有找到",new String[]{folder});
|
}
|
try( ZipOutputStream zos =new ZipOutputStream(new FileOutputStream(zipFileName), StringUtils.hasLength(zipEncode)?Charset.forName(zipEncode): Charset.forName(getZipCharset()));) {
|
//默认是UTF-8格式的
|
addFileToZip(zos, new File(folder).listFiles(),folder + File.separator);
|
}catch (Throwable e){
|
throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{});
|
}
|
}
|
|
/**
|
* 将文件夹压缩成压缩包
|
* @param zos zip输出流
|
* @param files 要压缩的文件
|
* @param rootPath 压缩的文件中的顶层目录
|
* @throws VciBaseException 文件不存在,或者内容出现错误会抛出异常
|
*/
|
private void addFileToZip(ZipOutputStream zos,File[] files,String rootPath) throws VciBaseException{
|
if(files!=null && files.length>0) {
|
for (File file : files) {
|
if(!StringUtils.hasLength(rootPath)){
|
try {
|
rootPath = file.getParent();
|
if (rootPath == null) {
|
rootPath = file.getPath();
|
}
|
}catch (Throwable e){
|
rootPath = file.getPath();
|
}
|
}
|
if (!file.exists()) {
|
throw new VciBaseException("文件{0}没有找到",new String[]{file.getAbsolutePath()});
|
}
|
if (file.isDirectory()) {
|
addFileToZip(zos, file.listFiles(),rootPath);
|
}else{
|
try (InputStream is = new FileInputStream(file); ){
|
String entryName = file.getAbsolutePath().replace(rootPath,"");
|
if(entryName.startsWith("\\")) {
|
entryName = entryName.substring(1);
|
}
|
ZipEntry zipEntry = new ZipEntry(entryName);
|
zos.putNextEntry(zipEntry);
|
byte[] b = new byte[100];
|
int length = 0;
|
while ((length = is.read(b)) != -1) {
|
zos.write(b, 0, length);
|
}
|
} catch (Exception e) {
|
throw new VciBaseException("ZIP压缩出错{0}",new String[]{file.getAbsolutePath()},e);
|
}
|
}
|
}
|
}
|
}
|
|
/**
|
* 将单个文件添加到zip中
|
* @param file 文件
|
* @param zipFile zip文件
|
* @throws VciBaseException 参数为空或者添加出错的会抛出异常
|
*/
|
public void addFileToZip(File file, String zipFile) throws VciBaseException{
|
if(file == null || !file.exists() ){
|
throw new VciBaseException("文件{0}没有找到" ,new String[]{file.getAbsolutePath()});
|
}
|
if(!StringUtils.hasLength(zipFile)){
|
throw new VciBaseException("zip文件{0}为空",new String[0]);
|
}
|
addFileToZip(new File[]{file},zipFile);
|
}
|
|
|
/**
|
* 将多个个文件添加到zip中
|
* @param files 文件
|
* @param zipFile zip文件
|
* @throws VciBaseException 参数为空或者添加出错的会抛出异常
|
*/
|
public void addFileToZip(File[] files, String zipFile) throws VciBaseException{
|
addFileToZip(files, zipFile,getZipCharset());
|
}
|
|
/**
|
* 将多个个文件添加到zip中
|
* @param files 文件
|
* @param zipFile zip文件
|
* @param zipEncode zip的字符集
|
* @throws VciBaseException 参数为空或者添加出错的会抛出异常
|
*/
|
public void addFileToZip(File[] files, String zipFile,String zipEncode) throws VciBaseException{
|
if(files == null || files.length == 0){
|
throw new VciBaseException("文件{0}内容是空",new String[0]);
|
}
|
if(!StringUtils.hasLength(zipFile)){
|
throw new VciBaseException("zip文件{0}是空",new String[0]);
|
}
|
try (ZipOutputStream zos =new ZipOutputStream(new FileOutputStream(zipFile), StringUtils.hasLength(zipEncode)?Charset.forName(zipEncode): Charset.forName(getZipCharset()));){
|
addFileToZip(zos, files,"");
|
}catch (Throwable e){
|
throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{},e);
|
}
|
}
|
}
|