packagecom。beini。test。javase。compress; importnet。jpountz。lz4。; importjava。io。; importjava。nio。ByteBuffer; importjava。nio。channels。FileChannel; Createdbybeinion20171030。 lz4:https:github。comlz4lz4java publicclassLz4Util{ paramsrcByte原始数据 return压缩后的数据 publicstaticbyte〔〕compressedByte(byte〔〕srcByte){ LZ4FactoryfactoryLZ4Factory。fastestInstance(); LZ4Compressorcompressorfactory。fastCompressor(); returncompressor。compress(srcByte); } paramcompressorByte压缩后的数据 paramsrcLength压缩前的数据长度 return publicstaticbyte〔〕decompressorByte(byte〔〕compressorByte,intsrcLength){ LZ4FactoryfactoryLZ4Factory。fastestInstance(); LZ4FastDecompressordecompressorfactory。fastDecompressor(); returndecompressor。decompress(compressorByte,srcLength); } paramsrcByte paramblockSize一次压缩的大小取值范围64字节32M之间 return throwsIOException publicstaticbyte〔〕lz4Compress(byte〔〕srcByte,intblockSize)throwsIOException{ LZ4FactoryfactoryLZ4Factory。fastestInstance(); ByteArrayOutputStreambyteOutputnewByteArrayOutputStream(); LZ4Compressorcompressorfactory。fastCompressor(); LZ4BlockOutputStreamcompressedOutputnewLZ4BlockOutputStream(byteOutput,blockSize,compressor); compressedOutput。write(srcByte); compressedOutput。close(); returnbyteOutput。toByteArray(); } paramcompressorByte paramblockSize一次压缩的大小取值范围64字节32M之间 return throwsIOException publicstaticbyte〔〕lz4Decompress(byte〔〕compressorByte,intblockSize)throwsIOException{ LZ4FactoryfactoryLZ4Factory。fastestInstance(); ByteArrayOutputStreambaosnewByteArrayOutputStream(blockSize); LZ4FastDecompressordecompresserfactory。fastDecompressor(); LZ4BlockInputStreamlzisnewLZ4BlockInputStream(newByteArrayInputStream(compressorByte),decompresser); intcount; byte〔〕buffernewbyte〔blockSize〕; while((countlzis。read(buffer))!1){ baos。write(buffer,0,count); } lzis。close(); returnbaos。toByteArray(); } Filetobyte〔〕 paramfilePath return throwsIOException publicstaticbyte〔〕returnFileByte(StringfilePath)throwsIOException{ FileInputStreamfileInputStreamnewFileInputStream(newFile(filePath)); FileChannelchannelfileInputStream。getChannel(); ByteBufferbyteBufferByteBuffer。allocate((int)channel。size()); channel。read(byteBuffer); returnbyteBuffer。array(); } createFile paramfileByte paramfilePath publicstaticvoidcreateFile(byte〔〕fileByte,StringfilePath){ BufferedOutputStreambufferedOutputStream; FileOutputStreamfileOutputStream; FilefilenewFile(filePath); try{ fileOutputStreamnewFileOutputStream(file); bufferedOutputStreamnewBufferedOutputStream(fileOutputStream); bufferedOutputStream。write(fileByte); fileOutputStream。close(); bufferedOutputStream。close(); }catch(IOExceptione){ e。printStackTrace(); } } }