/** * 压缩 * @param unGzipStr * @return */ public static String GzipCompress(String unGzipStr) { if (TextUtils.isEmpty(unGzipStr)) { return null; } try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(baos); gzip.write(unGzipStr.getBytes()); gzip.close(); byte[] encode = baos.toByteArray(); baos.flush(); baos.close(); return Base64.encodeToString(encode, 0); } catch (Throwable throwable) { } return null; } /** * 解压 * @param gzipStr * @return */ public static String Gzipdecompress(String gzipStr) { try { if (TextUtils.isEmpty(gzipStr)) { return null; } byte[] t = Base64.decode(gzipStr, 0); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(t); GZIPInputStream gzip = new GZIPInputStream(in); byte[] buffer = new byte[1024]; int n = 0; while ((n = gzip.read(buffer, 0, buffer.length)) > 0) { out.write(buffer, 0, n); } gzip.close(); in.close(); out.close(); return out.toString(); } catch (Throwable throwable) { } return null; }
Gzip压缩与解压
最新推荐文章于 2024-04-13 20:44:36 发布