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

 

 

 

為了做個比較,下面貼出gzip方式壓縮和解壓縮的demo(網上扣的,不做保證能運行)
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

// 將一個字符串按照zip方式壓縮和解壓縮
public class ZipUtil {

  // 壓縮
  public static String compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
      return str;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    return out.toString("ISO-8859-1");
  }

  // 解壓縮
  public static String uncompress(String str) throws IOException {
    if (str == null || str.length() == 0) {
      return str;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(str
        .getBytes("ISO-8859-1"));
    GZIPInputStream gunzip = new GZIPInputStream(in);
    byte[] buffer = new byte[256];
    int n;
    while ((n = gunzip.read(buffer)) >= 0) {
      out.write(buffer, 0, n);
    }
    // toString()使用平台默認編碼,也可以顯式的指定如toString("GBK")
    return out.toString();
  }

  // 測試方法
  public static void main(String[] args) throws IOException {
    System.out.println(ZipUtil.uncompress(ZipUtil.compress("中國China")));
  }

}

 






over。。。


免責聲明!

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



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