import org.springframework.util.AntPathMatcher;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
/**
* 获取zip文件列表
* @param zipPath zip路径
* @return
*/
public static List<String> getZipFilesPaths(String zipPath) {
List<String> filePathList = ZipUtil.getFileList(zipPath);
return filePathList;
}
private static List<String> getFileList(String zipPath) {
List<String> result = new ArrayList<>();
File file = new File(zipPath);
if(file.exists()){
try {
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipPath), Charset.forName("GBK"));
while (true) {
ZipEntry zipEntry = zipInputStream.getNextEntry();
if (zipEntry==null)
break;
if(zipEntry.isDirectory()){ //遇到文件夹就跳过
continue;
}else{
result.add(zipEntry.getName());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 压缩文件
* @param sourceFilePathList 待压缩文件绝对路径列表
* @param targetFilePath 压缩后zip文件绝对路径
* @param excludePath 排除目录
* @return
* @throws IOException
*/
private static int compress(List<String> sourceFilePathList, String targetFilePath, String excludePath) throws IOException {
byte[] buf = new byte[1024];
File excludeFile = new File(excludePath);
File zipFile = new File(targetFilePath);
if(!zipFile.exists())
zipFile.createNewFile();
int count = 0;
try {
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
for (String sourceFilePath : sourceFilePathList) {
if(sourceFilePath == null || sourceFilePath.length()<1){
continue;
}
File sourceFile = new File(sourceFilePath);
if(sourceFile == null || !sourceFile.exists()){
continue;
}
FileInputStream fileInputStream = new FileInputStream(sourceFile);
if (sourceFilePath.endsWith(zipFile.getName().replace(".zip",".xml"))) {
zipOutputStream.putNextEntry(new ZipEntry(sourceFile.getName()));
} else {
String relativePath = (sourceFile.getPath()).replace(excludeFile.getPath(), "");
if (relativePath.startsWith("/")) {
relativePath = relativePath.substring(1);
}
if (relativePath.startsWith("\\")) {
relativePath = relativePath.substring(1);
}
zipOutputStream.putNextEntry(new ZipEntry(relativePath));
}
int len;
while((len = fileInputStream.read(buf)) > 0){
zipOutputStream.write(buf, 0, len);
}
zipOutputStream.closeEntry();
fileInputStream.close();
count++;
}
zipOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
/**
* 整理待压缩文件路径
* @param sourcePath 待压缩路径
* @return
*/
private static List<String> collectFile(String sourcePath) {
List<String> result = new ArrayList<>();
File file = new File(sourcePath);
File[] listFiles = file.listFiles();
if (listFiles!=null && listFiles.length>0) {
for(File file_ : listFiles){
if(!file_.isDirectory()) {
result.add(file_.getPath());
} else {
List<String> tempList = collectFile(file_.getPath());
if (tempList!=null && tempList.size()>0) {
result.addAll(tempList);
}
}
}
}
return result;
}
/**
* 压缩文件
* @param sourceFilePath 待压缩文件路径
* @param targetFilePath 压缩后zip文件路径
* @throws IOException
*/
public static int compress(String sourceFilePath, String targetFilePath) throws IOException {
List<String> sourceFilePathList = ZipUtil.collectFile(sourceFilePath);
return ZipUtil.compress(sourceFilePathList, targetFilePath, sourceFilePath);
}
/**
* 解压
* @param filePath 压缩包全路径
* @param zipDir 解压路径
*/
public static void unzip(String filePath,String zipDir) {
String name = "";
try {
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(filePath, Charset.forName("GBK"));
Enumeration e = zipfile.entries();
while (e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
if( entry.isDirectory()){
name = entry.getName();
name = name.substring(0, name.length() - 1);
File fileObject = new File(zipDir + name);
fileObject.mkdir();
}else{
is = new BufferedInputStream(zipfile.getInputStream(entry));
int count;
byte[] dataByte = new byte[1024];
FileOutputStream fos = new FileOutputStream(zipDir+entry.getName());
dest = new BufferedOutputStream(fos, 1024);
while ((count = is.read(dataByte, 0, 1024)) != -1) {
dest.write(dataByte, 0, count);
}
dest.flush();
dest.close();
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
com.xqx.bdcbsdt.controller.ZipUtil.compress("E:\\sgasg","E:\\sgasg\\1.zip");
}
}
Java ZipUtil
于 2023-08-25 09:59:28 首次发布