当前位置:网站首页>Tool classes for extracting zip files
Tool classes for extracting zip files
2022-06-12 09:03:00 【LiBie lzh】
This method can directly decompress zip To the specified directory ,zip The file name in cannot contain traditional characters .
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: Unzip file
* @author: libie
* @createTime: 2022/6/10 In the morning 08:50
**/
public class ZipFileUtil {
/**
* @description: Unzip file
* @author: H2103424
* @dateTime: 2022/6/10 Afternoon 01:50
*
* @param file Need to decompress zip file
* @param uncompressPath Unzip the destination directory
* @return Files and folders contained in the compressed package
* @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(" Decompression path creation failed :"+uncompressPath);
System.out.println(" The decompression path was created successfully :"+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(" Illegal folder or file name , Please do not include traditional Chinese characters and special characters :"+e);
continue;
}
if( entry.isDirectory()){
System.out.println(" Create folder "+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(" Folder creation failed :"+fileObject.getName());
}
}else{
System.out.println(" Unzip the file "+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\\"));
}
}
Compressed package :
Decompression effect :

Method output and return value :
The decompression path was created successfully :C:\Users\H2103424\Desktop\testZip\
Create folder Pictures/
Unzip the file Pictures/1.bmp
Unzip the file Pictures/1.jpeg
Unzip the file Pictures/1.jpg
Create folder Pictures/1a/
Unzip the file Pictures/1a/1.jpeg
Unzip the file Pictures/1a/1.jpg
Create folder Pictures/1a/3c/
Unzip the file Pictures/1a/3c/2.jpg
Unzip the file Pictures/1a/3c/3.jpg
Create folder Pictures/1a/4d/
Unzip the file Pictures/2.jpg
Create folder Pictures/2b/
Unzip the file Pictures/2b/2.jpg
Unzip the file Pictures/3.jpg
Unzip the file Pictures/3241_45235_342.3ds
Unzip the file Pictures/4.jpg
Create folder Pictures/5e/
Unzip the file Pictures/aaa.jpg
Unzip the file Pictures/desktop.ini
Unzip the file Pictures/m03_j03_sa.dwg
Unzip the file 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 0Controller call :
@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;
}
}边栏推荐
- 2022 simulated examination platform operation of high voltage electrician work license question bank
- Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock
- Convert spaces to < br / > newline labels
- torch. logical_ And() method
- Judge whether the object is empty
- Background color translucent
- mySql学习记录——二、mySql建表命令
- Grab screen and ground glass effect
- Chapter 8 - two basic problems of data processing
- Dynamically create and submit forms
猜你喜欢

EIP-1559

Background attribute compound writing

Machine learning notes - circular neural network memo list

【无标题】Task3 多路召回

Popular understanding of time domain sampling and frequency domain continuation
![[character set 7] what are the wide character codes and multi byte codes of Chinese characters](/img/8c/6d375d90234e6094b6930c2cefefa1.png)
[character set 7] what are the wide character codes and multi byte codes of Chinese characters

Introduction to applet cloud development -- questionnaire evaluation applet practice (7)

torch. logical_ And() method
![[advanced pointer I] character array & array pointer & pointer array](/img/ea/150b2162e4e1641eee7e852935d101.png)
[advanced pointer I] character array & array pointer & pointer array

torch.logical_and()方法
随机推荐
Graphic analysis of viewbox in SVG
Method to limit the input box to only numbers
机器学习笔记 - 循环神经网络备忘清单
Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low
Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression
Adjust SVG width and height
Technology cloud report: how will the industrial Internet rebuild the security boundary in 2022?
Sword finger offer II 016 Longest substring without repeating characters - sliding window
When converting tensor to ndarray in tensorflow, the run or Eval function is constantly called in the loop, and the code runs more and more slowly!
Notes used by mqtt (combined with source code)
ISCSI详解(五)——ISCSI客户端配置实战
Dynamically create and submit forms
Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock
解压缩zip文件的工具类
Flink passes in custom parameters or profiles
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
Construction of memcached cache service under Linux:
Handling abnormal data
(13) Text rendering text
Binlog in mysql: