免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4458 | 回复: 1
打印 上一主题 下一主题

数据的压缩和解压 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-22 17:16 |只看该作者 |倒序浏览
再也不用担心数据压缩和解压的问题了。
支持 java c# dephi 调用

代码:
  1. package com.xulin.utils;

  2. import java.io.ByteArrayOutputStream;
  3. import java.util.zip.DeflaterOutputStream;
  4. import java.util.zip.InflaterOutputStream;

  5. import org.apache.commons.codec.binary.Base64;

  6. /**
  7. * 支持Delphi C# java三方压缩
  8. *
  9. * @author xulin
  10. *
  11. */
  12. public class Zip {

  13.     /**
  14.      * 压缩数据以UTF-8编码
  15.      *
  16.      * @param data
  17.      * @return 编码后的字符串
  18.      */
  19.     public static String compressData(String data) {
  20.         try {
  21.             ByteArrayOutputStream bos = new ByteArrayOutputStream();
  22.             DeflaterOutputStream zos = new DeflaterOutputStream(bos);
  23.             zos.write(data.getBytes("UTF-8"));
  24.             zos.close();
  25.             return new String(getenBASE64inCodec(bos.toByteArray()));
  26.         } catch (Exception ex) {
  27.             ex.printStackTrace();
  28.             return "ZIP_ERR";
  29.         }
  30.     }

  31.     /**
  32.      * 解压缩数据以UTF-8编码
  33.      *
  34.      * @param encdata
  35.      * @return 解码后的字符串
  36.      */
  37.     public static String decompressData(String encdata) {
  38.         try {
  39.             ByteArrayOutputStream bos = new ByteArrayOutputStream();
  40.             InflaterOutputStream zos = new InflaterOutputStream(bos);
  41.             zos.write(getdeBASE64inCodec(encdata));
  42.             zos.close();
  43.             return new String(bos.toByteArray(), "UTF-8");
  44.         } catch (Exception ex) {
  45.             ex.printStackTrace();
  46.             return "UNZIP_ERR";
  47.         }
  48.     }

  49.     /**
  50.      * 调用apache的编码方法
  51.      */
  52.     private static String getenBASE64inCodec(byte[] b) {
  53.         if (b == null)
  54.             return null;
  55.         return new String((new Base64()).encode(b));
  56.     }

  57.     /**
  58.      * 调用apache的解码方法
  59.      */
  60.     private static byte[] getdeBASE64inCodec(String s) {
  61.         if (s == null)
  62.             return null;
  63.         return new Base64().decode(s.getBytes());
  64.     }
  65. }
复制代码
Zip.java
  1. package com.xulin.utils;

  2. import java.io.ByteArrayOutputStream;
  3. import java.util.zip.DeflaterOutputStream;
  4. import java.util.zip.InflaterOutputStream;

  5. import org.apache.commons.codec.binary.Base64;

  6. /**
  7. * 支持Delphi C# java三方压缩
  8. *
  9. * @author xulin
  10. *
  11. */
  12. public class Zip {

  13.     /**
  14.      * 压缩数据以UTF-8编码
  15.      *
  16.      * @param data
  17.      * @return 编码后的字符串
  18.      */
  19.     public static String compressData(String data) {
  20.         try {
  21.             ByteArrayOutputStream bos = new ByteArrayOutputStream();
  22.             DeflaterOutputStream zos = new DeflaterOutputStream(bos);
  23.             zos.write(data.getBytes("UTF-8"));
  24.             zos.close();
  25.             return new String(getenBASE64inCodec(bos.toByteArray()));
  26.         } catch (Exception ex) {
  27.             ex.printStackTrace();
  28.             return "ZIP_ERR";
  29.         }
  30.     }

  31.     /**
  32.      * 解压缩数据以UTF-8编码
  33.      *
  34.      * @param encdata
  35.      * @return 解码后的字符串
  36.      */
  37.     public static String decompressData(String encdata) {
  38.         try {
  39.             ByteArrayOutputStream bos = new ByteArrayOutputStream();
  40.             InflaterOutputStream zos = new InflaterOutputStream(bos);
  41.             zos.write(getdeBASE64inCodec(encdata));
  42.             zos.close();
  43.             return new String(bos.toByteArray(), "UTF-8");
  44.         } catch (Exception ex) {
  45.             ex.printStackTrace();
  46.             return "UNZIP_ERR";
  47.         }
  48.     }

  49.     /**
  50.      * 调用apache的编码方法
  51.      */
  52.     private static String getenBASE64inCodec(byte[] b) {
  53.         if (b == null)
  54.             return null;
  55.         return new String((new Base64()).encode(b));
  56.     }

  57.     /**
  58.      * 调用apache的解码方法
  59.      */
  60.     private static byte[] getdeBASE64inCodec(String s) {
  61.         if (s == null)
  62.             return null;
  63.         return new Base64().decode(s.getBytes());
  64.     }
  65. }
复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2015-08-12 11:13 |只看该作者
java有zlib库好像是压缩解压的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP