压缩包导入

This commit is contained in:
hejinbo
2023-11-24 18:57:35 +08:00
parent 3a393be18d
commit e3cd2495cf
7 changed files with 208 additions and 255 deletions
@@ -17,43 +17,14 @@ import java.util.zip.ZipFile;
public class UnZipFileUtils {
public static void main(String[] args) {
// 压缩包路径
// String zipPath = "F:\\testZip\\zipFile\\上传申请书证据材料.zip";
// 解压后存放路径
// String targetPath = "F:\\testZip\\unzipFile";
// unZipFile(new File(zipPath),targetPath);
// String zipPath = "F:\\testZip\\unzipFile\\上传申请书证据材料";
// File dirUnzipPath = new File(zipPath);
// List<File> allFiles = new ArrayList<>();
// getFiles(dirUnzipPath,allFiles);
// if(allFiles!=null&&allFiles.size()>0){
// for (File fileIter : allFiles) {
// System.out.println(fileIter.getName());
// System.out.println(fileIter.getAbsolutePath());
// }
// }
String key = "\\";
String str = "F:\\testZip\\上传申请书证据材料\\证据材料\\仲裁裁决书模板.docx";
List<Integer> allindex = new ArrayList<>();
int a = str.indexOf(key);//*第一个出现的索引位置
allindex.add(a);
while (a != -1) {
System.out.print(a + "\t");
a = str.indexOf(key, a + 1);//*从这个索引往后开始第一个出现的位置
allindex.add(a);
}
String substr = str.substring(allindex.get(1)+1,allindex.get(2));
System.out.println("substr---"+substr);
File file = new File("D:\\home\\ruoyi\\仲裁委项目-测试单.zip");
String targetPath = "D:\\home\\unzip\\";
unZipFile(file,targetPath);
}
public static boolean unZipFile(File aboriginalFile, String targetPath) {
if (!aboriginalFile.exists()) {
log.error("此文件不存在:",aboriginalFile.getPath());
log.error("此文件不存在:", aboriginalFile.getPath());
return false;
}
try {
@@ -65,18 +36,18 @@ public class UnZipFileUtils {
String directPath = targetPath + File.separator + entryZip.getName();
File directFile = new File(directPath);
boolean makeDirs = directFile.mkdirs();
System.out.println("创建文件夹是否成功:" +makeDirs);
System.out.println("创建文件夹是否成功:" + makeDirs);
} else {
//若是文件,就创建文件
File sourceFile = new File(targetPath + File.separator + entryZip.getName());
if(!sourceFile.getParentFile().exists()){
if (!sourceFile.getParentFile().exists()) {
boolean mDirect = sourceFile.getParentFile().mkdirs();
System.out.println("创建压缩文件时,创建父文件夹是否成功:" +mDirect);
System.out.println("创建压缩文件时,创建父文件夹是否成功:" + mDirect);
}
boolean newUnZipFile = sourceFile.createNewFile();
if (newUnZipFile){
if (newUnZipFile) {
// 将压缩文件内容写入到这个文件中
try{
try {
InputStream is = srcZipFile.getInputStream(entryZip);
FileOutputStream fos = new FileOutputStream(sourceFile);
int len;
@@ -84,8 +55,8 @@ public class UnZipFileUtils {
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (Exception e){
log.error("解压失败",e);
} catch (Exception e) {
log.error("解压失败", e);
}
}
}
@@ -93,7 +64,7 @@ public class UnZipFileUtils {
return true;
} catch (IOException e) {
log.error("解压失败",e);
log.error("解压失败", e);
return false;
}
@@ -106,11 +77,11 @@ public class UnZipFileUtils {
return;
}
File[] fileArray = fileSource.listFiles();
if(fileArray!=null&&fileArray.length>0){
if (fileArray != null && fileArray.length > 0) {
for (File fileIter : fileArray) {
if (fileIter.isDirectory()) {
getFiles(fileIter,allFiles);
}else {
getFiles(fileIter, allFiles);
} else {
allFiles.add(fileIter);
}
@@ -120,7 +91,4 @@ public class UnZipFileUtils {
}
}