java實(shí)現(xiàn)zip,gzip,7z,zlib格式的壓縮打包
本文主要介紹的是通過(guò)使用java的相關(guān)類可以實(shí)現(xiàn)對(duì)文件或文件夾的壓縮。
zlib是一種數(shù)據(jù)壓縮程序庫(kù),它的設(shè)計(jì)目標(biāo)是處理單純的數(shù)據(jù)(而不管數(shù)據(jù)的來(lái)源是什么)。
7z 是一種新的壓縮格式,它擁有目前最高的壓縮比。
gzip是一種文件壓縮工具(或該壓縮工具產(chǎn)生的壓縮文件格式),它的設(shè)計(jì)目標(biāo)是處理單個(gè)的文件。gzip在壓縮文件中的數(shù)據(jù)時(shí)使用的就是zlib。為了保存與文件屬性有關(guān)的信息,gzip需要在壓縮文件(*.gz)中保存更多的頭信息內(nèi)容,而zlib不用考慮這一點(diǎn)。但gzip只適用于單個(gè)文件,所以我們?cè)赨NIX/Linux上經(jīng)??吹降膲嚎s包后綴都是*.tar.gz或*.tgz,也就是先用tar把多個(gè)文件打包成單個(gè)文件,再用gzip壓縮的結(jié)果。
zip是適用于壓縮多個(gè)文件的格式(相應(yīng)的工具有PkZip和WinZip等),因此,zip文件還要進(jìn)一步包含文件目錄結(jié)構(gòu)的信息,比gzip的頭信息更多。但需要注意,zip格式可采用多種壓縮算法,我們常見(jiàn)的zip文件大多不是用zlib的算法壓縮的,其壓縮數(shù)據(jù)的格式與gzip大不一樣。
所以,你應(yīng)當(dāng)根據(jù)你的具體需求,選擇不同的壓縮技術(shù):如果只需要壓縮/解壓縮數(shù)據(jù),你可以直接用zlib實(shí)現(xiàn),如果需要生成gzip格式的文件或解壓其他工具的壓縮結(jié)果,你就必須用gzip或zip等相關(guān)的類來(lái)處理了。
maven依賴
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.12</version>
</dependency>
zip格式
public static void zip(String input, String output, String name) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(output));
String[] paths = input.split("\\|");
File[] files = new File[paths.length];
byte[] buffer = new byte[1024];
for (int i = 0; i < paths.length; i++) {
files[i] = new File(paths[i]);
}
for (int i = 0; i < files.length; i++) {
FileInputStream fis = new FileInputStream(files[i]);
if (files.length == 1 && name != null) {
out.putNextEntry(new ZipEntry(name));
} else {
out.putNextEntry(new ZipEntry(files[i].getName()));
}
int len;
// 讀入需要下載的文件的內(nèi)容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
}
gzip打包
public static void gzip(String input, String output, String name) throws Exception {
String compress_name = null;
if (name != null) {
compress_name = name;
} else {
compress_name = new File(input).getName();
}
byte[] buffer = new byte[1024];
try {
GzipParameters gp = new GzipParameters(); //設(shè)置壓縮文件里的文件名
gp.setFilename(compress_name);
GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(new FileOutputStream(output), gp);
FileInputStream fis = new FileInputStream(input);
int length;
while ((length = fis.read(buffer)) > 0) {
gcos.write(buffer, 0, length);
}
fis.close();
gcos.finish();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
7z打包
public static void z7z(String input, String output, String name) throws Exception {
try {
SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File(output));
SevenZArchiveEntry entry = null;
String[] paths = input.split("\\|");
File[] files = new File[paths.length];
for (int i = 0; i < paths.length; i++) {
files[i] = new File(paths[i].trim());
}
for (int i = 0; i < files.length; i++) {
BufferedInputStream instream = null;
instream = new BufferedInputStream(new FileInputStream(paths[i]));
if (name != null) {
entry = sevenZOutput.createArchiveEntry(new File(paths[i]), name);
} else {
entry = sevenZOutput.createArchiveEntry(new File(paths[i]), new File(paths[i]).getName());
}
sevenZOutput.putArchiveEntry(entry);
byte[] buffer = new byte[1024];
int len;
while ((len = instream.read(buffer)) > 0) {
sevenZOutput.write(buffer, 0, len);
}
instream.close();
sevenZOutput.closeArchiveEntry();
}
sevenZOutput.close();
} catch (IOException ioe) {
System.out.println(ioe.toString() + " " + input);
}
}
zlib打包
public static void zlib(String input, String output) throws Exception {
// DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(output));
DeflateParameters dp = new DeflateParameters();
dp.setWithZlibHeader(true);
DeflateCompressorOutputStream dcos = new DeflateCompressorOutputStream(new FileOutputStream(output),dp);
FileInputStream fis = new FileInputStream(input);
int length = (int) new File(input).length();
byte data[] = new byte[length];
// int length;
while ((length = fis.read(data)) > 0) {
dcos.write(data, 0, length);
}
fis.close();
dcos.finish();
dcos.close();
}
希望本文所述對(duì)你有所幫助,java實(shí)現(xiàn)zip,gzip,7z,zlib格式的壓縮打包內(nèi)容就給大家介紹到這里了。希望大家繼續(xù)關(guān)注我們的網(wǎng)站!想要學(xué)習(xí)java可以繼續(xù)關(guān)注本站。
相關(guān)文章
JDK動(dòng)態(tài)代理接口和接口實(shí)現(xiàn)類深入詳解
這篇文章主要介紹了JDK動(dòng)態(tài)代理接口和接口實(shí)現(xiàn)類,JDK動(dòng)態(tài)代理是代理模式的一種實(shí)現(xiàn)方式,因?yàn)樗腔诮涌趤?lái)做代理的,所以也常被稱為接口代理,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
MybatisPlus實(shí)現(xiàn)數(shù)據(jù)攔截的使用示例
在MyBatis-Plus中,可以通過(guò)自定義攔截器來(lái)實(shí)現(xiàn)對(duì)SQL語(yǔ)句的攔截和修改,本文就來(lái)介紹一下如何使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Java源碼分析:Guava之不可變集合ImmutableMap的源碼分析
今天給大家?guī)?lái)的是關(guān)于Java源碼的相關(guān)知識(shí),文章圍繞著Java ImmutableMap展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-06-06
Java調(diào)用dll文件的實(shí)現(xiàn)解析
這篇文章主要介紹了Java調(diào)用dll文件的實(shí)現(xiàn)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringCloud-Spring?Boot?Starter使用測(cè)試及問(wèn)題小結(jié)
Spring?Boot?Starter?是在?SpringBoot?組件中被提出來(lái)的一種概念、簡(jiǎn)化了很多煩瑣的配置、通過(guò)引入各種?Spring?Boot?Starter?包可以快速搭建出一個(gè)項(xiàng)目的腳手架,這篇文章主要介紹了SpringCloud-Spring?Boot?Starter使用測(cè)試,需要的朋友可以參考下2022-07-07
Java代碼規(guī)范與質(zhì)量檢測(cè)插件SonarLint的使用
本文主要介紹了Java代碼規(guī)范與質(zhì)量檢測(cè)插件SonarLint的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java Spring MVC獲取請(qǐng)求數(shù)據(jù)詳解操作
Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級(jí) Web 開(kāi)發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì)。由于 Spring MVC 本身就是 Spring 框架的一部分,可以說(shuō)和 Spring 框架是無(wú)縫集成2021-11-11
詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟
本文就在項(xiàng)目中來(lái)集成 UidGenerator這一工程來(lái)作為項(xiàng)目的全局唯一 ID生成器。接下來(lái)通過(guò)實(shí)例代碼給大家詳解詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟,感興趣的朋友一起看看吧2018-10-10
基于Spring5實(shí)現(xiàn)登錄注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了基于Spring5實(shí)現(xiàn)登錄注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09

