java BASE64Encoder加密解密

/*
     * 数据GZIP 解压
     */
    public static String uncompress(String data) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
        ByteArrayInputStream input = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(data));  
        GZIPInputStream gzinpt = new GZIPInputStream(input);
        byte[] buf = new byte[1024];
        int number = 0;
        while((number = gzinpt.read(buf)) != -1){
            output.write(buf,0,number);
        }
        gzinpt.close();
        input.close();
        String result = new String(output.toString("UTF-8"));
        output.close();
        return result;  
    }
    
    /**
     * 数据GZIP 压缩
     *
     * @param str
     * @return
     * @throws IOException
     */
    public static String compress(String s){
        ByteArrayInputStream input;
        String result = "";
        try {
            input = new ByteArrayInputStream(s.getBytes("UTF-8"));
            ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
            GZIPOutputStream gzout = new GZIPOutputStream(output);
            byte[] buf = new byte[1024];
            int number;
            while ((number = input.read(buf)) != -1) {
                gzout.write(buf, 0, number);
            }
            gzout.close();
            input.close();
            result = new BASE64Encoder().encode(output.toByteArray());
            output.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

posted @ 2012-06-26 16:09  流星焱雨  阅读(939)  评论(0编辑  收藏  举报