java-實現字符串先base64解碼再zip解壓

一、java base64編碼解碼:

Base64編碼解碼已經加入Java 8 類庫的標準。
使用文檔:https://www.runoob.com/java/java8-base64.html

編碼解碼示例:

import java.util.Base64;
import java.util.Base64.Decoder;

public class Test {
  public static void main(String[] args) throws Exception {
    String str = "鋤禾日當午,汗滴禾下土。\n誰知盤中餐,粒粒皆辛苦。";
    
    String base64_1 = Base64.getEncoder().encodeToString(str.getBytes("utf-8"));
    System.out.println(base64_1);
    System.out.println("*******");
    
    String base64_2 = Base64.getMimeEncoder().encodeToString(str.getBytes("utf-8"));
    System.out.println(base64_2);
    System.out.println("*******");

    Decoder decoder = Base64.getMimeDecoder();
    
    byte[] bs_1 = decoder.decode(base64_1);
    System.out.println(new String(bs_1, "utf-8"));
    System.out.println("*******");
    
    byte[] bs_2 = decoder.decode(base64_2);
    System.out.println(new String(bs_2, "utf-8"));
  }
}

打印結果:

6ZSE56a+5pel5b2T5Y2I77yM5rGX5ru056a+5LiL5Zyf44CCCuiwgeefpeebmOS4remkkO+8jOeykueykueahui+m+iLpuOAgg==
*******
6ZSE56a+5pel5b2T5Y2I77yM5rGX5ru056a+5LiL5Zyf44CCCuiwgeefpeebmOS4remkkO+8jOey
kueykueahui+m+iLpuOAgg==
*******
鋤禾日當午,汗滴禾下土。
誰知盤中餐,粒粒皆辛苦。
*******
鋤禾日當午,汗滴禾下土。
誰知盤中餐,粒粒皆辛苦。

原文地址:https://blog.csdn.net/xuejianbest/article/details/93721968

二、java採用zip方式實現String的壓縮和解壓縮CompressStringUtil類

CompressStringUtil類:
/**
     * 壓縮
     *
     * @param paramString
     * @return
     */
    public static final byte[] compress(String paramString) throws Exception {
        if (paramString == null)
            return null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        ZipOutputStream zipOutputStream = null;
        byte[] arrayOfByte;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
            zipOutputStream.putNextEntry(new ZipEntry("0"));
            zipOutputStream.write(paramString.getBytes("GBK"));//這裏採用gbk方式壓縮,如果採用編譯器默認的utf-8,這裏就直接getByte();
            zipOutputStream.closeEntry();
            arrayOfByte = byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            arrayOfByte = null;
            throw new Exception("壓縮字符串數據出錯", e);
        } finally {
            if (zipOutputStream != null)
                try {
                    zipOutputStream.close();
                } catch (IOException e) {
                    LOGGER.debug("關閉zipOutputStream出錯", e);
                }
            if (byteArrayOutputStream != null)
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    LOGGER.error("關閉byteArrayOutputStream出錯", e);
                }
        }
        return arrayOfByte;
    }
/**
     * 解壓縮
     *
     * @param compressed
     * @return
     */
    public static String decompress(byte[] compressed) throws Exception {
        if (compressed == null)
            return null;
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed;
        try {
            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("GBK");//相應的這裏也要採用gbk方式解壓縮,如果採用編譯器默認的utf-8,這裏就直接toString()就ok了
        } catch (IOException e) {
            decompressed = null;
            throw new Exception("解壓縮字符串數據出錯", e);
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException e) {
                    LOGGER.debug("關閉ZipInputStream出錯", e);
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    LOGGER.error("關閉ByteArrayInputStream出錯", e);
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    LOGGER.debug("關閉ByteArrayOutputStream出錯", e);
                }
            }
        }
        return decompressed;
    }

測試:

public static void main(String[] args) throws Exception {
        byte[] aa = compress("czy愛wt");
        System.out.println("壓縮後字段" + aa);
        System.out.println("解壓縮後字段" + decompress(aa));
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章