欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

google snapy 压缩文件

程序员文章站 2024-01-16 14:44:52
...

引入 google snappy 包

   github 上snappy地址 http://github.com/google/snappy

<dependency>
	<groupId>org.xerial.snappy</groupId>
	<artifactId>snappy-java</artifactId>
	<version>1.1.2.6</version>
</dependency>

 使用snappy压缩文件

import java.io.ByteArrayOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.xerial.snappy.Snappy;

public class SnappyDemo {

	public static void main(String[] args) throws Exception {
		// 待压缩的文件,可以是任意文件
		RandomAccessFile raf = new RandomAccessFile("c:/花好月圆夜.mp4", "r");
                //压缩后保存的文件
		RandomAccessFile w = new RandomAccessFile("c:/cas-server-3.5.2-master.snappy", "rw");
		FileChannel channel = raf.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(4096);
		ByteArrayOutputStream out = new ByteArrayOutputStream(1024); 
		while (channel.read(buffer) > -1) {
			buffer.flip() ;
			
			while(buffer.hasRemaining()){
				out.write( buffer.get() ); 
			}
			buffer.clear() ;
		}
                //压缩内容写入 文件,如果文件比较大 压缩后的文件可以分多个文件保存,
               // 例如hadoop 文件每个块自定义为128M
		w.write( Snappy.compress(out.toByteArray()) ); 
		raf.close();
		w.close();
		out.close(); 
	}
}

 

使用snappy解压缩

import java.io.ByteArrayOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.xerial.snappy.Snappy;

public class UnSnappyDemo {

	public static void main(String[] args) throws Exception {
		RandomAccessFile raf = new RandomAccessFile("c:/cas-server-3.5.2-master.snappy", "r");
		RandomAccessFile w = new RandomAccessFile("c:/cas-server-3.5.2-master.zip", "rw");
		FileChannel channel = raf.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(196);
		ByteArrayOutputStream out = new ByteArrayOutputStream(1024);  
		while (channel.read(buffer) > -1){
			buffer.flip() ;
			while(buffer.hasRemaining()){
				out.write( buffer.get() ); 
			}
			buffer.clear() ;
			buffer = ByteBuffer.allocate(196);
		}
		w.write( Snappy.uncompress(out.toByteArray()) );
		raf.close();
		w.close(); 
	}
}

 

 

相关标签: google snappy