170814、Java使用gzip壓縮檔、還原檔案

來源:互聯網
上載者:User

標籤:blog   com   解壓   logs   span   rgs   oid   close   壓縮   

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();    }}

 

170814、Java使用gzip壓縮檔、還原檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.