java实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩

全栈工程师开发手册 (作者:栾鹏)
java教程全解

java实现gzip压缩解压缩byte[]字节数组,文件,字符串。

测试代码

public static void main(String[] args) {
		try {
			
			//测试字符串
			String inputStr = "zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org";  
			System.err.println("原文:\t" + inputStr);  
			  
	        byte[] input = inputStr.getBytes();  
	        System.err.println("长度:\t" + input.length);  
	  
	        byte[] data = GZipUtils.compress(input);  
	        System.err.println("压缩后:\t");  
	        System.err.println("长度:\t" + data.length);  
	  
	        byte[] output = GZipUtils.decompress(data);  
	        String outputStr = new String(output);  
	        System.err.println("解压缩后:\t" + outputStr);  
	        System.err.println("长度:\t" + output.length);  
	  
	        //测试文件
	        FileOutputStream fos = new FileOutputStream("test.txt");  
	        
	        fos.write(inputStr.getBytes());  
	        fos.flush();  
	        fos.close();  
	  
	        GZipUtils.compress("test.txt", false);  
	        GZipUtils.decompress("test.txt.gz", false);  
	        File file = new File("test.txt");  
	        FileInputStream fis = new FileInputStream(file);  
	        DataInputStream dis = new DataInputStream(fis);  
	        byte[] data1 = new byte[(int) file.length()];  
	        dis.readFully(data1);  
	        fis.close();  
	        outputStr = new String(data1);  
	        System.err.println("解压缩后:\t" + outputStr);  
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

gzip压缩解压缩工具类的实现

package com.lp.app.io;

import java.io.ByteArrayInputStream;  
import java.io.ByteArrayOutputStream;  
import java.io.DataInputStream;
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.util.zip.GZIPInputStream;  
import java.util.zip.GZIPOutputStream;  
  
//GZIP压缩解压缩工具 
public abstract class GZipUtils {  
  
    //数据压缩 
    public static byte[] compress(byte[] data) throws Exception {  
        ByteArrayInputStream bais = new ByteArrayInputStream(data);  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  
        // 压缩  
        compress(bais, baos);  
  
        byte[] output = baos.toByteArray();  
  
        baos.flush();  
        baos.close();  
  
        bais.close();  
  
        return output;  
    }  
  
    //文件压缩 
    public static void compress(File file) throws Exception {  
        compress(file, true);  
    }  
  
    //文件压缩 是否删除原始文件 
    public static void compress(File file, boolean delete) throws Exception {  
        FileInputStream fis = new FileInputStream(file);  
        FileOutputStream fos = new FileOutputStream(file.getPath() + ".gz");  
  
        compress(fis, fos);  
  
        fis.close();  
        fos.flush();  
        fos.close();  
  
        if (delete) {  
            file.delete();  
        }  
    }  
  
    //数据压缩 
    public static void compress(InputStream is, OutputStream os)  throws Exception {  
  
        GZIPOutputStream gos = new GZIPOutputStream(os);  
  
        int count;  
        byte data[] = new byte[1024];  
        while ((count = is.read(data, 0, 1024)) != -1) {  
            gos.write(data, 0, count);  
        }  
  
        gos.finish();  
  
        gos.flush();  
        gos.close();  
    }  
  
    //文件压缩  默认删除源文件
    public static void compress(String path) throws Exception {  
        compress(path, true);  
    }  
  
    //文件压缩 是否删除原始文件 
    public static void compress(String path, boolean delete) throws Exception {  
        File file = new File(path);  
        compress(file, delete);  
    }  
  
    //数据解压缩 
    public static byte[] decompress(byte[] data) throws Exception {  
        ByteArrayInputStream bais = new ByteArrayInputStream(data);  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  
        // 解压缩  
  
        decompress(bais, baos);  
  
        data = baos.toByteArray();  
  
        baos.flush();  
        baos.close();  
  
        bais.close();  
  
        return data;  
    }  
  
    //文件解压缩 
    public static void decompress(File file) throws Exception {  
        decompress(file, true);  
    }  
  
    //文件解压缩  是否删除原始文件 
    public static void decompress(File file, boolean delete) throws Exception {  
        FileInputStream fis = new FileInputStream(file);  
        FileOutputStream fos = new FileOutputStream(file.getPath().replace(".gz",  ""));  
        decompress(fis, fos);  
        fis.close();  
        fos.flush();  
        fos.close();  
  
        if (delete) {  
            file.delete();  
        }  
    }  
  
    //数据解压缩 
    public static void decompress(InputStream is, OutputStream os)  throws Exception {  
  
        GZIPInputStream gis = new GZIPInputStream(is);  
  
        int count;  
        byte data[] = new byte[1024];  
        while ((count = gis.read(data, 0, 1024)) != -1) {  
            os.write(data, 0, count);  
        }  
  
        gis.close();  
    }  
  
    //文件解压缩 ,默认删除源文件
    public static void decompress(String path) throws Exception {  
        decompress(path, true);  
    }  
  
    //文件解压缩   是否删除原始文件 
    public static void decompress(String path, boolean delete) throws Exception {  
        File file = new File(path);  
        decompress(file, delete);  
    }  
  
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾讯AI架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值