您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java如何实现解析zip压缩包并获取文件内容

2024/2/24 16:26:21发布18次查看
功能描述页面上传一个源码压缩包,后端将压缩包解压,并获取每个文件中的内容。
相关源码(1)首先定义一个与解压文件对应的实体类。
package com.sonar.data.vo; import lombok.data; /** * 文件解析对象 * * @author yuanqiang.zhang * @since 2022/7/12 */@datapublic class unzipfilevo { /** * 类型:0-文件夹;1-文件 */ private integer type; /** * 文件路径(如:src/main/java/com/zyq/entity/user.java) */ private string path; /** * 文件内容 */ private string content; }
(2)接下来就是解压的工具类了。
package com.sonar.data.utils.business; import com.sonar.data.vo.unzipfilevo;import org.springframework.web.multipart.multipartfile; import java.io.*;import java.util.*;import java.util.zip.zipentry;import java.util.zip.zipfile; /** * 解析文件工具类 * * @author yuanqiang.zhang * @since 2022/7/12 */@suppresswarnings("unused")public class unziputils { public static int byte_len = 10240; /** * 本地文件解压 * * @param file 具体文件 * @return 解压后的文件列表 */ public static list<unzipfilevo> unzip(file file) { if (objects.isnull(file) || !file.exists()) { return collections.emptylist(); } zipfile zip = null; try { zip = new zipfile(file); } catch (ioexception e) { e.printstacktrace(); } if (objects.isnull(zip)) { return collections.emptylist(); } enumeration<? extends zipentry> entries = zip.entries(); list<unzipfilevo> vos = new arraylist<>(); while (entries.hasmoreelements()) { zipentry entry = entries.nextelement(); string path = entry.getname(); unzipfilevo vo = new unzipfilevo(); vo.setpath(path); // 解析文件夹 boolean directory = entry.isdirectory(); if (directory) { vo.settype(0); vos.add(vo); continue; } // 解析文件 vo.settype(1); stringbuilder sb = new stringbuilder(); try (inputstream in = zip.getinputstream(entry); inputstreamreader inputstreamreader = new inputstreamreader(in); bufferedreader reader = new bufferedreader(inputstreamreader)) { string line; while ((line = reader.readline()) != null) { sb.append(line).append("\n"); } } catch (ioexception e) { e.printstacktrace(); } vo.setcontent(sb.tostring()); vos.add(vo); } if (objects.nonnull(zip)) { try { zip.close(); } catch (ioexception e) { e.printstacktrace(); } } return vos; } /** * 上传文件解压 * * @param multipartfile 上传文件 * @return 解压后的文件列表 */ public static list<unzipfilevo> unzip(multipartfile multipartfile) { file file = getfile(multipartfile); if (objects.isnull(file)) { return collections.emptylist(); } list<unzipfilevo> vos = unzip(file); if (file.exists()) { boolean delete = file.delete(); if (delete) { system.out.println(file.getname() + " 临时文件删除成功!"); } else { system.out.println(file.getname() + " 临时文件删除失败!"); } } return vos; } /** * multipartfile 转 file * * @param multipartfile 上传文件 * @return 本地文件 */ private static file getfile(multipartfile multipartfile) { string filename = system.currenttimemillis() + "_" + multipartfile.getoriginalfilename(); file file = new file("d://" + filename); int len; try (outputstream os = new fileoutputstream(file); inputstream in = multipartfile.getinputstream()) { byte[] buffer = new byte[byte_len]; while ((len = in.read(buffer, 0, byte_len)) != -1) { os.write(buffer, 0, len); } } catch (ioexception e) { e.printstacktrace(); return null; } return file; }}
调用说明工具类中提供了两个解压的方式:
方式一:本地文件 file 进行解压。
方式二:上传文件 multipartfile 进行解压。
package com.sonar.data.controller; import com.sonar.data.utils.business.unziputils;import com.sonar.data.vo.unzipfilevo;import org.springframework.web.bind.annotation.postmapping;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.requestpart;import org.springframework.web.bind.annotation.restcontroller;import org.springframework.web.multipart.multipartfile; import java.io.file;import java.util.list; /** * @author yuanqiang.zhang * @since 2022/7/8 */@restcontroller@requestmapping("/test")public class testcontroller { /** * 上传文件解压(示例) */ @postmapping("/import") public list<unzipfilevo> importtest(@requestpart("file") multipartfile mfile) { return unziputils.unzip(mfile); } /** * 本地文件解压(示例) */ public static void main(string[] args) { file file = new file("src.zip"); boolean delete = file.delete(); system.out.println(delete); } }
测试效果我们以上传压缩包解析为例,通过 postman 进行接口测试,解析的结果如下。
以上就是java如何实现解析zip压缩包并获取文件内容的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product