Android Gzip 壓縮與解壓

/**
 * Created by luoliuqing on 17/10/21.
 * GZip工具類
 */
public class GZipUtil {
    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";

    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";

    /**
     * 字符串壓縮為GZIP字節(jié)數(shù)組
     * @param str
     * @param encoding
     * @return
     */
    public static byte[] compress(String str, String encoding) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes(encoding));
            gzip.close();
        } catch (IOException e) {
            Log.e("gzip compress error.", e.getMessage());
        }
        return out.toByteArray();
    }

    /**
     * Gzip  byte[] 解壓成字符串
     * @param bytes
     * @return
     */
    public static String uncompressToString(byte[] bytes) {
        return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
    }


    /**
     * Gzip  byte[] 解壓成字符串
     * @param bytes
     * @param encoding
     * @return
     */
        public static String uncompressToString(byte[] bytes, String encoding) {
        String content = null;
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            content =  out.toString(encoding);
        } catch (IOException e) {
            Log.e("gzip compress error.",e.getMessage());
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return content;
    }
    /**
     * 判斷byte[]是否是Gzip格式
     * @param data
     * @return
     */
    public static boolean isGzip(byte[] data) {
        int header = (int)((data[0]<<8) | data[1]&0xFF);
        return header == 0x1f8b;
    }

}

調(diào)用

壓縮
 GZipUtil.compress(str)

解壓
GZipUtil.uncompressToString(bytes)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Windows下的常見壓縮格式有.zip和.rar,而Linux下的常見壓縮格式有:gz,bzip2,xz,zip...
    Aubin閱讀 432評(píng)論 0 1
  • Zip文件結(jié)構(gòu) ZIP文件結(jié)構(gòu)如下圖所示, File Entry表示一個(gè)文件實(shí)體,一個(gè)壓縮文件中有多個(gè)文件實(shí)體。 ...
    卡咔喀閱讀 12,146評(píng)論 0 9
  • 前言 最近由于項(xiàng)目需要,需要我諒解一下關(guān)于在移動(dòng)平臺(tái)的解壓功能,在移動(dòng)平臺(tái)解壓,我個(gè)人感覺是沒有太大必要的,畢竟手...
    NKming閱讀 20,607評(píng)論 27 65
  • 不同數(shù)據(jù)類型與NSData互轉(zhuǎn)參考如下:NSData 類型轉(zhuǎn)換 在開發(fā)中,經(jīng)常要對(duì)比較大的數(shù)據(jù)進(jìn)行壓縮后再上傳服務(wù)...
    iOS_肖晨閱讀 4,697評(píng)論 1 51
  • 電影的一開頭,便是米歇爾和妹妹,朋友們?cè)邴溙镏斜寂堋⒈荣悺⒏偁帯土P。到了那個(gè)廢棄的屋子后,有的人就必須要接受懲罰...
    王澤宇_閱讀 925評(píng)論 0 1