Java處理壓縮文件的步驟詳解
背景
在項(xiàng)目出現(xiàn)上傳文件,其中文件包含壓縮包,并對(duì)壓縮包的內(nèi)容進(jìn)行解析保存。
第一步:編寫代碼
1.1 請(qǐng)求層
我們用倒敘的方式來寫。先寫 ZipController
@Autowired private ZipService zipService; /** * 上傳二維碼文件 * @param qrCodeFile 二維碼文件 * @return 返回上傳的結(jié)果 */ @ApiOperation(value = "上傳二維碼文件") @PostMapping("/uploadQrCodeFile") public Result uploadQrCodeFile(@RequestParam("file") MultipartFile qrCodeFile) throws Exception { zipService.uploadQrCodeFile(qrCodeFile); return Result.sendSuccess("上傳成功"); }
1.2 業(yè)務(wù)處理層
接著就是寫 Service
@Service public class ZipService { private static final Logger logger= LoggerFactory.getLogger(ZipService.class); public void uploadQrCodeFile(MultipartFile multipartFile)throws Exception { if (multipartFile.getSize() == 0 || multipartFile.getOriginalFilename() == null || (multipartFile.getOriginalFilename() != null && !multipartFile.getOriginalFilename().contains("."))) { ExceptionCast.cast(Result.sendFailure("文件格式不正確或文件為空!")); } // 1.先下載文件到本地 String originalFilename = multipartFile.getOriginalFilename(); String destPath = System.getProperty("user.dir") + File.separator + "qrCodeFile"; FileUtil.writeFromStream( multipartFile.getInputStream(), new File(destPath + File.separator + originalFilename)); // 2.解壓文件 unzipAndSaveFileInfo(originalFilename, destPath); // 3.備份壓縮文件,刪除解壓的目錄 FileUtils.copyFile( new File(destPath + File.separator + originalFilename), new File(destPath + File.separator + "backup" + File.separator + originalFilename)); // 刪除原來的上傳的臨時(shí)壓縮包 FileUtils.deleteQuietly(new File(destPath + File.separator + originalFilename)); logger.info("文件上傳成功,文件名為:{}", originalFilename); } /** * 解壓和保存文件信息 * * @param originalFilename 源文件名稱 * @param destPath 目標(biāo)路徑 */ private void unzipAndSaveFileInfo(String originalFilename, String destPath) throws IOException { if (StringUtils.isEmpty(originalFilename) || !originalFilename.contains(".")) { ExceptionCast.cast(Result.sendFailure("文件名錯(cuò)誤!")); } // 壓縮 ZipUtil.unzip( new File(destPath + File.separator + originalFilename), new File(destPath), Charset.forName("GBK")); // 遍歷文件信息 String fileName = originalFilename.substring(0, originalFilename.lastIndexOf(".")); File[] files = FileUtil.ls(destPath + File.separator + fileName); if (files.length == 0) { ExceptionCast.cast(Result.sendFailure("上傳文件為空!")); } String targetPath = destPath + File.separator + "images"; for (File file : files) { // 復(fù)制文件到指定目錄 String saveFileName = System.currentTimeMillis() + new SecureRandom().nextInt(100) + file.getName(); FileUtils.copyFile(file, new File(targetPath + File.separator + saveFileName)); logger.info("文件名稱:"+file.getName()); logger.info("文件所在目錄地址:"+saveFileName); logger.info("文件所在目錄地址:"+targetPath + File.separator + saveFileName); } } }
1.3 新增配置
因spring boot有默認(rèn)上傳文件大小限制,故需配置文件大小。在 application.properties
中添加 upload
的配置
#### upload begin ### spring.servlet.multipart.enabled=true spring.servlet.multipart.max-request-size=10MB spring.servlet.multipart.max-file-size=10MB #### upload end ###
第二步:解壓縮處理
2.1 引入依賴
引入 Apache
解壓 / 壓縮 工具類處理,解壓 tar.gz
文件
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.20</version> </dependency>
2.2 解壓縮工具類
- 將
tar.gz
轉(zhuǎn)換為tar
- 解壓
tar
import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.utils.IOUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; // tar.gz 文件路徑 String sourcePath = "D:\\daleyzou.tar.gz"; // 要解壓到的目錄 String extractPath = "D:\\test\\daleyzou"; File sourceFile = new File(sourcePath); // decompressing *.tar.gz files to tar TarArchiveInputStream fin = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(sourceFile))); File extraceFolder = new File(extractPath); TarArchiveEntry entry; // 將 tar 文件解壓到 extractPath 目錄下 while ((entry = fin.getNextTarEntry()) != null) { if (entry.isDirectory()) { continue; } File curfile = new File(extraceFolder, entry.getName()); File parent = curfile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } // 將文件寫出到解壓的目錄 IOUtils.copy(fin, new FileOutputStream(curfile)); }
總結(jié)
到此這篇關(guān)于Java處理壓縮文件的步驟詳解的文章就介紹到這了,更多相關(guān)Java處理壓縮文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis高級(jí)映射及延遲加載的實(shí)現(xiàn)
MyBatis在處理對(duì)象關(guān)系映射時(shí),多對(duì)一關(guān)系是常見的場(chǎng)景,本文就來介紹了MyBatis高級(jí)映射及延遲加載的實(shí)現(xiàn),感興趣的可以了解一下2024-11-11SSM項(xiàng)目中使用攔截器和過濾器的實(shí)現(xiàn)示例
這篇文章主要介紹了SSM項(xiàng)目中使用攔截器和過濾器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java 圖片與byte數(shù)組互相轉(zhuǎn)換實(shí)例
下面小編就為大家?guī)硪黄狫ava 圖片與byte數(shù)組互相轉(zhuǎn)換實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫方式
這篇文章主要介紹了Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06JAVA PDF操作之實(shí)現(xiàn)截取N頁和多個(gè)PDF合并
這篇文章主要為大家詳細(xì)介紹了java關(guān)于PDF的一些操作,例如截取N頁并生成新文件,轉(zhuǎn)圖片以及多個(gè)PDF合并,文中的示例代碼講解詳細(xì),感興趣的可以了解下2025-01-01Java8新特性之空指針異常的克星Optional類的實(shí)現(xiàn)
這篇文章主要介紹了Java8新特性之空指針異常的克星Optional類的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10mybatis使用pageHelper插件進(jìn)行查詢分頁
這篇文章主要介紹了mybatis使用pageHelper插件進(jìn)行查詢分頁,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08Java實(shí)現(xiàn)基于token認(rèn)證的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)基于token認(rèn)證的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08