1.GZIP压缩

  1. public static byte[] compress(String str, String encoding) {
  2. if (str == null || str.length() == 0) {
  3. return null;
  4. }
  5. ByteArrayOutputStream out = new ByteArrayOutputStream();
  6. GZIPOutputStream gzip;
  7. try {
  8. gzip = new GZIPOutputStream(out);
  9. gzip.write(str.getBytes(encoding));
  10. gzip.close();
  11. } catch ( Exception e) {
  12. e.printStackTrace();
  13. }
  14. return out.toByteArray();
  15. }

2.GZIP解压缩

  1. public static byte[] uncompress(byte[] bytes) {
  2. if (bytes == null || bytes.length == 0) {
  3. return null;
  4. }
  5. ByteArrayOutputStream out = new ByteArrayOutputStream();
  6. ByteArrayInputStream in = new ByteArrayInputStream(bytes);
  7. try {
  8. GZIPInputStream ungzip = new GZIPInputStream(in);
  9. byte[] buffer = new byte[256];
  10. int n;
  11. while ((n = ungzip.read(buffer)) >= 0) {
  12. out.write(buffer, 0, n);
  13. }
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. return out.toByteArray();
  18. }

3.工具代码集合

  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.util.zip.GZIPInputStream;
  5. import java.util.zip.GZIPOutputStream;
  6.  
  7. public class GZIPUtils {
  8. public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
  9. public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";
  10.  
  11. public static byte[] compress(String str, String encoding) {
  12. if (str == null || str.length() == 0) {
  13. return null;
  14. }
  15. ByteArrayOutputStream out = new ByteArrayOutputStream();
  16. GZIPOutputStream gzip;
  17. try {
  18. gzip = new GZIPOutputStream(out);
  19. gzip.write(str.getBytes(encoding));
  20. gzip.close();
  21. } catch ( Exception e) {
  22. e.printStackTrace();
  23. }
  24. return out.toByteArray();
  25. }
  26.  
  27. public static byte[] compress(String str) throws IOException {
  28. return compress(str, GZIP_ENCODE_UTF_8);
  29. }
  30.  
  31. public static byte[] uncompress(byte[] bytes) {
  32. if (bytes == null || bytes.length == 0) {
  33. return null;
  34. }
  35. ByteArrayOutputStream out = new ByteArrayOutputStream();
  36. ByteArrayInputStream in = new ByteArrayInputStream(bytes);
  37. try {
  38. GZIPInputStream ungzip = new GZIPInputStream(in);
  39. byte[] buffer = new byte[256];
  40. int n;
  41. while ((n = ungzip.read(buffer)) >= 0) {
  42. out.write(buffer, 0, n);
  43. }
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. return out.toByteArray();
  48. }
  49.  
  50. public static String uncompressToString(byte[] bytes, String encoding) {
  51. if (bytes == null || bytes.length == 0) {
  52. return null;
  53. }
  54. ByteArrayOutputStream out = new ByteArrayOutputStream();
  55. ByteArrayInputStream in = new ByteArrayInputStream(bytes);
  56. try {
  57. GZIPInputStream ungzip = new GZIPInputStream(in);
  58. byte[] buffer = new byte[256];
  59. int n;
  60. while ((n = ungzip.read(buffer)) >= 0) {
  61. out.write(buffer, 0, n);
  62. }
  63. return out.toString(encoding);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. return null;
  68. }
  69.  
  70. public static String uncompressToString(byte[] bytes) {
  71. return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
  72. }
  73.  
  74. public static void main(String[] args) throws IOException {
  75. String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  76. System.out.println("字符串长度:"+s.length());
  77. System.out.println("压缩后::"+compress(s).length);
  78. System.out.println("解压后:"+uncompress(compress(s)).length);
  79. System.out.println("解压字符串后::"+uncompressToString(compress(s)).length());
  80. }
  81. }

java GZIP压缩与解压缩的更多相关文章

  1. GZIP压缩、解压缩工具类

    GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...

  2. GZip 压缩及解压缩

    /// <summary> /// GZipHelper /// </summary> public class GZipHelper { /// <summary> ...

  3. 对数据进行GZIP压缩或解压缩

    /** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...

  4. GZip压缩与解压缩

    GZIP的压缩与解压缩代码: public static class CompressionHelper { /// <summary> /// Compress the byte[] / ...

  5. java GZIP压缩和解压

    最近碰到了一个按GZIP解压指定的输入流数据,备份下 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream ...

  6. java gzip压缩与解压

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  7. Java实现压缩与解压缩

    import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...

  8. JAVA中压缩与解压缩

    以压缩Zip文件为例.主要是通过ZipOutputStream类实现.解压缩主要使用ZipFile类和ZipInputStream以及ZipEntry类. package main; import j ...

  9. java 版本压缩、解压缩zip

    import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...

随机推荐

  1. win7下出现'telnet' 不是内部或外部命令,也不是可运行的程序或批处理文件的解决方法

    在win7竟然不可能使用telnet命令,使用时会出现“'telnet' 不是内部或外部命令,也不是可运行的程序或批处理文件”,研究了很多,才终于明白WIN7默认是没有安装这个命令. 处理办法: 依次 ...

  2. SpringBoot启动的时候不去校验数据库连接配置是否正确

    spring boot在启动的时候只会检查你是否配置了数据库连接, 而不会检测配置的是否正确 这样会出现的问题是: 只有在你使用数据库的时候才知道配置出错, 我们希望是在程序启动的时候就进行检查, 如 ...

  3. JSP有哪些内置对象

    JSP有哪些内置对象? 1.page:JSP网页本身; 2.request:用户端请求,此请求会包含来自GET/POST请求的参数; 3.session:请求有关的会话; 4.application: ...

  4. fileupload简单使用

    form.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" page ...

  5. linux中fork对打开文件的处理

    1 子进程复制父进程的数据段.BBS段.代码段.堆空间.栈空间和文件描述符 2 对于文件描述符采用共享的方式 后面这个例子可以清晰的看出 #include <sys/types.h> #i ...

  6. BestCoder Round #74 (div.1) 1002Shortest Path(hdoj5636)

    哈哈哈哈,我就知道这道题目再扔给我,我还是不会,就是这么菜,哈哈哈 一开始官方题解就没搞懂-然后就看了一下别人的代码,水水过就算了.今天拿到-GG: 题意: 一开始,有一张原图,有一条长度为n的链. ...

  7. git 保存文件目录

    Ubuntu中git clone就保存在你命令行现在所在的目录里 所以最好推荐 先用cd这个命令去自己想保存的目录 之后再git clone

  8. Swoole和Workerman到底选谁?

    Swoole:面向生产环境的 PHP 异步网络通信引擎 使 PHP 开发人员可以编写高性能的异步并发 TCP.UDP.Unix Socket.HTTP,WebSocket 服务.Swoole 可以广泛 ...

  9. Swift @objcMembers

    使用@objcMembers关键字,将类中的所有方法暴露给Objc (效果等同于为所有方法加上@objc). 示例代码: @objcMembers class MyController: UIView ...

  10. HBuilder mui 报错No 'Access-Control-Allow-Origin' header

    Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' he ...