自定义使用javaAPI进行文件的解压缩
作者:互联网
使用javaAPI进行文件的解压缩
前言
二、代码
代码如下(示例):
package com.atguigu.mr.test;
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.InputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
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 org.junit.Test;
public class TestCompression {
@Test
public void testcompression() throws ClassNotFoundException, IOException {
//解压缩:调用Compressioncodec.createcompressioninputsream 返回一个可以解压缩的输入流
//压缩:调用Compressioncodec.createcompressionoutputsream 返回一个可以压缩的输出流
Path file=new Path("D:/悲惨世界.txt");
String codecClassName="org.apache.hadoop.io.compress.GzipCodec";
Class<?> codecClass = Class.forName(codecClassName);
Configuration conf = new Configuration();
FileSystem fs=FileSystem.get(conf);
FSDataInputStream inputStream = fs.open(file);
//确定使用哪种压缩格式compressioncodec
CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
FSDataOutputStream outputStream = fs.create(new Path("D:/file"+codec.getDefaultExtension()), true);
//带压缩的输出流
CompressionOutputStream createOutputStream = codec.createOutputStream(outputStream);
IOUtils.copyBytes(inputStream, createOutputStream, conf, true);
}
@Test
public void testdecompression() throws Exception {
Path file=new Path("D:/file.gz");
Configuration conf = new Configuration();
//根据后缀获取文件的额压缩格式
CompressionCodec codec = new CompressionCodecFactory(conf).getCodec(file);
//创建一个可以解压缩的输入流
FileSystem fs=FileSystem.get(conf);
FSDataInputStream is = fs.open(file);
CompressionInputStream createInputStream = codec.createInputStream(is);
//创建一个非压缩的输出流
FSDataOutputStream outputstream = fs.create(new Path("D:/file.txt"), true);
IOUtils.copyBytes(createInputStream, outputstream, conf, true);
}
}
在这里插入图片描述
标签:fs,自定义,javaAPI,解压缩,hadoop,conf,org,apache,import 来源: https://blog.csdn.net/m0_46541838/article/details/121467303