import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.io.compress.DeflateCodec;
import org.apache.hadoop.util.ReflectionUtils;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestCompressDemo {
private static Configuration conf;
@BeforeClass
public static void iniConf(){
conf = new Configuration();
}
/**
* 使用deflate压缩算法
*/
@Test
public void compressByDeflate() throws Exception{
//deflate编码器
String codecStr = “org.apache.hadoop.io.compress.DeflateCodec”;
Class<DeflateCodec> clazz = (Class<DeflateCodec>) Class.forName(codecStr);
DeflateCodec codec = ReflectionUtils.newInstance(clazz,conf);
//对输出流包装,产生新的压缩流
FileOutputStream fos = new FileOutputStream(“E:/zhaopian.deflate”);
CompressionOutputStream comOut = codec.createOutputStream(fos);
//写入流
IOUtils.copyBytes(new FileInputStream(“E:/zhaopian.jpg”),comOut,1024);
}
/**
* 使用deflate压缩算法
*/
@Test
public void compressByDeflate2() throws Exception{
//直接实例化codec对象
DeflateCodec codec = new DeflateCodec();
//检查并设置conf对象
ReflectionUtils.setConf(codec,conf);
//对输出流包装,产生新的压缩流
FileOutputStream fos = new FileOutputStream(“E:/zhaopian2.deflate”);
CompressionOutputStream comOut = codec.createOutputStream(fos);
//写入流
IOUtils.copyBytes(new FileInputStream(“E:/zhaopian.jpg”), comOut, 1024);
}
}
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/193419.html