java 使用gzip压缩和解压 传输文件必备

java gzip 压缩解压工具类,开箱即用
gzip原理看我另外一篇介绍
压缩效果直接看图:
在这里插入图片描述

package com.yeahmobi.datacheck.util;

import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class CompressUtil {
    public static void main(String[] args) throws IOException {
    	// 原文件
        String inFileName = "D:\\response.xls";
        compressFile(inFileName);

		// 压缩文件
        //String gzFileName = "D:\\response.xls.gz";
        //doUncompressFile(gzFileName);
    }

    // 压缩
    public static void compressFile(String inFileName) throws IOException {
        FileInputStream in = null;
        GZIPOutputStream out = null;

        String outFileName = inFileName + ".gz";
        try {
            in = new FileInputStream(new File(inFileName));
        } catch (FileNotFoundException e) {
            throw new RuntimeException("文件不存在:" + inFileName);
        }
        out = new GZIPOutputStream(new FileOutputStream(outFileName));

        byte[] buf = new byte[10240];
        int len = 0;
        if (in != null && out != null) {
            try {
                while (((in.available() > 10240) && (in.read(buf)) > 0)) {
                    out.write(buf);
                }
                len = in.available();
                in.read(buf, 0, len);
                out.write(buf, 0, len);
                in.close();
                out.flush();
                out.close();
            } catch (IOException e) {
                throw new RuntimeException("压缩失败:" + inFileName);
            }
        }
        in.close();
        out.close();

    }

    // 解压
    public static void doUncompressFile(String inFileName) {
        try {
            if (!getExtension(inFileName).equalsIgnoreCase("gz")) {
                throw new RuntimeException("文件名必须是gz后缀");
            }

            GZIPInputStream in = null;
            try {
                in = new GZIPInputStream(new FileInputStream(inFileName));
            } catch (FileNotFoundException e) {
                throw new RuntimeException("文件不存在 " + inFileName);
            }

            String outFileName = getFileName(inFileName);
            try (FileOutputStream out = new FileOutputStream(outFileName);) {

                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                throw new RuntimeException("解压失败" + outFileName);
            }

        } catch (IOException e) {
            throw new RuntimeException("失败");
        }

    }
    
    // 获取文件后缀名
    public static String getExtension(String f) {
        String ext = "";
        int i = f.lastIndexOf('.');

        if (i > 0 && i < f.length() - 1) {
            ext = f.substring(i + 1);
        }
        return ext;
    }
    
    // 获取文件名
    public static String getFileName(String f) {
        String fname = "";
        int i = f.lastIndexOf('.');

        if (i > 0 && i < f.length() - 1) {
            fname = f.substring(0, i);
        }
        return fname;
    }
}

你可能感兴趣的:(工具类汇总)