package com.vci.web.util.file;
|
|
import com.vci.properties.VciFileTransProperties;
|
import com.vci.starter.web.exception.VciBaseException;
|
import com.vci.starter.web.util.LangBaseUtil;
|
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.tools.zip.ZipEntry;
|
import org.apache.tools.zip.ZipOutputStream;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
|
/**
|
* @author ludc
|
* @date 2024/7/16 14:27
|
*/
|
@Component
|
public class VciZipUtil {
|
@Autowired(
|
required = false
|
)
|
private VciFileTransProperties transProperties;
|
|
public VciZipUtil() {
|
}
|
|
private String getZipCharset() {
|
return this.transProperties != null && !StringUtils.isBlank(this.transProperties.getZipCharset()) ? this.transProperties.getZipCharset() : "GBK";
|
}
|
|
public void folderToZipFile(String folder, String zipFileName) throws VciBaseException {
|
this.folderToZipFile(folder, zipFileName, this.getZipCharset());
|
}
|
|
public void folderToZipFile(String folder, String zipFileName, String zipEncode) throws VciBaseException {
|
File sourceFile = new File(folder);
|
if (!sourceFile.exists()) {
|
throw new VciBaseException("fileNotFound", new String[]{folder});
|
} else {
|
ZipOutputStream zos = null;
|
|
try {
|
zos = new ZipOutputStream(new FileOutputStream(zipFileName));
|
zos.setEncoding(zipEncode);
|
this.addFileToZip(zos, (new File(folder)).listFiles(), folder + File.separator);
|
} catch (Throwable var14) {
|
throw new VciBaseException(LangBaseUtil.getErrorMsg(var14), new String[0]);
|
} finally {
|
try {
|
if (zos != null) {
|
zos.close();
|
}
|
} catch (Exception var13) {
|
}
|
|
}
|
|
}
|
}
|
|
private void addFileToZip(ZipOutputStream zos, File[] files, String rootPath) throws VciBaseException {
|
if (files != null && files.length > 0) {
|
File[] var4 = files;
|
int var5 = files.length;
|
|
for(int var6 = 0; var6 < var5; ++var6) {
|
File file = var4[var6];
|
if (StringUtils.isBlank(rootPath)) {
|
try {
|
rootPath = file.getParent();
|
if (rootPath == null) {
|
rootPath = file.getPath();
|
}
|
} catch (Throwable var21) {
|
rootPath = file.getPath();
|
}
|
}
|
|
if (!file.exists()) {
|
throw new VciBaseException("fileNotFound", new String[]{file.getAbsolutePath()});
|
}
|
|
if (file.isDirectory()) {
|
this.addFileToZip(zos, file.listFiles(), rootPath);
|
} else {
|
FileInputStream is = null;
|
|
try {
|
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];
|
boolean var12 = false;
|
|
int length;
|
while((length = is.read(b)) != -1) {
|
zos.write(b, 0, length);
|
}
|
} catch (Exception var22) {
|
throw new VciBaseException("zipFileError", new String[]{file.getAbsolutePath()}, var22);
|
} finally {
|
try {
|
IOUtils.closeQuietly(is);
|
} catch (Exception var20) {
|
}
|
|
}
|
}
|
}
|
}
|
|
}
|
|
public void addFileToZip(File file, String zipFile) throws VciBaseException {
|
if (file != null && file.exists()) {
|
if (StringUtils.isBlank(zipFile)) {
|
throw new VciBaseException("zipFileNameNull", new String[0]);
|
} else {
|
this.addFileToZip(new File[]{file}, zipFile);
|
}
|
} else {
|
throw new VciBaseException("fileNotFound", new String[]{file.getAbsolutePath()});
|
}
|
}
|
|
public void addFileToZip(File[] files, String zipFile) throws VciBaseException {
|
this.addFileToZip(files, zipFile, this.getZipCharset());
|
}
|
|
public void addFileToZip(File[] files, String zipFile, String zipEncode) throws VciBaseException {
|
if (files != null && files.length != 0) {
|
if (StringUtils.isBlank(zipFile)) {
|
throw new VciBaseException("zipFileNameNull", new String[0]);
|
} else {
|
ZipOutputStream zos = null;
|
|
try {
|
zos = new ZipOutputStream(new FileOutputStream(zipFile));
|
zos.setEncoding(zipEncode);
|
this.addFileToZip(zos, files, "");
|
} catch (Throwable var13) {
|
throw new VciBaseException(LangBaseUtil.getErrorMsg(var13), new String[0], var13);
|
} finally {
|
try {
|
if (zos != null) {
|
zos.close();
|
}
|
} catch (Exception var12) {
|
}
|
|
}
|
|
}
|
} else {
|
throw new VciBaseException("fileNull", new String[0]);
|
}
|
}
|
}
|