|
|
@ -1,10 +1,11 @@ |
|
|
|
package io.sc.platform.core.util; |
|
|
|
|
|
|
|
import io.sc.platform.util.FileUtil; |
|
|
|
import net.lingala.zip4j.ZipFile; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.util.zip.ZipEntry; |
|
|
|
import java.util.zip.ZipOutputStream; |
|
|
|
|
|
|
|
public class ZipUtil { |
|
|
|
public static void zip(String targetDirPath) throws IOException { |
|
|
@ -22,37 +23,46 @@ public class ZipUtil { |
|
|
|
public static void zip(String targetDirPath,String targetZipFilePath,boolean delete) throws IOException { |
|
|
|
File targetDirFile =new File(targetDirPath); |
|
|
|
if(targetDirFile.exists() && targetDirFile.isDirectory()) { |
|
|
|
File targetZipFile =new File(targetZipFilePath); |
|
|
|
if(targetZipFile.exists() && targetZipFile.isFile()){ |
|
|
|
File targetZipFile = new File(targetZipFilePath); |
|
|
|
if (targetZipFile.exists() && targetZipFile.isFile()) { |
|
|
|
targetZipFile.delete(); |
|
|
|
} |
|
|
|
File targetZipFileParent =targetZipFile.getParentFile(); |
|
|
|
if(!targetZipFileParent.exists() || !targetZipFileParent.isDirectory()){ |
|
|
|
File targetZipFileParent = targetZipFile.getParentFile(); |
|
|
|
if (!targetZipFileParent.exists() || !targetZipFileParent.isDirectory()) { |
|
|
|
targetZipFileParent.mkdirs(); |
|
|
|
} |
|
|
|
ZipFile zipFile = new ZipFile(targetZipFilePath); |
|
|
|
|
|
|
|
if (targetDirFile.exists() && targetDirFile.isDirectory()) { |
|
|
|
File[] fs = targetDirFile.listFiles(); |
|
|
|
if (fs != null && fs.length > 0) { |
|
|
|
for (File f : fs) { |
|
|
|
if (f.isDirectory()) { |
|
|
|
zipFile.addFolder(f); |
|
|
|
} else { |
|
|
|
zipFile.addFile(f); |
|
|
|
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetZipFilePath))) { |
|
|
|
File[] files = targetDirFile.listFiles(); |
|
|
|
for (File file : files) { |
|
|
|
zip(zipOutputStream,file,file.getName()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
zipFile.close(); |
|
|
|
if (delete) { |
|
|
|
FileUtil.deldirs(targetDirFile); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
throw new IOException(targetDirPath + " NOT exists or is NOT a directory"); |
|
|
|
|
|
|
|
public static void zip(ZipOutputStream outputStream,File file, String relativePath) throws IOException { |
|
|
|
if(file.isDirectory()) { |
|
|
|
if (relativePath.endsWith("/")) { |
|
|
|
//如果文件夹是以“/”结尾,将文件夹作为压缩箱放入zipOut压缩输出流
|
|
|
|
outputStream.putNextEntry(new ZipEntry(relativePath)); |
|
|
|
outputStream.closeEntry(); |
|
|
|
} else { |
|
|
|
//如果文件夹不是以“/”结尾,将文件夹结尾加上“/”之后作为压缩箱放入zipOut压缩输出流
|
|
|
|
outputStream.putNextEntry(new ZipEntry(relativePath + "/")); |
|
|
|
outputStream.closeEntry(); |
|
|
|
} |
|
|
|
//遍历文件夹子目录,进行递归的zipFile
|
|
|
|
File[] children = file.listFiles(); |
|
|
|
for (File childFile : children) { |
|
|
|
zip(outputStream,childFile, relativePath + "/" + childFile.getName()); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
outputStream.putNextEntry(new ZipEntry(relativePath)); |
|
|
|
outputStream.write(Files.readAllBytes(file.toPath())); |
|
|
|
outputStream.closeEntry(); |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException { |
|
|
|
ZipUtil.zip("/Users/wangshaoping/wspsc/workspace/wangshaoping/v8/platform/app.platform/work/web/export/liquibase/platform","/Users/wangshaoping/wspsc/workspace/wangshaoping/v8/platform/app.platform/work/web/export/liquibase2/platform.zip"); |
|
|
|
} |
|
|
|
} |
|
|
|