Java使用Apache compress實(shí)現(xiàn)文件夾壓縮成Zip包
Apache common提供了很多實(shí)用的工具包,下面就說一下如何用compress包來壓縮文件夾。先引入compress,io和lang3這3個(gè)工具包:
<dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8</version> </dependency> </dependencies>
這個(gè)方法實(shí)現(xiàn)了將文件夾下所有的文件壓縮成zip包,并輸出到文件流中,可以直接寫入到文件或提供給前端下載,工具類如下:
import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.archivers.zip.Zip64Mode; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.filefilter.TrueFileFilter; import org.apache.commons.lang3.StringUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Collection; public class ZipUtils { /** * 壓縮文件夾下的所有文件 * @param dir 要壓縮的文件夾 * @param outputStream 輸出壓縮后的文件流 * @throws IOException IO異常 * @throws ArchiveException 壓縮異常 */ public static void zip(File dir, OutputStream outputStream) throws IOException, ArchiveException { ZipArchiveOutputStream zipOutput = null; try { zipOutput = (ZipArchiveOutputStream) new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputStream); zipOutput.setEncoding("utf-8"); zipOutput.setUseZip64(Zip64Mode.AsNeeded); Collection<File> files = FileUtils.listFilesAndDirs(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); for (File file : files) { InputStream in = null; try { if (file.getPath().equals(dir.getPath())) { continue; } String relativePath = StringUtils.replace(file.getPath(), dir.getPath() + File.separator, ""); ZipArchiveEntry entry = new ZipArchiveEntry(file, relativePath); zipOutput.putArchiveEntry(entry); if (file.isDirectory()) { zipOutput.closeArchiveEntry(); continue; } in = new FileInputStream(file); IOUtils.copy(in, zipOutput); zipOutput.closeArchiveEntry(); } finally { if (in != null) { IOUtils.closeQuietly(in); } } } zipOutput.finish(); } finally { IOUtils.closeQuietly(zipOutput); } } public static void main(String args[]) throws IOException, ArchiveException { //要壓縮的文件路徑 File dir = new File("C:\\data\\data"); //壓縮后zip包文件路徑 File dest = new File("C:\\data\\test.zip"); OutputStream outputStream = new FileOutputStream(dest); zip(dir, outputStream); } }
執(zhí)行main函數(shù)跑測(cè)試用例,發(fā)現(xiàn)程序?qū)ir路徑下的所有文件已經(jīng)壓縮并輸出zip包。
到此這篇關(guān)于Java使用Apache compress實(shí)現(xiàn)文件夾壓縮成Zip包的文章就介紹到這了,更多相關(guān)Java文件夾壓縮成Zip內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java8中關(guān)于Function.identity()的使用
這篇文章主要介紹了Java8中關(guān)于Function.identity()的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Spring Security將用戶數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Spring Security將用戶數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Spring中的@CrossOrigin注解的使用詳細(xì)解讀
這篇文章主要介紹了Spring中的@CrossOrigin注解的使用詳細(xì)解讀,跨源資源共享(CORS),是由大多數(shù)瀏覽器實(shí)現(xiàn)的W3C規(guī)范,允許對(duì)跨域請(qǐng)求進(jìn)行靈活授權(quán),用來代替IFRAME或JSONP等非正規(guī)實(shí)現(xiàn)方式,需要的朋友可以參考下2023-11-11Caused by: java.io.IOException: DerInputStrea
這篇文章主要介紹了Caused by: java.io.IOException: DerInputStream.getLength(): lengthTag=111, too big,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10Java JTable 實(shí)現(xiàn)日歷的示例
這篇文章主要介紹了Java JTable 實(shí)現(xiàn)日歷的示例,幫助大家更好的理解和學(xué)習(xí)Java jtable的使用方法,感興趣的朋友可以了解下2020-10-10基于Springboot的漫畫網(wǎng)站平臺(tái)設(shè)計(jì)與實(shí)現(xiàn)
本文將基于Springboot實(shí)現(xiàn)開發(fā)一個(gè)漫畫主題的網(wǎng)站,實(shí)現(xiàn)一個(gè)比漂亮的動(dòng)漫連載的網(wǎng)站系統(tǒng),界面設(shè)計(jì)優(yōu)雅大方,比較適合做畢業(yè)設(shè)計(jì)和課程設(shè)計(jì)使用,需要的可以參考一下2022-08-08