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);
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。