JAVA壓縮字符串、解壓(Gzip)


 

 

gzip工具類

 

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

/**
 * Gzip壓縮工具類
 * @author*/
public class Gzip {
    private static Logger logger = LogManager.getLogger(Gzip.class);
    private static final int BYTE_LEN = 256;
    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";  
    
    private Gzip() {

    }

    /**
     * 解壓
     * @param bytes 待解壓byte數組
     * @return
     * @throws IOException
     */
    public static byte[] uncompress(byte[] bytes) throws IOException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);

        GZIPInputStream ungzip = new GZIPInputStream(in);
        byte[] buffer = new byte[BYTE_LEN];
        int n;
        while ((n = ungzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }

        return out.toByteArray();
    }
    
    /**
     * 解壓返回字符串
     * @param bytes 待解壓byte數組
     * @param encoding 編碼
     * @return
     */
    public static String uncompressToString(byte[] bytes, String encoding) {  
        if (bytes == null || bytes.length == 0) {  
            return null;  
        }  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);  
        try {  
            GZIPInputStream ungzip = new GZIPInputStream(in);  
            byte[] buffer = new byte[BYTE_LEN];  
            int n;  
            while ((n = ungzip.read(buffer)) >= 0) {  
                out.write(buffer, 0, n);  
            }  
            return out.toString(encoding);  
        } catch (IOException e) {  
            logger.error("gzip uncompress to string error.", e);  
        }  
        return null;  
    }  
    
    /**
     * 壓縮
     * @param str 待壓縮字符串
     * @param encoding 編碼
     * @return
     * @throws IOException
     */
    public static byte[] compress(String str, String encoding) throws IOException {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes(encoding));
        gzip.close();
        return out.toByteArray();
    }
    

}

 

 

 

使用demo

 public static void main(String[] args) {
        String input = "我是好人";
        try {
            //壓縮
            byte[] newInfoBytes = Gzip.compress(input, Gzip.GZIP_ENCODE_UTF_8);

            //解壓
            String newInput = Gzip.uncompressToString(newInfoBytes, Gzip.GZIP_ENCODE_UTF_8);

            System.out.println(newInput);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM