java lz4压缩工具类
来源:互联网 发布:淘宝客佣金在哪设置 编辑:程序博客网 时间:2024/06/05 06:19
package com.beini.test.javase.compress;import net.jpountz.lz4.*;import java.io.*;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;/** * Created by beini on 2017/10/30. * lz4:https://github.com/lz4/lz4-java */public class Lz4Util { /** * @param srcByte 原始数据 * @return 压缩后的数据 */ public static byte[] compressedByte(byte[] srcByte) { LZ4Factory factory = LZ4Factory.fastestInstance(); LZ4Compressor compressor = factory.fastCompressor(); return compressor.compress(srcByte); } /** * @param compressorByte 压缩后的数据 * @param srcLength 压缩前的数据长度 * @return */ public static byte[] decompressorByte(byte[] compressorByte, int srcLength) { LZ4Factory factory = LZ4Factory.fastestInstance(); LZ4FastDecompressor decompressor = factory.fastDecompressor(); return decompressor.decompress(compressorByte, srcLength); } /** * @param srcByte * @param blockSize 一次压缩的大小 取值范围 64 字节-32M之间 * @return * @throws IOException */ public static byte[] lz4Compress(byte[] srcByte, int blockSize) throws IOException { LZ4Factory factory = LZ4Factory.fastestInstance(); ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); LZ4Compressor compressor = factory.fastCompressor(); LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream(byteOutput, blockSize, compressor); compressedOutput.write(srcByte); compressedOutput.close(); return byteOutput.toByteArray(); } /** * @param compressorByte * @param blockSize 一次压缩的大小 取值范围 64 字节-32M之间 * @return * @throws IOException */ public static byte[] lz4Decompress(byte[] compressorByte, int blockSize) throws IOException { LZ4Factory factory = LZ4Factory.fastestInstance(); ByteArrayOutputStream baos = new ByteArrayOutputStream(blockSize); LZ4FastDecompressor decompresser = factory.fastDecompressor(); LZ4BlockInputStream lzis = new LZ4BlockInputStream(new ByteArrayInputStream(compressorByte), decompresser); int count; byte[] buffer = new byte[blockSize]; while ((count = lzis.read(buffer)) != -1) { baos.write(buffer, 0, count); } lzis.close(); return baos.toByteArray(); } /** * File to byte[] * * @param filePath * @return * @throws IOException */ public static byte[] returnFileByte(String filePath) throws IOException { FileInputStream fileInputStream = new FileInputStream(new File(filePath)); FileChannel channel = fileInputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size()); channel.read(byteBuffer); return byteBuffer.array(); } /** * createFile * * @param fileByte * @param filePath */ public static void createFile(byte[] fileByte, String filePath) { BufferedOutputStream bufferedOutputStream; FileOutputStream fileOutputStream; File file = new File(filePath); try { fileOutputStream = new FileOutputStream(file); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); bufferedOutputStream.write(fileByte); fileOutputStream.close(); bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }}
阅读全文
1 0
- java lz4压缩工具类
- java 图片压缩工具类
- java上传图片压缩工具类
- 使用JAVA编写zip压缩工具类
- 关于压缩工具类ZipCompressor.java的说明
- JAVA实现zip包压缩工具类
- Java之文件压缩工具类-yellowcong
- 压缩工具类
- java 图片压缩工具
- JAVA图片压缩工具
- Zip压缩工具类
- android 图片压缩工具类
- 多功能图片压缩工具类
- 多功能图片压缩工具 类
- ZipUtils-压缩工具类
- Android图片压缩工具类
- Android图片压缩工具类
- 文件压缩工具类
- Util工具类 跨Oracle、MySQL通用分页查询
- iOS算法总结-快速排序
- HTML常用标签
- 波纹按钮点击特效
- Java经典基础练习1-10
- java lz4压缩工具类
- Jetson TX2 开发记录
- Android查看应用包名package和launcher_activity名称的方法
- jzoj5436 【NOIP2017提高A组集训10.30】Group
- Integer与int的种种比较你知道多少?
- MPLS——多协议标签交换
- Android自定义View基础之Canvas、Paint介绍
- 容器set和map的使用
- 简单的SSO登陆系统