>>分享Java编程技术,对《Java面向对象编程》等书籍提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 22493 个阅读者 刷新本主题
 * 贴子主题:  java使用gzip实现文件解压缩示例 回复文章 点赞(0)  收藏  
作者:日月光华    发表时间:2023-05-09 07:58:28     消息  查看  搜索  好友  邮件  复制  引用

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

代码如下:
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);  
    }  
}

程序猿的技术大观园:www.javathinker.net
网站系统异常


系统异常信息
Request URL: http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=1215

java.lang.NullPointerException

如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员