加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Snappy压缩

发布时间:2020-12-15 07:35:09 所属栏目:Java 来源:网络整理
导读: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.co

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:UsersgujieDesktopjs46818_19279_4547_50.json");?? ??? ?//压缩后文件?? ??? ?File fileWrite = new File("D:UsersgujieDesktopjssnappytest.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;?? ??? ?}?? ?}}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读