其他分享
首页 > 其他分享> > Snappy压缩

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;
        }
    }
}

标签:return,String,getTime,压缩,Snappy,import,Date,new
来源: https://www.cnblogs.com/blueshy/p/11586357.html