欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

Android端可以對字符串進行壓縮,我們在進行大量簡單文本傳輸時,可以先壓縮字符串再發(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()使用平臺默認(rèn)編碼,也可以顯式的指定如toString("GBK")
  return out.toString();
 }
}

紅米手機測試輸出

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 , 耗時約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: 存壓縮后的數(shù)據(jù)進數(shù)據(jù)庫 zip1.length=1112
08-09 13:16:53.448 32248-32267/com.rustfisher.ndkproj D/rustApp: 壓縮后的數(shù)據(jù)已進數(shù)據(jù)庫 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

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

榮耀手機測試

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%

壓縮和解壓縮耗時視手機情況而定。

總結(jié)

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

相關(guān)文章

最新評論