zip解压文件,可选择保留的文件夹及该文件夹下的所有文件
代码:
zip里面的文件:
public static void main(String[] args) {
// 要解压的ZIP文件路径
String zipFilePath = "G:\\WeChat\\WeChat Files\\wxid_aff2r4isimwl22\\FileStorage\\File\\2023-07\\Model.zip";
// 解压的输出文件夹路径
String outputFolderPath = "G:\\WeChat\\WeChat Files\\wxid_aff2r4isimwl22\\FileStorage\\File\\2023-07\\20230704";
// 要保留的文件夹路径;如果要保留models/default/model/该文件夹,则直接修改对应的值就好
String targetFolder = "models/";
try {
// 创建输出文件夹
File outputFolder = new File(outputFolderPath);
outputFolder.mkdirs();
// 创建ZipInputStream来读取zip文件
FileInputStream fileInputStream = new FileInputStream(zipFilePath);
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
// 逐个读取zip文件中的entry
ZipEntry entry = zipInputStream.getNextEntry();
while (entry != null) {
String entryName = entry.getName();
if (entryName.startsWith(targetFolder) && !entry.isDirectory()) {
// 创建输出文件
File outputFile = new File(outputFolder, entryName);
outputFile.getParentFile().mkdirs();
// 将entry写入输出文件
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = zipInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}
fileOutputStream.close();
}
zipInputStream.closeEntry();
entry = zipInputStream.getNextEntry();
}
zipInputStream.close();
fileInputStream.close();
System.out.println("解压完成!");
} catch (IOException e) {
e.printStackTrace();
}
}