一、zlibCompress数据压缩
public static byte[] zlibCompress(byte[] data) {
byte[] output;
//LEVEL 5 to make it the same as iOS COMPRESSION_ZLIB
Deflater compresser = new Deflater(5,true);
compresser.reset();
compresser.setInput(data);
compresser.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
try {
byte[] buf = new byte[1024];
while (!compresser.finished()) {
int i = compresser.deflate(buf);
bos.write(buf, 0, i);
}
output = bos.toByteArray();
} catch (Exception e) {
output = data;
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
compresser.end();
return output;
}
二、zlibCompress数据解压缩
public static byte[] zlibDecompress(byte[] data) {
byte[] output;
Inflater decompresser = new Inflater(true);
decompresser.reset();
decompresser.setInput(data);
ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
try {
byte[] buf = new byte[1024];
while (!decompresser.finished()) {
int i = decompresser.inflate(buf);
o.write(buf, 0, i);
}
output = o.toByteArray();
} catch (Exception e) {
output = data;
e.printStackTrace();
} finally {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
decompresser.end();
return output;
}
三、二维码的生成和恢复
//decompress
String str = result.substring(AppConstants.WatchWallet.PREFIX_SIGN.length());
byte[] decodedBytes = android.util.Base64.decode(str.getBytes("UTF-8"), Base64.NO_WRAP);
qrstr_decoded = new String(StringUtils.zlibDecompress(decodedBytes));
//compress
byte[] encodedBytes = Base64.encode(StringUtils.zlibCompress(jsonstr.getBytes("utf-8")), Base64.NO_WRAP);
mQrcodeMvpPresenter.createQrcode(AppConstants.WatchWallet.PREFIX_SIGN + new String(encodedBytes));
四、代码下载
点击下载