`

java字符串压缩

    博客分类:
  • JAVA
 
阅读更多

采用jdk自带的gzip和zip方法进行压缩。并用apache common 的base64进行压缩字符串的转码。base64的转码需要导入apache的codec包,不要使用sun的base64转码包。

 

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; 
import org.apache.commons.codec.binary.Base64; 
/** 
 * author:mujunfeng
 * desc:字符串压缩/解压工具,提供gzip和zip两种方式,压缩后的字符串使用base64转码 
 * */
 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 String(new Base64().encode(out.toByteArray())); 
    } 
  
    /** 
     * 使用gzip进行解压缩 
     * @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 Base64().decode(compressedStr.getBytes()); 
            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 String(new Base64().encode(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 compressed 
     *            压缩后的文本 
     * @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 Base64().decode(compressedStr.getBytes()); 
            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; 
    } 
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics