首页 > 编程语言 > 详细

java 压缩和解压

时间:2021-03-03 22:29:27      阅读:5      评论:0      收藏:0      [点我收藏+]
package CompressionApp;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.*;

public class compressApp {
public static void main(String[] args) throws Exception{
//压缩
compression("data/access.log","org.apache.hadoop.io.compress.BZip2Codec");
//解压
decompression("data/access.log.bz2") ;
}

public static void compression(String path,String method) throws IOException, ClassNotFoundException {
    FileInputStream path1 = new FileInputStream(new File(path));
    Class<?> aClass = Class.forName(method) ;
    CompressionCodec code = (CompressionCodec) ReflectionUtils.newInstance(aClass, new Configuration());
    FileOutputStream fos = new FileOutputStream(new File(path + code.getDefaultExtension()));
    CompressionOutputStream outputStream = code.createOutputStream(fos);
    IOUtils.copyBytes(path1,outputStream,1024);
    path1.close();
    outputStream.close();
    fos.close();

}

public static void decompression(String path) throws IOException {
    CompressionCodecFactory Factory = new CompressionCodecFactory(new Configuration());
    CompressionCodec codec = Factory.getCodec(new Path(path));
    if(codec == null){
        System.out.println("没有对应的压缩格式");
        return;
    }
    CompressionInputStream inputStream = codec.createInputStream(new FileInputStream(new File(path)));
    FileOutputStream fileOutputStream = new FileOutputStream(new File(path + "jieya"));
     IOUtils.copyBytes(inputStream,fileOutputStream,1024,false);
    fileOutputStream.close();
     inputStream.close();

}

}

java 压缩和解压

原文:https://blog.51cto.com/15084467/2645903

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号