Snappy压缩

package demo02.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;

import org.apache.commons.codec.CharEncoding;
import org.xerial.snappy.Snappy;

/**
 * 使用snappy压缩算法压缩文件
 * @author gujie
 *
 */
public class SnappyUtil {

    public static void main(String[] args) throws IOException {
        long time1 = new Date().getTime();
        //输入文件
        File fileread = new File("D:\\Users\\gujie\\Desktop\\js\\46818_19279_4547_50.json");
        //压缩后文件
        File fileWrite = new File("D:\\Users\\gujie\\Desktop\\js\\snappytest.snappy");

        String charEncoding =  CharEncoding.ISO_8859_1;//只能是ISO_8859_1,由byte[]的编码决定
        
        //读取文件
        String readFile = readFile(fileread, charEncoding);
        System.out.println("read during(" + (new Date().getTime() - time1) + ")");
        
        //压缩内容
        long time2 = new Date().getTime();
        byte[] compressHtml = compressHtml(readFile);
        System.out.println("snappy during(" + (new Date().getTime() - time2) + ")");
        
        //存储压缩内容
        time2 = new Date().getTime();
        writeFile(fileWrite, compressHtml, charEncoding);
        System.out.println("snappy save during(" + (new Date().getTime() - time2) + ")");

        //读取压缩文件
        long time3 = new Date().getTime();
        String snappyStr = readFile(fileWrite,charEncoding);
        System.out.println("read snappy during(" + (new Date().getTime() - time3) + ")");
        
        //解压压缩文件内容
        long time4 = new Date().getTime();
        String decompressHtml = decompressHtml(snappyStr.getBytes(charEncoding));
//        System.out.println("dec file:"+decompressHtml);
        System.out.println("decode snappy during(" + (new Date().getTime() - time4) + ")");
        
        //查看压缩比
        long totalSpaceBefore = fileread.length();
        long totalSpaceAfter = fileWrite.length();
        System.out.println("压缩前:" + totalSpaceBefore + "\t压缩后:" + totalSpaceAfter + "\t压缩比:"
                + totalSpaceAfter * 1.0 / totalSpaceBefore);
    }

    /**
     * 写文件接口
     * @param file
     * @param bytes
     * @param encodeing
     * @throws IOException
     */
    public static void writeFile(File file, byte[] bytes, String encodeing) throws IOException {
        OutputStreamWriter op = new OutputStreamWriter(new FileOutputStream(file), encodeing);
        op.append(new String(bytes,encodeing));
        op.flush();
        op.close();
    }
    
    /**
     * 读文件接口
     * @param file
     * @param encodeing
     * @return
     */
    public static String readFile(File file, String encodeing) {
        StringBuilder stringBuffer = new StringBuilder();
        try {
            byte [] fileBytes = Files.readAllBytes(Paths.get(file.getPath()));
            return new String(fileBytes,encodeing);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuffer.toString();
    }

    /**
     * 压缩字符串
     * @param html
     * @return
     */
    public static byte[] compressHtml(String html) {
        try {
            return Snappy.compress(html.getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解压字节数组
     * @param bytes
     * @return
     */
    public static String decompressHtml(byte[] bytes) {
        try {
            return new String(Snappy.uncompress(bytes));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

转载于:https://www.cnblogs.com/blueshy/p/11586357.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值