亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)壓縮字符串的方法示例

 更新時(shí)間:2017年08月27日 15:10:13   作者:RustFisher  
最近在做Android開發(fā),遇到了需要壓縮字符串的功能,下面這篇文章主要給大家介紹了Android實(shí)現(xiàn)壓縮字符串的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

Android端可以對(duì)字符串進(jìn)行壓縮,我們?cè)谶M(jìn)行大量簡單文本傳輸時(shí),可以先壓縮字符串再發(fā)送。接收端接收后再解壓。也可以將字符串壓縮后存入數(shù)據(jù)庫中,下面話不多說了,來一起看看詳細(xì)的介紹吧。

使用到的類庫

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()使用平臺(tái)默認(rèn)編碼,也可以顯式的指定如toString("GBK")
  return out.toString();
 }
}

紅米手機(jī)測試輸出

08-09 13:16:53.388 32248-32267/com.rustfisher.ndkproj D/rustApp: 開始存入數(shù)據(jù)庫 ori1 len=304304
08-09 13:16:53.418 32248-32267/com.rustfisher.ndkproj D/rustApp: 已存入數(shù)據(jù)庫 ori1 len=304304 , 耗時(shí)約37 ms
08-09 13:16:53.418 32248-32267/com.rustfisher.ndkproj D/rustApp: 開始?jí)嚎s ori1 len=304304
08-09 13:16:53.438 32248-32267/com.rustfisher.ndkproj D/rustApp: 壓縮完畢 zip1 len=1112 , 耗時(shí)約19 ms
08-09 13:16:53.438 32248-32267/com.rustfisher.ndkproj D/rustApp: 存壓縮后的數(shù)據(jù)進(jìn)數(shù)據(jù)庫 zip1.length=1112
08-09 13:16:53.448 32248-32267/com.rustfisher.ndkproj D/rustApp: 壓縮后的數(shù)據(jù)已進(jìn)數(shù)據(jù)庫 zip1.length=1112 , 耗時(shí)約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: 解壓完畢 耗時(shí)約36 ms

存儲(chǔ)時(shí)間受存儲(chǔ)字符串的長度影響。字符串長度與存儲(chǔ)耗時(shí)正相關(guān)。

榮耀手機(jī)測試

08-09 10:38:42.759 23075-23109/com.rustfisher D/rustApp: 開始?jí)嚎s 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: 解壓完畢

此例中,榮耀壓縮耗時(shí)約5ms,解壓耗時(shí)約25ms。

可以看出,壓縮后與原長度之比 1112/304304, 約0.365%

壓縮和解壓縮耗時(shí)視手機(jī)情況而定。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論