网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何在Android中对字符串进行压缩

这期内容当中小编将会给大家带来有关如何在Android中对字符串进行压缩,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

站在用户的角度思考问题,与客户深入沟通,找到德保网站设计与德保网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广、国际域名空间、雅安服务器托管、企业邮箱。业务覆盖德保地区。

使用到的类库

GZIPOutputStream

代码示例

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class StrZipUtil {
 /**
  * @param input 需要压缩的字符串
  * @return 压缩后的字符串
  * @throws IOException IO
  */
 public static String compress(String input) throws IOException {
  if (input == null || input.length() == 0) {
   return input;
  }
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  GZIPOutputStream gzipOs = new GZIPOutputStream(out);
  gzipOs.write(input.getBytes());
  gzipOs.close();
  return out.toString("ISO-8859-1");
 }
 /**
  * @param zippedStr 压缩后的字符串
  * @return 解压缩后的
  * @throws IOException IO
  */
 public static String uncompress(String zippedStr) throws IOException {
  if (zippedStr == null || zippedStr.length() == 0) {
   return zippedStr;
  }
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  ByteArrayInputStream in = new ByteArrayInputStream(zippedStr
    .getBytes("ISO-8859-1"));
  GZIPInputStream gzipIs = new GZIPInputStream(in);
  byte[] buffer = new byte[256];
  int n;
  while ((n = gzipIs.read(buffer)) >= 0) {
   out.write(buffer, 0, n);
  }
  // toString()使用平台默认编码,也可以显式的指定如toString("GBK")
  return out.toString();
 }
}

红米手机测试输出

08-09 13:16:53.388 32248-32267/com.rustfisher.ndkproj D/rustApp: 开始存入数据库 ori1 len=304304
08-09 13:16:53.418 32248-32267/com.rustfisher.ndkproj D/rustApp: 已存入数据库 ori1 len=304304 , 耗时约37 ms
08-09 13:16:53.418 32248-32267/com.rustfisher.ndkproj D/rustApp: 开始压缩 ori1 len=304304
08-09 13:16:53.438 32248-32267/com.rustfisher.ndkproj D/rustApp: 压缩完毕 zip1 len=1112 , 耗时约19 ms
08-09 13:16:53.438 32248-32267/com.rustfisher.ndkproj D/rustApp: 存压缩后的数据进数据库 zip1.length=1112
08-09 13:16:53.448 32248-32267/com.rustfisher.ndkproj D/rustApp: 压缩后的数据已进数据库 zip1.length=1112 , 耗时约8 ms
08-09 13:16:53.448 32248-32267/com.rustfisher.ndkproj D/rustApp: 解压开始
08-09 13:16:53.488 32248-32267/com.rustfisher.ndkproj D/rustApp: 解压完毕 耗时约36 ms

存储时间受存储字符串的长度影响。字符串长度与存储耗时正相关。

荣耀手机测试

08-09 10:38:42.759 23075-23109/com.rustfisher D/rustApp: 开始压缩 ori1 len=304304
08-09 10:38:42.764 23075-23109/com.rustfisher D/rustApp: 压缩完毕 zip1 len=1112
08-09 10:38:42.764 23075-23109/com.rustfisher D/rustApp: 解压开始
08-09 10:38:42.789 23075-23109/com.rustfisher D/rustApp: 解压完毕

此例中,荣耀压缩耗时约5ms,解压耗时约25ms。

可以看出,压缩后与原长度之比 1112/304304, 约0.365%

压缩和解压缩耗时视手机情况而定。

上述就是小编为大家分享的如何在Android中对字符串进行压缩了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


网页名称:如何在Android中对字符串进行压缩
网站链接:http://bjjierui.cn/article/pdcpgh.html

其他资讯