如何实现一个上传压缩包,然后将压缩包进行解压?
下面我们就来实现一下这个代码。
最终实现的效果:
上传一个压缩包文件,会将压缩包文件下载到本地,然后再将压缩包解压到同目录下的extracted文件夹下。
这是压缩包内的文件,三张图片
一、使用的技术
使用的是java自带的java.util.zip.zipfile包。
java.util.zip.zipfile
是 java 中用于处理 zip 文件的类。它提供了读取 zip 文件内容的功能,允许你以编程方式从 zip 文件中提取文件和目录结构,以及读取其中的数据。
下面是一些 zipfile
类的主要特性和用法:
-
读取 zip 文件内容:
zipfile
允许你打开一个 zip 文件,并从中读取内容。你可以通过提供 zip 文件的路径或者file
对象来创建一个zipfile
实例。 -
访问 zip 文件中的条目(entries): 一旦你打开了一个 zip 文件,你可以通过
entries()
方法获取 zip 文件中的所有条目。每个条目代表 zip 文件中的一个文件或目录。 -
提取文件内容: 通过
getinputstream()
方法,你可以从 zip 文件中提取特定文件的内容。这个方法返回一个inputstream
对象,你可以使用它来读取文件的数据。 -
关闭 zip 文件: 使用
close()
方法关闭zipfile
实例,释放相关资源。
二、实际用法
实际使用的时候是从前端上传压缩包文件,然后后端接收。(具体参考后文contrller中的完整代码逻辑)
1、上传的一般都是multipartfile
类型,需要先将multipartfile类型转为file类型,使用fileutils.copyinputstreamtofile
这个方法。
2、转换为file类型时就需要先将文件存到本地,所以在这里指定了一个压缩包的文件路径file filetemp = new file("d:\\unziptest\\test.zip");
3、使用getabsolutepath
方法来获取这个路径,将压缩包的本地路径传给fileuncompressing
文件解压缩方法即可完成解压。
4、在文件解压缩方法中,会将文件解压到d:\\unziptest\\extracted
这个目录下,可以自行修改,也可以作为一个参数传进去。
我这里解压缩的时候获取了某个固定文件类型的文件路径集合,如果需要在解压缩的时候做逻辑处理,可以参考。如果只是单纯的解压缩,可以把这个逻辑删掉。
//要下载到的本地临时文件路径
file filetemp = new file("d:\\unziptest\\test.zip");
file parentfolder = filetemp.getparentfile();
//如果父文件夹(unziptest)不存在则创建
if(!parentfolder.exists()){
//创建父文件夹及其所有上级文件夹
parentfolder.mkdir();
}
//multipartfile转file
org.apache.commons.io.fileutils.copyinputstreamtofile(multipartfile.getinputstream(), filetemp);
//压缩包文件路径
string zipfilepath = filetemp.getabsolutepath();
fileuncompressing(zipfilepath);
三、文件解压缩方法(通用)
/**
* 文件解压缩
* @return
*/
public static list<string> fileuncompressing(string zipfilepath){
string targetfolder = "d:\\unziptest\\extracted";
//文件名后缀
string nclastname=".nc";
list<string> pathlist=new arraylist<>();
try {
// 创建目标文件夹
file targetdir = new file(targetfolder);
targetdir.mkdirs();
// 打开压缩文件
// zipfile zipfile = new zipfile(zipfilepath);
zipfile zipfile = new zipfile(zipfilepath, charset.forname("gbk"));
// 遍历压缩文件中的所有条目
enumeration<? extends zipentry> entries = zipfile.entries();
while (entries.hasmoreelements()) {
zipentry entry = entries.nextelement();
// 解压缩条目到目标文件夹
string entryname = entry.getname();
entryname = new string(entryname.getbytes(charset.forname("gbk")));
file entryfile = new file(targetdir, entryname);
if (entry.isdirectory()) {
entryfile.mkdirs();
} else {
entryfile.getparentfile().mkdirs();
inputstream inputstream = zipfile.getinputstream(entry);
outputstream outputstream = new fileoutputstream(entryfile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputstream.read(buffer)) > 0) {
outputstream.write(buffer, 0, length);
}
outputstream.close();
inputstream.close();
}
//判断文件是否是.nc结尾
if (entryname.endswith(nclastname)) {
// 拼接路径
path path = paths.get(targetfolder, entryname);
pathlist.add(path.tostring());
}
}
// 关闭压缩文件
zipfile.close();
// log.info("文件读取完成");
system.out.println("文件读取完成");
} catch (zipexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
return pathlist;
}
四、完整代码逻辑
@restcontroller
@requestmapping("/practice")
public class practicecontroller extends basecontroller
{
@postmapping("/list")
public tabledatainfo list(multipartfile multipartfile) throws ioexception {
//要下载到的本地临时文件路径
file filetemp = new file("d:\\unziptest\\test.zip");
file parentfolder = filetemp.getparentfile();
//如果父文件夹(unziptest)不存在则创建
if(!parentfolder.exists()){
//创建父文件夹及其所有上级文件夹
parentfolder.mkdir();
}
//multipartfile转file
org.apache.commons.io.fileutils.copyinputstreamtofile(multipartfile.getinputstream(), filetemp);
//压缩包文件路径
string zipfilepath = filetemp.getabsolutepath();
fileuncompressing(zipfilepath);
startpage();
list<studentscores> list = new arraylist<>();
return getdatatable(list);
}
/**
* 文件解压缩
* @return
*/
public static list<string> fileuncompressing(string zipfilepath){
// string zipfilepath = "d:\\unziptest\\era5.zip";
string targetfolder = "d:\\unziptest\\extracted";
//文件名后缀
string nclastname=".nc";
list<string> pathlist=new arraylist<>();
try {
// 创建目标文件夹
file targetdir = new file(targetfolder);
targetdir.mkdirs();
// 打开压缩文件
// zipfile zipfile = new zipfile(zipfilepath);
zipfile zipfile = new zipfile(zipfilepath, charset.forname("gbk"));
// 遍历压缩文件中的所有条目
enumeration<? extends zipentry> entries = zipfile.entries();
while (entries.hasmoreelements()) {
zipentry entry = entries.nextelement();
// 解压缩条目到目标文件夹
string entryname = entry.getname();
entryname = new string(entryname.getbytes(charset.forname("gbk")));
file entryfile = new file(targetdir, entryname);
if (entry.isdirectory()) {
entryfile.mkdirs();
} else {
entryfile.getparentfile().mkdirs();
inputstream inputstream = zipfile.getinputstream(entry);
outputstream outputstream = new fileoutputstream(entryfile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputstream.read(buffer)) > 0) {
outputstream.write(buffer, 0, length);
}
outputstream.close();
inputstream.close();
}
//判断文件是否是.nc结尾
if (entryname.endswith(nclastname)) {
// 拼接路径
path path = paths.get(targetfolder, entryname);
pathlist.add(path.tostring());
}
}
// 关闭压缩文件
zipfile.close();
// log.info("文件读取完成");
system.out.println("文件读取完成");
} catch (zipexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
return pathlist;
}
}
发表评论