// zip文件上传
public Map uploadFile(MultipartFile[] files) {
Map outMap = new HashMap<String, Object>();
try {
// zip文件存放路径
String path = localpath + "/document";
File dir = new File(path);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
int fileLength = files.length;
for (int i = 0; i < fileLength; i++) {
MultipartFile upfile = files[i];
String upFilename = upfile.getOriginalFilename();
String suffix = upFilename.substring(upFilename.lastIndexOf("."));
String filename = UUID.randomUUID().toString() + suffix;
File targetFile = new File(path, filename);
upfile.transferTo(targetFile);
outMap.put("url", "/upload/document/" + filename);
outMap.put("state", "success");
zipTest(path + "/" + filename);
}
} catch (Exception e) {
e.printStackTrace();
outMap.put("state", "error");
}
return outMap;
}
public void zipTest(String lpath) throws Exception {
// 解压文件存放路径
String newPath = localpath + "/zipFile";
File file = new File(lpath);
// 文件是否存在
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
ZipFile zipFile = null;
ZipInputStream zin = null;
FileInputStream fis = null;
try {
Charset gbk = Charset.forName("GBK");
zipFile = new ZipFile(file, gbk);
fis = new FileInputStream(file);
zin = new ZipInputStream(fis, gbk);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
String path = ze.getName();
System.out.println(path);
if (!ze.isDirectory() ) {
InputStream inputStream = zipFile.getInputStream(ze);
File deskFile = new File(newPath,path);
FileOutputStream output = new FileOutputStream(deskFile);
IOUtils.copy(inputStream, output);
output.close();
inputStream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (zin != null) {
zin.closeEntry();
zin.close();
}
if (fis != null)
fis.close();
if (zipFile != null)
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java 上传zip文件把解压后的文件存到另外的文件夹
于 2022-05-24 17:26:57 首次发布