【转载】java对byte数组解压缩(zip,gzip,bzip2,jzlib)

1.首先在项目中导入bzip2.jar;(下载地址:http://download.csdn.net/detail/majian_1987/5997697

2.下载jzlib的源代码zip包,解压,然后把com.jcraft.jzlib.*的所有java文件都拷贝到项目中(连包带java文件一起拷贝),下载地址:http://download.csdn.net/detail/majian_1987/5997737

3.然后编写如下代码:


 
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.DataOutputStream;  
  4. import java.io.IOException;  
  5. import java.util.zip.GZIPInputStream;  
  6. import java.util.zip.GZIPOutputStream;  
  7. import java.util.zip.ZipEntry;  
  8. import java.util.zip.ZipInputStream;  
  9. import java.util.zip.ZipOutputStream;  
  10. import org.apache.tools.bzip2.CBZip2InputStream;  
  11. import org.apache.tools.bzip2.CBZip2OutputStream;  
  12. import com.jcraft.jzlib.JZlib;  
  13. import com.jcraft.jzlib.ZInputStream;  
  14. import com.jcraft.jzlib.ZOutputStream;  
  15. public class Test {  
  16. /*** 
  17. * 压缩GZip 
  18. *  
  19. * @param data 
  20. * @return 
  21. */  
  22. public static byte[] gZip(byte[] data) {  
  23. byte[] b = null;  
  24. try {  
  25. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  26. GZIPOutputStream gzip = new GZIPOutputStream(bos);  
  27. gzip.write(data);  
  28. gzip.finish();  
  29. gzip.close();  
  30. b = bos.toByteArray();  
  31. bos.close();  
  32. catch (Exception ex) {  
  33. ex.printStackTrace();  
  34. }  
  35. return b;  
  36. }  
  37. /*** 
  38. * 解压GZip 
  39. *  
  40. * @param data 
  41. * @return 
  42. */  
  43. public static byte[] unGZip(byte[] data) {  
  44. byte[] b = null;  
  45. try {  
  46. ByteArrayInputStream bis = new ByteArrayInputStream(data);  
  47. GZIPInputStream gzip = new GZIPInputStream(bis);  
  48. byte[] buf = new byte[1024];  
  49. int num = -1;  
  50. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  51. while ((num = gzip.read(buf, 0, buf.length)) != -1) {  
  52. baos.write(buf, 0, num);  
  53. }  
  54. b = baos.toByteArray();  
  55. baos.flush();  
  56. baos.close();  
  57. gzip.close();  
  58. bis.close();  
  59. catch (Exception ex) {  
  60. ex.printStackTrace();  
  61. }  
  62. return b;  
  63. }  
  64. /*** 
  65. * 压缩Zip 
  66. *  
  67. * @param data 
  68. * @return 
  69. */  
  70. public static byte[] zip(byte[] data) {  
  71. byte[] b = null;  
  72. try {  
  73. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  74. ZipOutputStream zip = new ZipOutputStream(bos);  
  75. ZipEntry entry = new ZipEntry("zip");  
  76. entry.setSize(data.length);  
  77. zip.putNextEntry(entry);  
  78. zip.write(data);  
  79. zip.closeEntry();  
  80. zip.close();  
  81. b = bos.toByteArray();  
  82. bos.close();  
  83. catch (Exception ex) {  
  84. ex.printStackTrace();  
  85. }  
  86. return b;  
  87. }  
  88. /*** 
  89. * 解压Zip 
  90. *  
  91. * @param data 
  92. * @return 
  93. */  
  94. public static byte[] unZip(byte[] data) {  
  95. byte[] b = null;  
  96. try {  
  97. ByteArrayInputStream bis = new ByteArrayInputStream(data);  
  98. ZipInputStream zip = new ZipInputStream(bis);  
  99. while (zip.getNextEntry() != null) {  
  100. byte[] buf = new byte[1024];  
  101. int num = -1;  
  102. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  103. while ((num = zip.read(buf, 0, buf.length)) != -1) {  
  104. baos.write(buf, 0, num);  
  105. }  
  106. b = baos.toByteArray();  
  107. baos.flush();  
  108. baos.close();  
  109. }  
  110. zip.close();  
  111. bis.close();  
  112. catch (Exception ex) {  
  113. ex.printStackTrace();  
  114. }  
  115. return b;  
  116. }  
  117. /*** 
  118. * 压缩BZip2 
  119. *  
  120. * @param data 
  121. * @return 
  122. */  
  123. public static byte[] bZip2(byte[] data) {  
  124. byte[] b = null;  
  125. try {  
  126. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  127. CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos);  
  128. bzip2.write(data);  
  129. bzip2.flush();  
  130. bzip2.close();  
  131. b = bos.toByteArray();  
  132. bos.close();  
  133. catch (Exception ex) {  
  134. ex.printStackTrace();  
  135. }  
  136. return b;  
  137. }  
  138. /*** 
  139. * 解压BZip2 
  140. *  
  141. * @param data 
  142. * @return 
  143. */  
  144. public static byte[] unBZip2(byte[] data) {  
  145. byte[] b = null;  
  146. try {  
  147. ByteArrayInputStream bis = new ByteArrayInputStream(data);  
  148. CBZip2InputStream bzip2 = new CBZip2InputStream(bis);  
  149. byte[] buf = new byte[1024];  
  150. int num = -1;  
  151. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  152. while ((num = bzip2.read(buf, 0, buf.length)) != -1) {  
  153. baos.write(buf, 0, num);  
  154. }  
  155. b = baos.toByteArray();  
  156. baos.flush();  
  157. baos.close();  
  158. bzip2.close();  
  159. bis.close();  
  160. catch (Exception ex) {  
  161. ex.printStackTrace();  
  162. }  
  163. return b;  
  164. }  
  165. /** 
  166. * 把字节数组转换成16进制字符串 
  167. *  
  168. * @param bArray 
  169. * @return 
  170. */  
  171. public static String bytesToHexString(byte[] bArray) {  
  172. StringBuffer sb = new StringBuffer(bArray.length);  
  173. String sTemp;  
  174. for (int i = 0; i < bArray.length; i++) {  
  175. sTemp = Integer.toHexString(0xFF & bArray[i]);  
  176. if (sTemp.length() < 2)  
  177. sb.append(0);  
  178. sb.append(sTemp.toUpperCase());  
  179. }  
  180. return sb.toString();  
  181. }  
  182. /** 
  183. * 压缩数据 
  184. *  
  185. * @param object 
  186. * @return 
  187. * @throws IOException 
  188. */  
  189. public static byte[] jzlib(byte[] object) {  
  190. byte[] data = null;  
  191. try {  
  192. ByteArrayOutputStream out = new ByteArrayOutputStream();  
  193. ZOutputStream zOut = new ZOutputStream(out,  
  194. JZlib.Z_DEFAULT_COMPRESSION);  
  195. DataOutputStream objOut = new DataOutputStream(zOut);  
  196. objOut.write(object);  
  197. objOut.flush();  
  198. zOut.close();  
  199. data = out.toByteArray();  
  200. out.close();  
  201. catch (IOException e) {  
  202. e.printStackTrace();  
  203. }  
  204. return data;  
  205. }  
  206. /** 
  207. * 解压被压缩的数据 
  208. *  
  209. * @param object 
  210. * @return 
  211. * @throws IOException 
  212. */  
  213. public static byte[] unjzlib(byte[] object) {  
  214. byte[] data = null;  
  215. try {  
  216. ByteArrayInputStream in = new ByteArrayInputStream(object);  
  217. ZInputStream zIn = new ZInputStream(in);  
  218. byte[] buf = new byte[1024];  
  219. int num = -1;  
  220. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  221. while ((num = zIn.read(buf, 0, buf.length)) != -1) {  
  222. baos.write(buf, 0, num);  
  223. }  
  224. data = baos.toByteArray();  
  225. baos.flush();  
  226. baos.close();  
  227. zIn.close();  
  228. in.close();  
  229.   
  230. catch (IOException e) {  
  231. e.printStackTrace();  
  232. }  
  233. return data;  
  234. }  
  235. public static void main(String[] args) {  
  236. String s = "this is a test";  
  237.   
  238. byte[] b1 = zip(s.getBytes());  
  239. System.out.println("zip:" + bytesToHexString(b1));  
  240. byte[] b2 = unZip(b1);  
  241. System.out.println("unZip:" + new String(b2));  
  242. byte[] b3 = bZip2(s.getBytes());  
  243. System.out.println("bZip2:" + bytesToHexString(b3));  
  244. byte[] b4 = unBZip2(b3);  
  245. System.out.println("unBZip2:" + new String(b4));  
  246. byte[] b5 = gZip(s.getBytes());  
  247. System.out.println("bZip2:" + bytesToHexString(b5));  
  248. byte[] b6 = unGZip(b5);  
  249. System.out.println("unBZip2:" + new String(b6));  
  250. byte[] b7 = jzlib(s.getBytes());  
  251. System.out.println("jzlib:" + bytesToHexString(b7));  
  252. byte[] b8 = unjzlib(b7);  
  253. System.out.println("unjzlib:" + new String(b8));  
  254. }  
  255. }  

 

 

 

//输出结果

zip:504B03041400080008008B82CC3A000000000000000000000000030000007A69702BC9C82C5600A2448592D4E21200504B0708EAE71E0D0E0000000E000000504B010214001400080008008B82CC3AEAE71E0D0E0000000E0000000300000000000000000000000000000000007A6970504B05060000000001000100310000003F0000000000
unZip:this is a test
bZip2:6839314159265359F81D428D0000061180400022600C00200021A34CD42180A9B16D11D0F177245385090F81D428D0
unBZip2:this is a test
bZip2:1F8B08000000000000002BC9C82C5600A2448592D4E21200EAE71E0D0E000000
unBZip2:this is a test
jzlib:789C2BC9C82C5600A2448592D4E2120026330516
unjzlib:this is a test

posted @ 2015-04-15 22:14  Genji_  阅读(1764)  评论(0编辑  收藏  举报