Java 壓縮字符串 工具類


壓縮算法進行字符串壓縮


提前小結:Deflater > gzip > zip


Deflater壓縮,Inflater解壓(較好)


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.output.ByteArrayOutputStream;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * DeflaterUtils 壓縮字符串
 */
public class DeflaterUtils {

    /**
     * 壓縮
     */
    public static String zipString(String unzipString) {
        /*
         *     https://www.yiibai.com/javazip/javazip_deflater.html#article-start
         *     0 ~ 9 壓縮等級 低到高
         *     public static final int BEST_COMPRESSION = 9;            最佳壓縮的壓縮級別。
         *     public static final int BEST_SPEED = 1;                  壓縮級別最快的壓縮。
         *     public static final int DEFAULT_COMPRESSION = -1;        默認壓縮級別。
         *     public static final int DEFAULT_STRATEGY = 0;            默認壓縮策略。
         *     public static final int DEFLATED = 8;                    壓縮算法的壓縮方法(目前唯一支持的壓縮方法)。
         *     public static final int FILTERED = 1;                    壓縮策略最適用於大部分數值較小且數據分布隨機分布的數據。
         *     public static final int FULL_FLUSH = 3;                  壓縮刷新模式,用於清除所有待處理的輸出並重置拆卸器。
         *     public static final int HUFFMAN_ONLY = 2;                僅用於霍夫曼編碼的壓縮策略。
         *     public static final int NO_COMPRESSION = 0;              不壓縮的壓縮級別。
         *     public static final int NO_FLUSH = 0;                    用於實現最佳壓縮結果的壓縮刷新模式。
         *     public static final int SYNC_FLUSH = 2;                  用於清除所有未決輸出的壓縮刷新模式; 可能會降低某些壓縮算法的壓縮率。
         */

        //使用指定的壓縮級別創建一個新的壓縮器。
        Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
        //設置壓縮輸入數據。
        deflater.setInput(unzipString.getBytes(StandardCharsets.UTF_8));
        //當被調用時,表示壓縮應該以輸入緩沖區的當前內容結束。
        deflater.finish();

        final byte[] bytes = new byte[512];
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(512);

        while (!deflater.finished()) {
            //壓縮輸入數據並用壓縮數據填充指定的緩沖區。
            int length = deflater.deflate(bytes);
            outputStream.write(bytes, 0, length);
        }
        //關閉壓縮器並丟棄任何未處理的輸入。
        deflater.end();
        return Base64.encodeBase64String(outputStream.toByteArray());
        //處理回車符
//        return zipString.replaceAll("[\r\n]", "");
    }

    /**
     * 解壓縮
     */
    public static String unzipString(String zipString) {
        byte[] decode = Base64.decodeBase64(zipString);
        //創建一個新的解壓縮器  https://www.yiibai.com/javazip/javazip_inflater.html

        Inflater inflater = new Inflater();
        //設置解壓縮的輸入數據。
        inflater.setInput(decode);
        final byte[] bytes = new byte[512];
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(512);
        try {
            //finished() 如果已到達壓縮數據流的末尾,則返回true。
            while (!inflater.finished()) {
                //將字節解壓縮到指定的緩沖區中。
                int length = inflater.inflate(bytes);
                outputStream.write(bytes, 0, length);
            }
        } catch (DataFormatException e) {
            e.printStackTrace();
            return null;
        } finally {
            //關閉解壓縮器並丟棄任何未處理的輸入。
            inflater.end();
        }

        try {
            return outputStream.toString("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}


總結

  • 壓縮前的字節長度為:1825
  • 壓縮后的字節長度為:284
  • 壓縮率為63.73%,壓縮后體積為原來的36.27%
  • 5000字符長度壓縮耗時5ms(較快)

壓縮率和壓縮前字節長度有一定關系,並不是隨着字節長度線性增長。如:

5000字符長度,壓縮后:2500左右
13000字符長度,壓縮后:4000左右

注:經優化,已解決偶爾出現的中文亂碼問題,猜測與java的啟動編碼有關


zip與Gzip壓縮(還行)


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

/**
 * 推薦gzip
 */
public class ZipUtils {

    /**
     * 使用gzip進行壓縮
     */
    public static String gzip(String primStr) {
        if (primStr == null || primStr.length() == 0) {
            return primStr;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(primStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return new sun.misc.BASE64Encoder().encode(out.toByteArray());
    }

    /**
     * <p>Description:使用gzip進行解壓縮</p>
     *
     * @param compressedStr
     * @return
     */
    public static String gunzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);

            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }

    /**
     * 使用zip進行壓縮
     *
     * @param str 壓縮前的文本
     * @return 返回壓縮后的文本
     */
    public static final String zip(String str) {
        if (str == null)
            return null;
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        String compressedStr = null;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str.getBytes());
            zout.closeEntry();
            compressed = out.toByteArray();
            compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
        } catch (IOException e) {
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressedStr;
    }

    /**
     * 使用zip進行解壓縮
     *
     * @param compressedStr 壓縮后的文本
     * @return 解壓后的字符串
     */
    public static final String unzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed = null;
        try {
            byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            zin = new ZipInputStream(in);
            zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return decompressed;
    }
    
}

總結

  • 壓縮前的字節長度為:1825
  • 壓縮后的字節長度為:307
  • 壓縮率為62.04%,壓縮后體積為原來的37.95%
  • 5000字符長度壓縮耗時20ms(較慢)

壓縮率與字符長度關系同上



免責聲明!

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



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