亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Java壓縮技術 GZIP——Java原生實現

系統 2123 0

相關鏈接:?
Java壓縮技術(一) ZLib ?
Java壓縮技術(二) ZIP壓縮——Java原生實現 ?
Java壓縮技術(三) ZIP解壓縮——Java原生實現 ?
Java壓縮技術(四) GZIP——Java原生實現 ?
Java壓縮技術(五) GZIP相關——瀏覽器解析 ?
Java壓縮技術(六) BZIP2——Commons實現 ?
Java壓縮技術(七) TAR——Commons實現 ?

GZIP常常用在linxu環境下,是一種非常簡單的壓縮算法。在Java實現API中,它僅僅包含兩個實現類:GZIPInputStream和GZIPOutputStream。?
GZIPOutputStream類用于壓縮 ?
GZIPInputStream類用于解壓縮 ?

先說壓縮實現,GZIPOutputStream只有一個方法用于壓縮,就是帶定長的write方法。簡單調用如下文所示:?

Java代碼?? 收藏代碼
  1. /** ?
  2. ?*?數據壓縮 ?
  3. ?*? ?
  4. ?*?@param?is ?
  5. ?*?@param?os ?
  6. ?*?@throws?Exception ?
  7. ?*/ ??
  8. public ? static ? void ?compress(InputStream?is,?OutputStream?os)??
  9. ???????? throws ?Exception?{??
  10. ??
  11. ????GZIPOutputStream?gos?=? new ?GZIPOutputStream(os);??
  12. ??
  13. ???? int ?count;??
  14. ???? byte ?data[]?=? new ? byte [BUFFER];??
  15. ???? while ?((count?=?is.read(data,? 0 ,?BUFFER))?!=?- 1 )?{??
  16. ????????gos.write(data,? 0 ,?count);??
  17. ????}??
  18. ??
  19. ????gos.finish();??
  20. ??
  21. ????gos.flush();??
  22. ????gos.close();??
  23. }??


記得完成操作后,調用finish方法和flush方法! ?

核心的壓縮實現就這么多! ?

對于解壓縮,GZIPInputStream也對應GZIPOutputStream提供了一個帶定長的read方法。簡單調用如下文所示:?

Java代碼?? 收藏代碼
  1. /** ?
  2. ?*?數據解壓縮 ?
  3. ?*? ?
  4. ?*?@param?is ?
  5. ?*?@param?os ?
  6. ?*?@throws?Exception ?
  7. ?*/ ??
  8. public ? static ? void ?decompress(InputStream?is,?OutputStream?os)??
  9. ???????? throws ?Exception?{??
  10. ??
  11. ????GZIPInputStream?gis?=? new ?GZIPInputStream(is);??
  12. ??
  13. ???? int ?count;??
  14. ???? byte ?data[]?=? new ? byte [BUFFER];??
  15. ???? while ?((count?=?gis.read(data,? 0 ,?BUFFER))?!=?- 1 )?{??
  16. ????????os.write(data,? 0 ,?count);??
  17. ????}??
  18. ??
  19. ????gis.close();??
  20. }??



就這么簡單! ?核心內容完畢!?

順便補充一下,在liunx下操作gzip命令?

gzip file 用于壓縮,如 gzip a.txt 將得到文件 a.txt.gz 同時刪除文件a.txt! ?
gzip -d file.gz 用于解壓縮,如 gzip -d a.txt.gz 將得到文件 a.txt 同時刪除文件a.txt.gz! ?

根據這些特性,我補充了相應的文件操作實現,詳見下文! ?

完整實現:?

Java代碼?? 收藏代碼
  1. /** ?
  2. ?*?2010-4-13 ?
  3. ?*/ ??
  4. package ?org.zlex.commons.io;??
  5. ??
  6. import ?java.io.ByteArrayInputStream;??
  7. import ?java.io.ByteArrayOutputStream;??
  8. import ?java.io.File;??
  9. import ?java.io.FileInputStream;??
  10. import ?java.io.FileOutputStream;??
  11. import ?java.io.InputStream;??
  12. import ?java.io.OutputStream;??
  13. import ?java.util.zip.GZIPInputStream;??
  14. import ?java.util.zip.GZIPOutputStream;??
  15. ??
  16. /** ?
  17. ?*?GZIP工具 ?
  18. ?*? ?
  19. ?*?@author?<a?href="mailto:zlex.dongliang@gmail.com">梁棟</a> ?
  20. ?*?@since?1.0 ?
  21. ?*/ ??
  22. public ? abstract ? class ?GZipUtils?{??
  23. ??
  24. ???? public ? static ? final ? int ?BUFFER?=? 1024 ;??
  25. ???? public ? static ? final ?String?EXT?=? ".gz" ;??
  26. ??
  27. ???? /** ?
  28. ?????*?數據壓縮 ?
  29. ?????*? ?
  30. ?????*?@param?data ?
  31. ?????*?@return ?
  32. ?????*?@throws?Exception ?
  33. ?????*/ ??
  34. ???? public ? static ? byte []?compress( byte []?data)? throws ?Exception?{??
  35. ????????ByteArrayInputStream?bais?=? new ?ByteArrayInputStream(data);??
  36. ????????ByteArrayOutputStream?baos?=? new ?ByteArrayOutputStream();??
  37. ??
  38. ???????? //?壓縮 ??
  39. ????????compress(bais,?baos);??
  40. ??
  41. ???????? byte []?output?=?baos.toByteArray();??
  42. ??
  43. ????????baos.flush();??
  44. ????????baos.close();??
  45. ??
  46. ????????bais.close();??
  47. ??
  48. ???????? return ?output;??
  49. ????}??
  50. ??
  51. ???? /** ?
  52. ?????*?文件壓縮 ?
  53. ?????*? ?
  54. ?????*?@param?file ?
  55. ?????*?@throws?Exception ?
  56. ?????*/ ??
  57. ???? public ? static ? void ?compress(File?file)? throws ?Exception?{??
  58. ????????compress(file,? true );??
  59. ????}??
  60. ??
  61. ???? /** ?
  62. ?????*?文件壓縮 ?
  63. ?????*? ?
  64. ?????*?@param?file ?
  65. ?????*?@param?delete ?
  66. ?????*????????????是否刪除原始文件 ?
  67. ?????*?@throws?Exception ?
  68. ?????*/ ??
  69. ???? public ? static ? void ?compress(File?file,? boolean ?delete)? throws ?Exception?{??
  70. ????????FileInputStream?fis?=? new ?FileInputStream(file);??
  71. ????????FileOutputStream?fos?=? new ?FileOutputStream(file.getPath()?+?EXT);??
  72. ??
  73. ????????compress(fis,?fos);??
  74. ??
  75. ????????fis.close();??
  76. ????????fos.flush();??
  77. ????????fos.close();??
  78. ??
  79. ???????? if ?(delete)?{??
  80. ????????????file.delete();??
  81. ????????}??
  82. ????}??
  83. ??
  84. ???? /** ?
  85. ?????*?數據壓縮 ?
  86. ?????*? ?
  87. ?????*?@param?is ?
  88. ?????*?@param?os ?
  89. ?????*?@throws?Exception ?
  90. ?????*/ ??
  91. ???? public ? static ? void ?compress(InputStream?is,?OutputStream?os)??
  92. ???????????? throws ?Exception?{??
  93. ??
  94. ????????GZIPOutputStream?gos?=? new ?GZIPOutputStream(os);??
  95. ??
  96. ???????? int ?count;??
  97. ???????? byte ?data[]?=? new ? byte [BUFFER];??
  98. ???????? while ?((count?=?is.read(data,? 0 ,?BUFFER))?!=?- 1 )?{??
  99. ????????????gos.write(data,? 0 ,?count);??
  100. ????????}??
  101. ??
  102. ????????gos.finish();??
  103. ??
  104. ????????gos.flush();??
  105. ????????gos.close();??
  106. ????}??
  107. ??
  108. ???? /** ?
  109. ?????*?文件壓縮 ?
  110. ?????*? ?
  111. ?????*?@param?path ?
  112. ?????*?@throws?Exception ?
  113. ?????*/ ??
  114. ???? public ? static ? void ?compress(String?path)? throws ?Exception?{??
  115. ????????compress(path,? true );??
  116. ????}??
  117. ??
  118. ???? /** ?
  119. ?????*?文件壓縮 ?
  120. ?????*? ?
  121. ?????*?@param?path ?
  122. ?????*?@param?delete ?
  123. ?????*????????????是否刪除原始文件 ?
  124. ?????*?@throws?Exception ?
  125. ?????*/ ??
  126. ???? public ? static ? void ?compress(String?path,? boolean ?delete)? throws ?Exception?{??
  127. ????????File?file?=? new ?File(path);??
  128. ????????compress(file,?delete);??
  129. ????}??
  130. ??
  131. ???? /** ?
  132. ?????*?數據解壓縮 ?
  133. ?????*? ?
  134. ?????*?@param?data ?
  135. ?????*?@return ?
  136. ?????*?@throws?Exception ?
  137. ?????*/ ??
  138. ???? public ? static ? byte []?decompress( byte []?data)? throws ?Exception?{??
  139. ????????ByteArrayInputStream?bais?=? new ?ByteArrayInputStream(data);??
  140. ????????ByteArrayOutputStream?baos?=? new ?ByteArrayOutputStream();??
  141. ??
  142. ???????? //?解壓縮 ??
  143. ??
  144. ????????decompress(bais,?baos);??
  145. ??
  146. ????????data?=?baos.toByteArray();??
  147. ??
  148. ????????baos.flush();??
  149. ????????baos.close();??
  150. ??
  151. ????????bais.close();??
  152. ??
  153. ???????? return ?data;??
  154. ????}??
  155. ??
  156. ???? /** ?
  157. ?????*?文件解壓縮 ?
  158. ?????*? ?
  159. ?????*?@param?file ?
  160. ?????*?@throws?Exception ?
  161. ?????*/ ??
  162. ???? public ? static ? void ?decompress(File?file)? throws ?Exception?{??
  163. ????????decompress(file,? true );??
  164. ????}??
  165. ??
  166. ???? /** ?
  167. ?????*?文件解壓縮 ?
  168. ?????*? ?
  169. ?????*?@param?file ?
  170. ?????*?@param?delete ?
  171. ?????*????????????是否刪除原始文件 ?
  172. ?????*?@throws?Exception ?
  173. ?????*/ ??
  174. ???? public ? static ? void ?decompress(File?file,? boolean ?delete)? throws ?Exception?{??
  175. ????????FileInputStream?fis?=? new ?FileInputStream(file);??
  176. ????????FileOutputStream?fos?=? new ?FileOutputStream(file.getPath().replace(EXT,??
  177. ???????????????? "" ));??
  178. ????????decompress(fis,?fos);??
  179. ????????fis.close();??
  180. ????????fos.flush();??
  181. ????????fos.close();??
  182. ??
  183. ???????? if ?(delete)?{??
  184. ????????????file.delete();??
  185. ????????}??
  186. ????}??
  187. ??
  188. ???? /** ?
  189. ?????*?數據解壓縮 ?
  190. ?????*? ?
  191. ?????*?@param?is ?
  192. ?????*?@param?os ?
  193. ?????*?@throws?Exception ?
  194. ?????*/ ??
  195. ???? public ? static ? void ?decompress(InputStream?is,?OutputStream?os)??
  196. ???????????? throws ?Exception?{??
  197. ??
  198. ????????GZIPInputStream?gis?=? new ?GZIPInputStream(is);??
  199. ??
  200. ???????? int ?count;??
  201. ???????? byte ?data[]?=? new ? byte [BUFFER];??
  202. ???????? while ?((count?=?gis.read(data,? 0 ,?BUFFER))?!=?- 1 )?{??
  203. ????????????os.write(data,? 0 ,?count);??
  204. ????????}??
  205. ??
  206. ????????gis.close();??
  207. ????}??
  208. ??
  209. ???? /** ?
  210. ?????*?文件解壓縮 ?
  211. ?????*? ?
  212. ?????*?@param?path ?
  213. ?????*?@throws?Exception ?
  214. ?????*/ ??
  215. ???? public ? static ? void ?decompress(String?path)? throws ?Exception?{??
  216. ????????decompress(path,? true );??
  217. ????}??
  218. ??
  219. ???? /** ?
  220. ?????*?文件解壓縮 ?
  221. ?????*? ?
  222. ?????*?@param?path ?
  223. ?????*?@param?delete ?
  224. ?????*????????????是否刪除原始文件 ?
  225. ?????*?@throws?Exception ?
  226. ?????*/ ??
  227. ???? public ? static ? void ?decompress(String?path,? boolean ?delete)? throws ?Exception?{??
  228. ????????File?file?=? new ?File(path);??
  229. ????????decompress(file,?delete);??
  230. ????}??
  231. ??
  232. }??



羅嗦了半天,到底行不行??
來個測試用例,測試用例如下所示:?

Java代碼?? 收藏代碼
  1. /** ?
  2. ?*?2010-4-13 ?
  3. ?*/ ??
  4. package ?org.zlex.commons.compress.compress;??
  5. ??
  6. import ? static ?org.junit.Assert.assertEquals;??
  7. ??
  8. import ?java.io.DataInputStream;??
  9. import ?java.io.File;??
  10. import ?java.io.FileInputStream;??
  11. import ?java.io.FileOutputStream;??
  12. ??
  13. import ?org.junit.Test;??
  14. ??
  15. /** ?
  16. ?*?@author?<a?href="mailto:zlex.dongliang@gmail.com">梁棟</a> ?
  17. ?*?@since?1.0 ?
  18. ?*/ ??
  19. public ? class ?GZipUtilsTest?{??
  20. ??
  21. ???? private ?String?inputStr?=? "zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org" ;??
  22. ??
  23. ???? @Test ??
  24. ???? public ? final ? void ?testDataCompress()? throws ?Exception?{??
  25. ??
  26. ????????System.err.println( "原文:\t" ?+?inputStr);??
  27. ??
  28. ???????? byte []?input?=?inputStr.getBytes();??
  29. ????????System.err.println( "長度:\t" ?+?input.length);??
  30. ??
  31. ???????? byte []?data?=?GZipUtils.compress(input);??
  32. ????????System.err.println( "壓縮后:\t" );??
  33. ????????System.err.println( "長度:\t" ?+?data.length);??
  34. ??
  35. ???????? byte []?output?=?GZipUtils.decompress(data);??
  36. ????????String?outputStr?=? new ?String(output);??
  37. ????????System.err.println( "解壓縮后:\t" ?+?outputStr);??
  38. ????????System.err.println( "長度:\t" ?+?output.length);??
  39. ??
  40. ????????assertEquals(inputStr,?outputStr);??
  41. ??
  42. ????}??
  43. ??
  44. ???? @Test ??
  45. ???? public ? final ? void ?testFileCompress()? throws ?Exception?{??
  46. ??
  47. ????????FileOutputStream?fos?=? new ?FileOutputStream( "d:/f.txt" );??
  48. ??
  49. ????????fos.write(inputStr.getBytes());??
  50. ????????fos.flush();??
  51. ????????fos.close();??
  52. ??
  53. ????????GZipUtils.compress( "d:/f.txt" ,? false );??
  54. ??
  55. ????????GZipUtils.decompress( "d:/f.txt.gz" ,? false );??
  56. ??
  57. ????????File?file?=? new ?File( "d:/f.txt" );??
  58. ??
  59. ????????FileInputStream?fis?=? new ?FileInputStream(file);??
  60. ??
  61. ????????DataInputStream?dis?=? new ?DataInputStream(fis);??
  62. ??
  63. ???????? byte []?data?=? new ? byte [( int )?file.length()];??
  64. ????????dis.readFully(data);??
  65. ??
  66. ????????fis.close();??
  67. ??
  68. ????????String?outputStr?=? new ?String(data);??
  69. ????????assertEquals(inputStr,?outputStr);??
  70. ????}??
  71. }??


結果如何??
先看testDataCompress()方法控制臺輸出結果。?
控制臺輸出如下:?

引用

原文: zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org?
長度: 52?
壓縮后:
長度: 45?
解壓縮后: zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org?
長度: 52?


這里使用英文字符做測試,當輸入字符串的字節數大于50左右時,壓縮效果明顯;如果這里使用中文壓縮,可能當壓縮上千字節時方能體現出壓縮效果!?
對于文件操作,朋友們可以自行實驗,我代碼里的實現是按照gzip命令來的!?
舉例來說:?
壓縮時,將文件a.txt壓縮為a.txt.gz,同時刪除文件a.txt。?
解壓縮時,將文件a.txt.gz解壓縮為a.txt,同時刪除文件a.txt.gz。?

注意執行testFileCompress方法,查看產生的文件! ?你大可以放到linux上去做驗證! ?

commons也提供了GZIP算法的實現,甚至更多種壓縮算法(tar、bzip2等)的實現,有機會我將繼續整理! ?

相關鏈接:?
Java壓縮技術(一) ZLib ?
Java壓縮技術(二) ZIP壓縮——Java原生實現 ?
Java壓縮技術(三) ZIP解壓縮——Java原生實現 ?
Java壓縮技術(四) GZIP——Java原生實現 ?
Java壓縮技術(五) GZIP相關——瀏覽器解析 ?
Java壓縮技術(六) BZIP2——Commons實現 ?
Java壓縮技術(七) TAR——Commons實現

Java壓縮技術 GZIP——Java原生實現


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久亚洲国产中v天仙www | 日韩欧美视频一区二区在线观看 | 99国产精品热久久久久久夜夜嗨 | 亚洲国产高清人在线 | 免费观看日本a毛片 | 国内精品一区二区 | 亚洲精品一线观看 | 福利一区视频 | 狠狠操狠狠干 | 国产小视频在线观看免费 | 成人久久影院 | 女性毛片 | 99久久九九| 久久久99视频 | 久久国产免费福利资源网站 | 亚洲天堂二区 | 西西大胆实体啪啪色哟哟 | 欧美kkk4444在线观看 | 人人爽天天爽 | jizz中国jizz女人 | www.天天操.com | 欧美在线香蕉在线现视频 | 日本高清一道本 | 欧美一级毛片欧美毛片视频 | 欧美一级毛片高清免费观看 | videoxxoo欧美老师 | 久久免费国产视频 | 欧美久久综合网 | 亚洲欧美日韩一区超高清 | 中国男女全黄大片一级 | 日本aa视频 | 91久久精品日日躁夜夜躁欧美 | 91资源| 久草精品视频在线播放 | 亚洲日本中文字幕在线2022 | 麻豆国产精品免费视频 | 一级特级aa欧美毛片 | 国产美女久久久久 | 中文字幕在线观看2023 | www中文字幕| 久久久中文字幕 |