java gzip压缩与解压

 

 

 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


/**
 * 字符串的压缩
 * @param str 待压缩的字符串
 * @return 返回压缩后的字符串
 * @throws IOException
 */
public class GZIP {
    /**
     * 字符串的压缩
     * 
     * @param str
     *            待压缩的字符串
     * @return 返回压缩后的字符串
     * @throws IOException
     */
    public static String compress(String str) throws IOException {
        if (null == str || str.length() <= 0) {
            return str;
        }
        // 创建一个新的 byte 数组输出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // 使用默认缓冲区大小创建新的输出流
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        // 将 b.length 个字节写入此输出流
        gzip.write(str.getBytes());
        gzip.close();
        // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
        return out.toString("ISO-8859-1");
    }

    /**
     * 字符串的解压
     * 
     * @param str
     *            对字符串解压
     * @return 返回解压缩后的字符串
     * @throws IOException
     */
    public static String unCompress(String str) throws IOException {
        if (null == str || str.length() <= 0) {
            return str;
        }
        // 创建一个新的 byte 数组输出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        // 使用默认缓冲区大小创建新的输入流
        GZIPInputStream gzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n = 0;
        while ((n = gzip.read(buffer)) >= 0) {// 将未压缩数据读入字节数组
            // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte数组输出流
            out.write(buffer, 0, n);
        }
        // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
        return out.toString("UTF-8");
    }

    public static void WriteStringToFile(String filePath, String str) {  
        try {  
            RandomAccessFile rf = new RandomAccessFile(filePath, "rw");  
            rf.writeBytes(str);  
            rf.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    
    public static void main(String[] args) throws IOException {
        String str = "[{\"alias\":\"brownHorse\",\"level\":3,\"reward\":18,\"runSpeed\":1.0,\"speciesId\":1,\"uuid\":\"26CB848557CE4757B7D8D80C21730E55\"},{\"alias\":\"brownHorse\",\"level\":3,\"reward\":60,\"runSpeed\":1.0,\"speciesId\":1,\"uuid\":\"7529DAAC831E461D8AE68FBE71891BB7\"},{\"alias\":\"blackHorse\",\"level\":1,\"reward\":116,\"runSpeed\":2.0,\"speciesId\":2,\"uuid\":\"50AA595A6F3F424DA176478CC9A2FB81\"},{\"alias\":\"blackHorse\",\"level\":1,\"reward\":151,\"runSpeed\":2.0,\"speciesId\":2,\"uuid\":\"A8E54431AEAD48B4A5B5C9C5271BC741\"},{\"alias\":\"redHorse\",\"level\":1,\"reward\":286,\"runSpeed\":3.0,\"speciesId\":3,\"uuid\":\"CFA98753E51D45CFA0789C819CB5D51B\"},{\"alias\":\"redHorse\",\"level\":1,\"reward\":232,\"runSpeed\":3.0,\"speciesId\":3,\"uuid\":\"953C03AB07FF4495836DD4FDAE992E16\"}]";
        String res = compress(str);
        System.out.println(res);
        System.out.println(res.length());
        System.out.println(unCompress(res));
        WriteStringToFile("d://a.txt", res);
    }
}

 

posted @ 2018-03-22 09:39  这个名字想了很久~  阅读(500)  评论(0编辑  收藏  举报