此方法可以直接解压缩zip中的文件及文件夹至指定目录,zip中文件名称不能包含繁体字符。
import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* @description: 解壓縮文件
* @author: libie
* @createTime: 2022/6/10 上午 08:50
**/
public class ZipFileUtil {
/**
* @description: 解壓縮文件
* @author: H2103424
* @dateTime: 2022/6/10 下午 01:50
*
* @param file 需要解壓縮的zip文件
* @param uncompressPath 解壓縮目的目錄
* @return 壓縮包中包含的文件及文件夾
* @throws IOException
*/
public static List<String> uncompress(File file, String uncompressPath) throws IOException {
List<String> filePaths = new ArrayList<>();
File path = new File(uncompressPath);
if (!path.exists()){
if (!path.mkdirs())
throw new IOException("解壓路徑創建失敗:"+uncompressPath);
System.out.println("解壓路徑創建成功:"+uncompressPath);
}
BufferedOutputStream os = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(file);
Enumeration ele = zipfile.entries();
while (ele.hasMoreElements()) {
try {
entry = (ZipEntry) ele.nextElement();
} catch (IllegalArgumentException e){
System.err.println("文件夾或文件名不合法,請不要包含繁體漢字及特殊字符:"+e);
continue;
}
if( entry.isDirectory()){
System.out.println("創建文件夾 "+entry.getName());
String name = entry.getName();
name = name.substring(0, name.length() - 1);
File fileObject = new File(uncompressPath + name);
if (!fileObject.exists() && !fileObject.mkdir()){
System.err.println("文件夾創建失敗:"+fileObject.getName());
}
}else{
System.out.println("解壓文件 "+entry.getName());
is = new BufferedInputStream(zipfile.getInputStream(entry));
int count;
int buffer = 1024;
byte[] dataByte = new byte[buffer];
FileOutputStream fos = new FileOutputStream(uncompressPath+entry.getName());
os = new BufferedOutputStream(fos, buffer);
while ((count = is.read(dataByte, 0, buffer)) != -1) {
os.write(dataByte, 0, count);
}
os.flush();
os.close();
is.close();
}
filePaths.add(uncompressPath+entry.getName());
}
zipfile.close();
return filePaths;
}
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\H2103424\\Desktop\\Pictures.zip");
System.out.println(uncompress(file, "C:\\Users\\H2103424\\Desktop\\testZip\\"));
}
}
压缩包:
解压效果:
方法输出及返回值:
解壓路徑創建成功:C:\Users\H2103424\Desktop\testZip\
創建文件夾 Pictures/
解壓文件 Pictures/1.bmp
解壓文件 Pictures/1.jpeg
解壓文件 Pictures/1.jpg
創建文件夾 Pictures/1a/
解壓文件 Pictures/1a/1.jpeg
解壓文件 Pictures/1a/1.jpg
創建文件夾 Pictures/1a/3c/
解壓文件 Pictures/1a/3c/2.jpg
解壓文件 Pictures/1a/3c/3.jpg
創建文件夾 Pictures/1a/4d/
解壓文件 Pictures/2.jpg
創建文件夾 Pictures/2b/
解壓文件 Pictures/2b/2.jpg
解壓文件 Pictures/3.jpg
解壓文件 Pictures/3241_45235_342.3ds
解壓文件 Pictures/4.jpg
創建文件夾 Pictures/5e/
解壓文件 Pictures/aaa.jpg
解壓文件 Pictures/desktop.ini
解壓文件 Pictures/m03_j03_sa.dwg
解壓文件 Pictures/rewq_rewq.dwg
[C:\Users\H2103424\Desktop\testZip\Pictures/, C:\Users\H2103424\Desktop\testZip\Pictures/1.bmp, C:\Users\H2103424\Desktop\testZip\Pictures/1.jpeg, C:\Users\H2103424\Desktop\testZip\Pictures/1.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/, C:\Users\H2103424\Desktop\testZip\Pictures/1a/1.jpeg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/1.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/3.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/4d/, C:\Users\H2103424\Desktop\testZip\Pictures/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/2b/, C:\Users\H2103424\Desktop\testZip\Pictures/2b/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/3.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/3241_45235_342.3ds, C:\Users\H2103424\Desktop\testZip\Pictures/4.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/5e/, C:\Users\H2103424\Desktop\testZip\Pictures/aaa.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/desktop.ini, C:\Users\H2103424\Desktop\testZip\Pictures/m03_j03_sa.dwg, C:\Users\H2103424\Desktop\testZip\Pictures/rewq_rewq.dwg]
Process finished with exit code 0
Controller调用:
@Override
public ResultObj uploadPhotos(MultipartFile file, WebSysUserEntity loginUser){
String uncompressPath = "C:\\Users\\H2103424\\Desktop\\testZip\\";
try(InputStream inputStream = file.getInputStream()){
File photosZip = new File("photos.zip");
FileUtils.copyInputStreamToFile(inputStream, photosZip);
photosZip.delete();
ZipFileUtil.uncompress(photosZip, uncompressPath);
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}