java GZIP压缩与解压缩

1.GZIP压缩

    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 ( Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

2.GZIP解压缩

    public static byte[] uncompress(byte[] bytes) {
        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);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

3.工具代码集合

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPUtils  {
    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";

    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 ( Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static byte[] compress(String str) throws IOException {
        return compress(str, GZIP_ENCODE_UTF_8);
    }

    public static byte[] uncompress(byte[] bytes) {
        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);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static String uncompressToString(byte[] bytes, String encoding) {
        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);
            }
            return out.toString(encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String uncompressToString(byte[] bytes) {
        return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
    } 

    public static void main(String[] args) throws IOException {
        String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        System.out.println("字符串长度:"+s.length());
        System.out.println("压缩后::"+compress(s).length);
        System.out.println("解压后:"+uncompress(compress(s)).length);
        System.out.println("解压字符串后::"+uncompressToString(compress(s)).length());
    }
}

原文地址:https://www.cnblogs.com/zhuyeshen/p/12160741.html

时间: 2024-08-30 02:50:52

java GZIP压缩与解压缩的相关文章

JAVA中压缩与解压缩

以压缩Zip文件为例.主要是通过ZipOutputStream类实现. import java.io.*; import java.util.*; import java.text.*; import java.util.zip.*; //压缩一个文件 //压缩一个文件夹 public class Hello { public static void main(String[] args)throws Exception { final int BUFFER_LENGTH = 1024*1024

java 文件压缩及解压缩

java操作windows命令(Rar.exe)执行文件压缩 // String srcPath = "D:\\test";// 被压缩文件夹 String srcPath = "D:\\test.txt";// 被压缩文件 String destPath = "D:\\test.rar";// 压缩后文件 String rarexePath = "C:\\Program Files\\WinRAR\\Rar.exe"; //

java 版本压缩、解压缩zip

import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class FileMgr { public FileMgr() { } /** * 压缩文件 * @param srcfile File[] 需要压缩的文件列表 * @param zipfile File 压缩

【原创】java实现压缩、解压缩的常见问题Memo

国庆假期,宅呀~ 无意中看到一篇java压缩算法的文章,就顺便度娘了一些相关文章来看. 本来想深入研究一下算法,但是,毕竟是假期,哪有那份心思啊,就实际应用简单Memo一下吧. 1. 中文乱码问题 其实这是无意中看到的,度娘一下漫天都是,看来各位程序猿还是以务实的应用为多啊. 度娘里,99%的帖子都是说用 Ant中的 org.apache.tools.zip.*  来代替 java.util.zip.* 这种办法来解决中文乱码问题.究其原因是,jdk中写死了UTF-8编码,而操作系统的文件编码如

Java Web 减少网络 IO、静态资源磁盘 IO 有效的办法--响应使用 GZIP( 压缩http请求与响应gzip压缩)

(转载http://blog.csdn.net/hylclxy/article/details/7779662) 出于节约流量考虑, 客户端在向服务端发送request的时候对post数据进行gzip压缩, 同时服务端把返回的数据也进行gzip压缩. 为防止遗忘, 记录在此.   编写工具类GzipUtil.java, 开始没考虑好, 方法实现得较乱: public static String METHOD_POST = "POST"; public static final Stri

Java.util.zip 压缩与解压缩工具类

Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jdk 编写该工具类,不依赖任何第三方 jar,随用随取,实现功能大体如下: 1.目录级别递归压缩与解压缩 zip: 2.单文件压缩和解压缩 zip : import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipIn

170814、Java使用gzip压缩文件、还原文件

package com.rick.utils; import java.io.*; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /******************************************************** *@Desc: gizp工具类 *@Author: ZRP *@Date: 2017/11/9 9:31 ********************

利用JAVA API函数实现数据的压缩与解压缩

综述 许多信息资料都或多或少的包含一些多余的数据.通常会导致在客户端与服务器之间,应用程序与计算机之间极大的数据传输量.最常见的解决数据存储和信息传送的方法是安装额外的存储设备和扩展现有的通讯能力.这样做是可以的,但无疑会增加组织的运作成本.一种有效的解决数据存储与信息传输的方法是通过更有效率的代码来存储数据.这篇文章简要的介绍了数据的压缩与解压缩,并展示了用java.util.zip包来实现数据的压缩与解压缩是多么的方便与高效. 当然用诸如WinZip,gzip,和Java压缩(或jar)之类

linux笔记 文件的压缩与解压缩gzip、bzip2、xz、zip&unzip、tar打包、tar打包和压缩并用

压缩文件意义节省网络传输带宽,降低磁盘使用率.但会使用一定的cpu. 1.gzip 不支持压缩目录,gzip后直接跟文件名,在当前目录下压缩时,原文件会消失. #gzip filename 指定压缩目录:#gzip -c 123.txt > /tmp/123.txt.gz 将当前目录下123.txt文件压缩到tmp目录下名字为123.txt.gz 解压缩:#gzip -d file.gz #zcat file.gz  查看.gz文件内容 2.bzip2 不支持压缩目录,bzip2后可直接跟文件名