关于GZIPOutputStream的一些使用总结

1. 首先,网上很多教程有写怎么使用java内置的gzip工具类,这里就不多少了,主要是想说一下,当我们压缩生成gz文件的时候,如果要保留原始文件的后缀,比如a.xml压缩成a.xml.gz 那么,我们在生成压缩包指定文件名的时候把这个.xml加上。

Gzip工具类:

package com.util;

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

public class GzipUtil {

    /**
     * compress the file in sourceFilePath to destDirectory
     * @param sourceFilePath source file path
     * @param destDirectory gzip file path
     */
    public static void compress(String sourceFilePath, String destDirectory) {
        try(
                FileInputStream in = new FileInputStream(new File(sourceFilePath));
                GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(destDirectory))
        ) {
            byte[] buf = new byte[10240];
            int len;
            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);

            }catch (IOException e) {
               
            }
        } catch (FileNotFoundException e) {
             System.out.println("Could not find the sourceFilePath ... "+sourceFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * compress the source content to byte array
     * @param sourceFileByte byte[] from source content
     * @return byte array from GZIPOutputStream
     */
    public static byte[] compressToByteArray(byte[] sourceFileByte) {
        try(
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)
        ) {
            gzipOutputStream.write(sourceFileByte);
            gzipOutputStream.flush();
            gzipOutputStream.finish();
            return baos.toByteArray();
        } catch (FileNotFoundException e) {
            System.out.println("Could not find the sourceFilePath ... "+sourceFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

测试类这样写:

GzipUtil.compress("D:\\759.1_1.20190603T0200-04.xml", "D:\\759.1_1.20190603T0200-04.xml.gz");

2. 还有,有时候我们想要得到Gzip流来直接流方式上传文件,可以像上面工具类中的compressToByteArray()这样,参数也就是我们的内容的字节数组啦。

注意

1) gzipOutputStream.write(sourceFileByte); 之后要记得 flush() 和finish() ,不然压缩包的东西是空的。

2) 当我们分批上传这个Gzip流到同样的指定路径的时候,我们也可以得到正常的Gz包。

这里部分参考:https://blog.csdn.net/ll413343176/article/details/90049422

3) 当遇到:EOFException: Unexpected end of ZLIB input stream。可以参考:https://blog.csdn.net/Revivedsun/article/details/52073554

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值