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。。。

posted @ 2017-08-16 20:52  ctgu_czy  阅读(3606)  评论(0编辑  收藏  举报