JAVA中的deflate壓縮實(shí)現(xiàn)方法
在文件的傳輸過(guò)程中,為了使大文件能夠更加方便快速的傳輸,一般采用壓縮的辦法來(lái)對(duì)文件壓縮后再傳輸,JAVA中的java.util.zip包中的Deflater和Inflater類為使用者提供了DEFLATE算法的壓縮功能,以下是自已編寫的壓縮和解壓縮實(shí)現(xiàn),并以壓縮文件內(nèi)容為例說(shuō)明,其中涉及的具體方法可查看JDK的API了解說(shuō)明。
/** * * @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技術(shù)長(zhǎng)久占居主要地位的12個(gè)原因
這篇文章主要為大家詳細(xì)介紹了12個(gè)Java長(zhǎng)久占居主要地位的原因,感興趣的小伙伴們可以參考一下2016-07-07聊聊Spring Boot 如何集成多個(gè) Kafka
這篇文章主要介紹了Spring Boot 集成多個(gè) Kafka的相關(guān)資料,包括配置文件,生成者和消費(fèi)者配置過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10Java調(diào)用商品詳情API的項(xiàng)目實(shí)踐
在現(xiàn)代電子商務(wù)網(wǎng)站中,商品詳情API是一個(gè)重要的組件,本文就來(lái)介紹一下Java調(diào)用商品詳情API的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法
這篇文章主要介紹了java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法,涉及JDBC操作數(shù)據(jù)庫(kù)的連接、創(chuàng)建表、添加數(shù)據(jù)、查詢等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08詳解SpringCloud Ribbon 負(fù)載均衡通過(guò)服務(wù)器名無(wú)法連接的神坑
這篇文章主要介紹了詳解SpringCloud Ribbon 負(fù)載均衡通過(guò)服務(wù)器名無(wú)法連接的神坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06JAVA文件讀寫例題實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了JAVA文件讀寫例題實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Spring實(shí)戰(zhàn)之使用ClassPathResource加載xml資源示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用ClassPathResource加載xml資源,結(jié)合實(shí)例形式分析了Spring使用ClassPathResource加載xml資源的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2019-12-12