java怎么拦截某个对象
241
2022-11-25
java 压缩和解压
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(); }
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~