美文网首页
Gzip 压缩与解压缩

Gzip 压缩与解压缩

作者: Cloverss | 来源:发表于2017-12-04 11:24 被阅读0次

    压缩

        public static byte[] gzip(byte[] data) {
            try {
                ByteArrayOutputStream bos  = new ByteArrayOutputStream();
                GZIPOutputStream      gzip = new GZIPOutputStream(bos);
                gzip.write(data);
                gzip.finish();
                gzip.close();
                byte[] bytes = bos.toByteArray();
                bos.close();
                return bytes;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    

    解压缩

        public static byte[] ungzip(byte[] data) {
            try {
                ByteArrayInputStream  bis  = new ByteArrayInputStream(data);
                GZIPInputStream       gzip = new GZIPInputStream(bis);
                byte[]                bytes  = new byte[1024];
                int                   len;
                ByteArrayOutputStream bos  = new ByteArrayOutputStream();
                while ((len = gzip.read(bytes, 0, bytes.length)) != -1) {
                    bos.write(bytes, 0, len);
                }
                gzip.close();
                bis.close();
                byte[] byteArray = bos.toByteArray();
                bos.flush();
                bos.close();
                return byteArray;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    

    相关文章

      网友评论

          本文标题:Gzip 压缩与解压缩

          本文链接:https://www.haomeiwen.com/subject/acgtixtx.html