129 lines
4.7 KiB
Java
129 lines
4.7 KiB
Java
package com.tenwa.app.invoice.utils;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
import org.apache.log4j.LogManager;
|
|
import org.apache.log4j.Logger;
|
|
//import org.apache.logging.log4j.LogManager;
|
|
//import org.apache.logging.log4j.Logger;
|
|
import org.apache.tomcat.util.http.fileupload.FileUtils;
|
|
|
|
|
|
public class FilesToZipUtils {
|
|
|
|
private static final Logger log = LogManager.getLogger(FilesToZipUtils.class);
|
|
|
|
public static void delZipFile(String zipFilename) {
|
|
File file = new File(zipFilename);
|
|
if (file.exists()) {
|
|
file.delete();
|
|
try {
|
|
File localZipParentFile= file.getParentFile() ;
|
|
File[] listFiles = localZipParentFile.listFiles();
|
|
if(listFiles!=null && listFiles.length==0)
|
|
FileUtils.deleteDirectory(localZipParentFile);
|
|
} catch (IOException e) {
|
|
log.error(">>>>>>>>[error] file parent is not deleted>>>>>>>>" + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void delZipFiles(List<File> files) {
|
|
if(files!=null && files.size()>0)
|
|
for(File file :files){
|
|
if (file.exists()) {
|
|
file.delete();
|
|
try {
|
|
File localZipParentFile= file.getParentFile() ;
|
|
File[] listFiles = localZipParentFile.listFiles();
|
|
if(listFiles!=null && listFiles.length==0)
|
|
FileUtils.deleteDirectory(localZipParentFile);
|
|
} catch (IOException e) {
|
|
log.error(">>>>>>>>[error] file parent is not deleted>>>>>>>>" + e.getMessage());
|
|
}
|
|
}else {
|
|
log.error(">>>>>>>>[error] file is not exists>>>>>>>>" + file);
|
|
}
|
|
}
|
|
}
|
|
public static void toZipFiles(List<File> files, String zipFilename) throws Exception {
|
|
try {
|
|
File file = new File(zipFilename);
|
|
if (!file.exists()) {
|
|
String path = zipFilename.substring(0, zipFilename.lastIndexOf("/")) ;
|
|
if(!new File(path).exists()) {
|
|
new File(path).mkdirs();
|
|
}
|
|
file.createNewFile();
|
|
}
|
|
FileOutputStream fous = new FileOutputStream(file);
|
|
ZipOutputStream zipOut = new ZipOutputStream(fous);
|
|
zipFile(files, zipOut);
|
|
zipOut.close();
|
|
fous.close();
|
|
if (file.exists()) {
|
|
log.info(">>>>>>>>[success] zip file is generated success>>>>>>>>" + file);
|
|
log.info(">>>>>>>>fils will be delete>>>>>>>>" );
|
|
log.info(">>>>>>>>fils is deleting>>>>>>>>" );
|
|
delZipFiles(files);
|
|
}else {
|
|
log.error(">>>>>>>>[error] zip file is not generated success>>>>>>>>" + file);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void zipFile(List<File> files, ZipOutputStream outputStream) {
|
|
int size = files.size();
|
|
for (int i = 0; i < size; i++) {
|
|
File file = (File) files.get(i);
|
|
if (file.exists()) {
|
|
zipFile(file, outputStream);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public static void zipFile(File inputFile, ZipOutputStream ouputStream) {
|
|
try {
|
|
if (inputFile.exists()) {
|
|
if (inputFile.isFile()) {
|
|
FileInputStream IN = new FileInputStream(inputFile);
|
|
BufferedInputStream bins = new BufferedInputStream(IN, 512);
|
|
ZipEntry entry = new ZipEntry(inputFile.getName());
|
|
ouputStream.putNextEntry(entry);
|
|
// 向压缩文件中输出数据
|
|
int nNumber;
|
|
byte[] buffer = new byte[512];
|
|
while ((nNumber = bins.read(buffer)) != -1) {
|
|
ouputStream.write(buffer, 0, nNumber);
|
|
}
|
|
// 关闭创建的流对象
|
|
bins.close();
|
|
IN.close();
|
|
} else {
|
|
try {
|
|
File[] files = inputFile.listFiles();
|
|
for (int i = 0; i < files.length; i++) {
|
|
zipFile(files[i], ouputStream);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|