JAVA中的deflate壓縮實(shí)現(xiàn)方法
在文件的傳輸過程中,為了使大文件能夠更加方便快速的傳輸,一般采用壓縮的辦法來(lái)對(duì)文件壓縮后再傳輸,JAVA中的java.util.zip包中的Deflater和Inflater類為使用者提供了DEFLATE算法的壓縮功能,以下是自已編寫的壓縮和解壓縮實(shí)現(xiàn),并以壓縮文件內(nèi)容為例說明,其中涉及的具體方法可查看JDK的API了解說明。
/**
*
* @param inputByte
* 待解壓縮的字節(jié)數(shù)組
* @return 解壓縮后的字節(jié)數(shù)組
* @throws IOException
*/
public static byte[] uncompress(byte[] inputByte) throws IOException {
int len = 0;
Inflater infl = new Inflater();
infl.setInput(inputByte);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] outByte = new byte[1024];
try {
while (!infl.finished()) {
// 解壓縮并將解壓縮后的內(nèi)容輸出到字節(jié)輸出流bos中
len = infl.inflate(outByte);
if (len == 0) {
break;
}
bos.write(outByte, 0, len);
}
infl.end();
} catch (Exception e) {
//
} finally {
bos.close();
}
return bos.toByteArray();
}
/**
* 壓縮.
*
* @param inputByte
* 待壓縮的字節(jié)數(shù)組
* @return 壓縮后的數(shù)據(jù)
* @throws IOException
*/
public static byte[] compress(byte[] inputByte) throws IOException {
int len = 0;
Deflater defl = new Deflater();
defl.setInput(inputByte);
defl.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] outputByte = new byte[1024];
try {
while (!defl.finished()) {
// 壓縮并將壓縮后的內(nèi)容輸出到字節(jié)輸出流bos中
len = defl.deflate(outputByte);
bos.write(outputByte, 0, len);
}
defl.end();
} finally {
bos.close();
}
return bos.toByteArray();
}
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("D:\\testdeflate.txt");
int len = fis.available();
byte[] b = new byte[len];
fis.read(b);
byte[] bd = compress(b);
// 為了壓縮后的內(nèi)容能夠在網(wǎng)絡(luò)上傳輸,一般采用Base64編碼
String encodestr = Base64.encodeBase64String(bd);
byte[] bi = uncompress(Base64.decodeBase64(encodestr));
FileOutputStream fos = new FileOutputStream("D:\\testinflate.txt");
fos.write(bi);
fos.flush();
fos.close();
fis.close();
} catch (Exception e) {
//
}
}
以上這篇JAVA中的deflate壓縮實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程的start方法回調(diào)run方法的操作技巧
面試過程中經(jīng)常會(huì)被面試官問到為什么我們調(diào)用start()方法時(shí)會(huì)執(zhí)行run()方法,為什么不能直接調(diào)用run()方法,問的一頭霧水,今天小編給大家介紹下Java線程的start方法回調(diào)run方法的操作技巧,需要的朋友參考下吧2017-11-11
解決IDEA右鍵沒有創(chuàng)建新的package選項(xiàng)的情況
這篇文章主要介紹了解決IDEA右鍵沒有創(chuàng)建新的package選項(xiàng)的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-02-02
springboot中@Async默認(rèn)線程池導(dǎo)致OOM問題
這篇文章主要介紹了springboot中@Async默認(rèn)線程池導(dǎo)致OOM問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
MyBatis Plus整合Redis實(shí)現(xiàn)分布式二級(jí)緩存的問題
Mybatis內(nèi)置的二級(jí)緩存在分布式環(huán)境下存在分布式問題,無(wú)法使用,但是我們可以整合Redis來(lái)實(shí)現(xiàn)分布式的二級(jí)緩存,這篇文章給大家介紹MyBatis Plus整合Redis實(shí)現(xiàn)分布式二級(jí)緩存,感興趣的朋友跟隨小編一起看看吧2023-11-11
sin(x)如何求解的java代碼實(shí)現(xiàn)方法
這篇文章主要為大家介紹了sin(x)如何求解的java代碼實(shí)現(xiàn)方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Idea配置Maven阿里云鏡像加速的實(shí)現(xiàn)
這篇文章主要介紹了Idea配置Maven阿里云鏡像加速的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

