使用hadoop的压缩方式进行压缩和解压

压缩算法及其编码/解码器

压缩格式对应的编码/解码器
DEFLATEorg.apache.hadoop.io.compress.DefaultCodec
gziporg.apache.hadoop.io.compress.GzipCodec
bziporg.apache.hadoop.io.compress.BZip2Codec
Snappyorg.apache.hadoop.io.compress.SnappyCodec

压缩过程实现:
接受一个字符串参数,用于指定编码/解码器,使用反射机制创建对应的并对相应的编码解码对象,对文件进行压缩。

public static  void  compress(String method) throws ClassNotFoundException, IOException {
        File fileIn = new File("adult.data");
        //输入流
        FileInputStream in = new FileInputStream(fileIn);
        Class<?> codecClass = Class.forName(method);
        Configuration conf = new Configuration();
        //通过名称找对应的编码/解码器
        CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
        File fileOut = new File("adult.data" + codec.getDefaultExtension());
        fileOut.delete();
        //文件输出流
        FileOutputStream out = new FileOutputStream(fileOut);
        //通过编码/解码器创建对应的输出流
        CompressionOutputStream cout = codec.createOutputStream(out);
        //压缩
        IOUtils.copyBytes(in,cout,4096,false);
        in.close();
        cout.close();
    }

解压缩过程实现:
解压文件时,通常通过指定其拓展名来推断解码器。

public static void decompress(File file) throws IOException {
        Configuration conf = new Configuration();
        CompressionCodecFactory factory = new CompressionCodecFactory(conf);
        //通过文件拓展名获得相应的编码/解码器
        CompressionCodec codec = factory.getCodec(new Path(file.getName()));
        if(codec == null){
            System.out.println("Cannot find codec for file " + file);
        }
        File fileOut = new File(file.getName());
        //通过编码/解码器创建对应的输入流
        CompressionInputStream in = codec.createInputStream(new FileInputStream(file));
        FileOutputStream out = new FileOutputStream(new File("adult.data.decompress"));
        IOUtils.copyBytes(in,out,4096,false);
        in.close();
        out.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值