令仔代码收藏系列----Java压缩与解压缩

Posted 令仔很忙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了令仔代码收藏系列----Java压缩与解压缩相关的知识,希望对你有一定的参考价值。

package com.flight.inter.otaadapter.manage;

import com.flight.inter.otaadapter.commons.util.Base64Util;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


public class MessageZIPManage 

    /**
     * 压缩
     * @param encode
     * @return
     */
    public static String zipEncode(String encode)
        byte[] pureResult;
        try 
            pureResult = encode.getBytes("UTF-8");
            ByteArrayInputStream inputStream =
                    new ByteArrayInputStream(pureResult);
            ByteArrayOutputStream miwenOutput=new ByteArrayOutputStream();
            GZIPOutputStream outStream =
                    new GZIPOutputStream ( miwenOutput);
            byte[] buf = new byte[10000];
            while (true) 
                int size = inputStream.read(buf);
                if (size <= 0)
                    break;
                outStream.write(buf, 0, size);
            
            outStream.close();
            byte[] miwenbytes=miwenOutput.toByteArray();
            String pursf= Base64Util.encode2Str(miwenbytes);
            return pursf;

         catch (Exception e) 
            return null;
        
    

    /**
     * 解压缩
     * @param decode
     * @return
     */
    public static String zipDecode(String decode)
        byte[] pureResult;
        try 
            pureResult = Base64Util.decode2ByteArray(decode);
            ByteArrayOutputStream outStream =
                    new ByteArrayOutputStream(10 * pureResult.length);
            GZIPInputStream inStream =
                    new GZIPInputStream ( new ByteArrayInputStream(pureResult) );
            byte[] buf = new byte[10000];
            while (true) 
                int size = inStream.read(buf);
                if (size <= 0)
                    break;
                outStream.write(buf, 0, size);
            
            outStream.close();
            String pursf=new String(outStream.toByteArray());
            return pursf;

         catch (Exception e) 
            return null;
        

    


以上是关于令仔代码收藏系列----Java压缩与解压缩的主要内容,如果未能解决你的问题,请参考以下文章

免费毕设JAVA文件压缩与解压缩实践(源代码+论文)

Java实现压缩文件与解压缩文件

毕业设计JAVA文件压缩与解压缩实践(源代码+论文)

C++项目:基于HuffmanTree实现文件的压缩与解压缩

Java压缩与解压缩问题

java GZIP压缩与解压缩