Android實現(xiàn)壓縮字符串的方法示例
前言
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)文章
Android RecyclerView實現(xiàn)下拉列表功能
這篇文章主要介紹了Android RecyclerView實現(xiàn)下拉列表功能,下拉展開更多選項,具有一定的實用性,感興趣的小伙伴們可以參考一下2016-11-11解決Bitmap通過getWidth和getHeight獲取尺寸不符的問題
這篇文章主要介紹了解決Bitmap通過getWidth和getHeight獲取尺寸不符的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Android設(shè)備藍牙連接掃描槍獲取掃描內(nèi)容
這篇文章主要為大家詳細(xì)介紹了Android設(shè)備藍牙連接掃描槍獲取掃描內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09androidx下的fragment的lazy懶加載問題詳解
這篇文章主要介紹了androidx下的fragment的lazy懶加載問題詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Android編程實現(xiàn)WebView自適應(yīng)全屏方法小結(jié)
這篇文章主要介紹了Android編程實現(xiàn)WebView自適應(yīng)全屏方法,結(jié)合實例形式總結(jié)了三種常用的WebView自適應(yīng)全屏實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼
Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼,需要的朋友可以參考一下2013-05-05