zoukankan      html  css  js  c++  java
  • 170814、Java使用gzip压缩文件、还原文件

    package com.rick.utils;
    
    import java.io.*;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    
    /********************************************************
     *@Desc: gizp工具类
     *@Author: ZRP
     *@Date: 2017/11/9 9:31
     ********************************************************/
    public class GzipUtil {
    
        /**
         * @Desc :  gzip压缩
         * @Author : ZRP
         * @Params: [data]
         * @Return: byte[]
         * @Date : 2017/11/9 9:43
         */
        public static byte[] gzip(byte[] data) throws Exception {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(bos);
            gzip.write(data);
            gzip.finish();
            gzip.close();
            byte[] ret = bos.toByteArray();
            bos.close();
            return ret;
        }
    
    
        /**
         * @Desc :  gzip解压缩
         * @Author : ZRP
         * @Params: [data]
         * @Return: byte[]
         * @Date : 2017/11/9 9:47
         */
        public static byte[] ungzip(byte[] data) throws Exception{
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            GZIPInputStream gzip = new GZIPInputStream(bis);
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((num = gzip.read(buf, 0, buf.length)) != -1){
                bos.write(buf, 0, num);
            }
            gzip.close();
            bis.close();
            byte[] ret = bos.toByteArray();
            bos.flush();
            bos.close();
            return ret;
        }
    
        /**
         * 测试压缩和还原
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception{
            //读取文件
            String readPath = System.getProperty("user.dir") + File.separatorChar + "sources" + File.separatorChar + "001.jpg";
            File file = new File(readPath);
            FileInputStream in = new FileInputStream(file);
            byte[] data = new byte[in.available()];
            in.read(data);
            in.close();
            System.out.println("文件原始大小:" + data.length);
    
            //测试压缩
            byte[] ret1 = GzipUtil.gzip(data);
            System.out.println("压缩之后大小:" + ret1.length);
            byte[] ret2 = GzipUtil.ungzip(ret1);
            System.out.println("还原之后大小:" + ret2.length);
    
    
            //写出文件
            String writePath = System.getProperty("user.dir") + File.separatorChar + "receive" +  File.separatorChar + "001.jpg";
            FileOutputStream fos = new FileOutputStream(writePath);
            fos.write(ret2);
            fos.close();
    
        }
    
    }
  • 相关阅读:
    深度学习练习(三)
    深度学习核心技术笔记(一)
    tensorflow的函数
    举例
    Tensorflow 笔记
    tensorflow框架
    基于python的感知机
    深度学习练习(一)
    深度学习练习(二)
    [javascript 实践篇]——那些你不知道的“奇淫巧技”
  • 原文地址:https://www.cnblogs.com/zrbfree/p/7807994.html
Copyright © 2011-2022 走看看