Androidはzlib圧縮と解凍を実現

4345 ワード

/**
 *     
 *
 * @param gzipStr
 * @return
 */
public static String decompress(String gzipStr) {
    if (gzipStr.equals("")) {
        return null;
    }
    byte[] t = AbBase64.decode(gzipStr);

    Inflater decompresser = new Inflater();
    decompresser.setInput(t, 0, t.length);
    //  byte[]    ,                  ,    zip    byte[] result = new byte[t.length];
    try {
        int resultLength = decompresser.inflate(result); //     } catch (DataFormatException e) {
        e.printStackTrace();
    }
    decompresser.end();
    return new String(result);

}

//  
public static String compress(String data) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DeflaterOutputStream zos = new DeflaterOutputStream(bos);
        zos.write(data.getBytes());
        zos.close();
        return AbBase64.encode(bos.toByteArray());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return "ZIP_ERR";
}