java使用gzip实现文件解压缩示例

所属分类: 软件编程 / java 阅读数: 49
收藏 0 赞 0 分享

复制代码 代码如下:

package com.cjonline.foundation.cpe.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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;

public abstract class GZipUtils { 

    public static final int BUFFER = 1024; 
    public static final String EXT = ".gz"; 

    /**
     * 数据压缩
     * 
     * @param data
     * @return
     * @throws Exception
     */ 
    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; 
    } 

    /**
     * 文件压缩
     * 
     * @param file
     * @throws Exception
     */ 
    public static void compress(File file) throws Exception { 
        compress(file, true); 
    } 

    /**
     * 文件压缩
     * 
     * @param file
     * @param delete
     *            是否删除原始文件
     * @throws Exception
     */ 
    public static void compress(File file, boolean delete) throws Exception { 
        FileInputStream fis = new FileInputStream(file); 
        FileOutputStream fos = new FileOutputStream(file.getPath() + EXT); 

        compress(fis, fos); 

        fis.close(); 
        fos.flush(); 
        fos.close(); 

        if (delete) { 
            file.delete(); 
        } 
    } 

    /**
     * 数据压缩
     * 
     * @param is
     * @param os
     * @throws Exception
     */ 
    public static void compress(InputStream is, OutputStream os) 
            throws Exception { 

        GZIPOutputStream gos = new GZIPOutputStream(os); 

        int count; 
        byte data[] = new byte[BUFFER]; 
        while ((count = is.read(data, 0, BUFFER)) != -1) { 
            gos.write(data, 0, count); 
        } 

        gos.finish(); 

        gos.flush(); 
        gos.close(); 
    } 

    /**
     * 文件压缩
     * 
     * @param path
     * @throws Exception
     */ 
    public static void compress(String path) throws Exception { 
        compress(path, true); 
    } 

    /**
     * 文件压缩
     * 
     * @param path
     * @param delete
     *            是否删除原始文件
     * @throws Exception
     */ 
    public static void compress(String path, boolean delete) throws Exception { 
        File file = new File(path); 
        compress(file, delete); 
    } 

    /**
     * 数据解压缩
     * 
     * @param data
     * @return
     * @throws Exception
     */ 
    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; 
    } 

    /**
     * 文件解压缩
     * 
     * @param file
     * @throws Exception
     */ 
    public static void decompress(File file) throws Exception { 
        decompress(file, true); 
    } 

    /**
     * 文件解压缩
     * 
     * @param file
     * @param delete
     *            是否删除原始文件
     * @throws Exception
     */ 
    public static void decompress(File file, boolean delete) throws Exception { 
        FileInputStream fis = new FileInputStream(file); 
        FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT, 
                "")); 
        decompress(fis, fos); 
        fis.close(); 
        fos.flush(); 
        fos.close(); 

        if (delete) { 
            file.delete(); 
        } 
    } 

    /**
     * 数据解压缩
     * 
     * @param is
     * @param os
     * @throws Exception
     */ 
    public static void decompress(InputStream is, OutputStream os) 
            throws Exception { 

        GZIPInputStream gis = new GZIPInputStream(is); 

        int count; 
        byte data[] = new byte[BUFFER]; 
        while ((count = gis.read(data, 0, BUFFER)) != -1) { 
            os.write(data, 0, count); 
        } 

        gis.close(); 
    } 

    /**
     * 文件解压缩
     * 
     * @param path
     * @throws Exception
     */ 
    public static void decompress(String path) throws Exception { 
        decompress(path, true); 
    } 

    /**
     * 文件解压缩
     * 
     * @param path
     * @param delete
     *            是否删除原始文件
     * @throws Exception
     */ 
    public static void decompress(String path, boolean delete) throws Exception { 
        File file = new File(path); 
        decompress(file, delete); 
    } 
}

更多精彩内容其他人还在看

java 中maven pom.xml文件教程详解

这篇文章主要介绍了java 中maven pom.xml文件教程详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合netty的实现方法

这篇文章主要介绍了spring boot整合netty的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Netty与Spring Boot的整合实现

这篇文章主要介绍了Netty与Spring Boot的整合的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring动态加载bean后调用实现方法解析

这篇文章主要介绍了Spring动态加载bean后调用实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现画图板上画一条直线

这篇文章主要为大家详细介绍了java实现画图板上画一条直线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java通过python命令执行DataX任务的实例

今天小编就为大家分享一篇Java通过python命令执行DataX任务的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springBoot集成redis的key,value序列化的相关问题

这篇文章主要介绍了springBoot集成redis的key,value序列化的相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java实现登录案例

这篇文章主要为大家详细介绍了java实现登录案例的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java解决请求跨域的两种方法

这篇文章主要为大家详细介绍了java解决请求跨域的两种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringBoot集成Beetl后统一处理页面异常的方法

这篇文章主要介绍了SpringBoot集成Beetl后统一处理页面异常的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多