Java如何实现解析zip压缩包并获取文件内容(java,zip,开发技术)

时间:2024-05-02 12:48:42 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

功能描述

页面上传一个源码压缩包,后端将压缩包解压,并获取每个文件中的内容。

相关源码

(1)首先定义一个与解压文件对应的实体类。

packagecom.sonar.data.vo;importlombok.Data;/***文件解析对象**@authorYuanqiang.Zhang*@since2022/7/12*/@DatapublicclassUnzipFileVo{/***类型:0-文件夹;1-文件*/privateIntegertype;/***文件路径(如:src/main/java/com/zyq/entity/User.java)*/privateStringpath;/***文件内容*/privateStringcontent;}

(2)接下来就是解压的工具类了。

packagecom.sonar.data.utils.business;importcom.sonar.data.vo.UnzipFileVo;importorg.springframework.web.multipart.MultipartFile;importjava.io.*;importjava.util.*;importjava.util.zip.ZipEntry;importjava.util.zip.ZipFile;/***解析文件工具类**@authorYuanqiang.Zhang*@since2022/7/12*/@SuppressWarnings("unused")publicclassUnZipUtils{publicstaticintBYTE_LEN=10240;/***本地文件解压**@paramfile具体文件*@return解压后的文件列表*/publicstaticList<UnzipFileVo>unzip(Filefile){if(Objects.isNull(file)||!file.exists()){returnCollections.emptyList();}ZipFilezip=null;try{zip=newZipFile(file);}catch(IOExceptione){e.printStackTrace();}if(Objects.isNull(zip)){returnCollections.emptyList();}Enumeration<?extendsZipEntry>entries=zip.entries();List<UnzipFileVo>vos=newArrayList<>();while(entries.hasMoreElements()){ZipEntryentry=entries.nextElement();Stringpath=entry.getName();UnzipFileVovo=newUnzipFileVo();vo.setPath(path);//解析文件夹booleandirectory=entry.isDirectory();if(directory){vo.setType(0);vos.add(vo);continue;}//解析文件vo.setType(1);StringBuildersb=newStringBuilder();try(InputStreamin=zip.getInputStream(entry);InputStreamReaderinputStreamReader=newInputStreamReader(in);BufferedReaderreader=newBufferedReader(inputStreamReader)){Stringline;while((line=reader.readLine())!=null){sb.append(line).append("\n");}}catch(IOExceptione){e.printStackTrace();}vo.setContent(sb.toString());vos.add(vo);}if(Objects.nonNull(zip)){try{zip.close();}catch(IOExceptione){e.printStackTrace();}}returnvos;}/***上传文件解压**@parammultipartFile上传文件*@return解压后的文件列表*/publicstaticList<UnzipFileVo>unzip(MultipartFilemultipartFile){Filefile=getFile(multipartFile);if(Objects.isNull(file)){returnCollections.emptyList();}List<UnzipFileVo>vos=unzip(file);if(file.exists()){booleandelete=file.delete();if(delete){System.out.println(file.getName()+"临时文件删除成功!");}else{System.out.println(file.getName()+"临时文件删除失败!");}}returnvos;}/***MultipartFile转File**@parammultipartFile上传文件*@return本地文件*/privatestaticFilegetFile(MultipartFilemultipartFile){StringfileName=System.currentTimeMillis()+"_"+multipartFile.getOriginalFilename();Filefile=newFile("D://"+fileName);intlen;try(OutputStreamos=newFileOutputStream(file);InputStreamin=multipartFile.getInputStream()){byte[]buffer=newbyte[BYTE_LEN];while((len=in.read(buffer,0,BYTE_LEN))!=-1){os.write(buffer,0,len);}}catch(IOExceptione){e.printStackTrace();returnnull;}returnfile;}}

调用说明

工具类中提供了两个解压的方式:

方式一:本地文件 File 进行解压。

方式二:上传文件 MultipartFile 进行解压。

packagecom.sonar.data.controller;importcom.sonar.data.utils.business.UnZipUtils;importcom.sonar.data.vo.UnzipFileVo;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestPart;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.multipart.MultipartFile;importjava.io.File;importjava.util.List;/***@authorYuanqiang.Zhang*@since2022/7/8*/@RestController@RequestMapping("/test")publicclassTestController{/***上传文件解压(示例)*/@PostMapping("/import")publicList<UnzipFileVo>importTest(@RequestPart("file")MultipartFilemFile){returnUnZipUtils.unzip(mFile);}/***本地文件解压(示例)*/publicstaticvoidmain(String[]args){Filefile=newFile("src.zip");booleandelete=file.delete();System.out.println(delete);}}

测试效果

我们以上传压缩包解析为例,通过 Postman 进行接口测试,解析的结果如下。

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

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Java如何实现解析zip压缩包并获取文件内容的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:java如何实现模拟USB接口的功能下一篇:

8 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18