贴一段java读取hdfs 解压gz zip tar.gz保存到hdfs的代码

package main.java;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.*;

import org.apache.commons.compress.archivers.ArchiveException;

import org.apache.commons.compress.archivers.ArchiveInputStream;

import org.apache.commons.compress.archivers.ArchiveStreamFactory;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;

import java.io.IOException;
import java.net.URI;

import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;;

/**

* 解压tar.gz  zip  gz文件包 这里的数据源和输出目录都为HDFS

*

*/

public class GZipHdfs {

private BufferedOutputStream bufferedOutputStream;
   String zipfileName = null;

public GZipHdfs(String fileName) {

this.zipfileName = fileName;

}

/*

* 执行入口,rarFileName为需要解压的文件路径(具体到文件),destDir为解压目标路径  路径为HDFS

*/

public List<String> unTargzFile(String rarFileName, String destDir) throws IOException {

GZipHdfs GZipHdfs = new GZipHdfs(rarFileName);
       Configuration conf = new Configuration();
       FileSystem fs = FileSystem.get(URI.create(destDir), conf);
       boolean result = fs.isDirectory(new Path(destDir));
       if (!result) {
           fs.mkdirs(new Path(destDir));
       }
       String outputDirectory = destDir;

List<String> r = GZipHdfs.defUnTargzFile(outputDirectory, fs);
       fs.close();
       return r;

}

public List<String> defUnTargzFile(String outputDirectory, FileSystem fs) {

FileInputStream fis = null;
       ArchiveInputStream in = null;
       BufferedInputStream bufferedInputStream = null;
       List<String> tarList = new LinkedList<String>();

try {

FSDataInputStream hdfsInputStream = fs.open(new Path(zipfileName));

GZIPInputStream is = new GZIPInputStream(new BufferedInputStream(
                   hdfsInputStream));

in = new ArchiveStreamFactory().createArchiveInputStream("tar", is);

bufferedInputStream = new BufferedInputStream(in);

TarArchiveEntry entry = (TarArchiveEntry) in.getNextEntry();

while (entry != null) {

String name = entry.getName();

String[] names = name.split("/");

String fileName = outputDirectory;

for (int i = 0; i < names.length; i++) {

String str = names[i];

fileName = fileName + "/" + str;

}

FSDataOutputStream hdfsOutStream = fs.create(new Path(fileName));

bufferedOutputStream = new BufferedOutputStream(
                       hdfsOutStream);

int b;

while ((b = bufferedInputStream.read()) != -1) {

bufferedOutputStream.write(b);

}

bufferedOutputStream.flush();

bufferedOutputStream.close();

entry = (TarArchiveEntry) in.getNextEntry();

tarList.add(name);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (ArchiveException e) {

e.printStackTrace();

} finally {

try {

if (bufferedInputStream != null) {

bufferedInputStream.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}
       return tarList;

}
   /*

* 执行入口,rarFileName为需要解压的文件路径(具体到文件),destDir为解压目标路径  路径为HDFS

*/

public List<String> unZipFile(String rarFileName, String destDir) throws IOException {

GZipHdfs GZipHdfs = new GZipHdfs(rarFileName);
       Configuration conf = new Configuration();

FileSystem fs = FileSystem.get(URI.create(destDir), conf);
       boolean result = fs.isDirectory(new Path(destDir));
       if (!result) {
           fs.mkdirs(new Path(destDir));
       }
       String outputDirectory = destDir;

List<String> r = GZipHdfs.defUnZipFile(outputDirectory, fs);
       fs.close();
       return r;

}

public List<String> defUnZipFile(String outputDirectory, FileSystem fs) {

FileInputStream fis = null;
       ArchiveInputStream in = null;
       BufferedInputStream bufferedInputStream = null;
       List<String> zipList = new LinkedList<String>();

try {

FSDataInputStream hdfsInputStream = fs.open(new Path(zipfileName));
           ZipInputStream is = new ZipInputStream(new BufferedInputStream(
                   hdfsInputStream));

bufferedInputStream = new BufferedInputStream(is);
           ZipEntry entry =is.getNextEntry();

while (entry != null) {

String name = entry.getName();

String[] names = name.split("/");

String fileName = outputDirectory;

for (int i = 0; i < names.length; i++) {

String str = names[i];

fileName = fileName + "/" + str;

}

FSDataOutputStream hdfsOutStream = fs.create(new Path(fileName));

bufferedOutputStream = new BufferedOutputStream(
                       hdfsOutStream);

int b;

while ((b = bufferedInputStream.read()) != -1) {

bufferedOutputStream.write(b);

}

bufferedOutputStream.flush();

bufferedOutputStream.close();

entry = (ZipEntry) is.getNextEntry();

zipList.add(name);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bufferedInputStream != null) {

bufferedInputStream.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}
       return zipList;

}
   /*

* 执行入口,rarFileName为需要解压的文件路径(具体到文件),destDir为解压目标路径  路径为HDFS

*/

public List<String> unGZipFile(String rarFileName, String destDir) throws IOException {

GZipHdfs GZipHdfs = new GZipHdfs(rarFileName);
       Configuration conf = new Configuration();
       FileSystem fs = FileSystem.get(URI.create(destDir), conf);
       boolean result = fs.isDirectory(new Path(destDir));
       if (!result) {
           fs.mkdirs(new Path(destDir));
       }
       String outputDirectory = destDir;

List<String> r = GZipHdfs.defUnGZipFile(outputDirectory, fs);
       fs.close();
       return r;

}

public List<String> defUnGZipFile(String outputDirectory, FileSystem fs) {

FileInputStream fis = null;
       ArchiveInputStream in = null;
       BufferedInputStream bufferedInputStream = null;
       List<String> tarList = new LinkedList<String>();

try {

FSDataInputStream hdfsInputStream = fs.open(new Path(zipfileName));

GzipCompressorInputStream is = new GzipCompressorInputStream(new BufferedInputStream(
                   hdfsInputStream));
           bufferedInputStream = new BufferedInputStream(is);

String[] nameList = zipfileName.split("/");
           String name=nameList[nameList.length-1].replace(".gz","");
           String fileName = outputDirectory+"/"+name;
           FSDataOutputStream hdfsOutStream = fs.create(new Path(fileName));
           bufferedOutputStream = new BufferedOutputStream(
                   hdfsOutStream);
           int b;
           while ((b = bufferedInputStream.read()) != -1) {
               bufferedOutputStream.write(b);

}
           bufferedOutputStream.flush();
           bufferedOutputStream.close();
           tarList.add(name);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bufferedInputStream != null) {

bufferedInputStream.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}
       return tarList;

}

}

原文地址:http://blog.51cto.com/qidai/2157056

时间: 2024-08-11 01:21:38

贴一段java读取hdfs 解压gz zip tar.gz保存到hdfs的代码的相关文章

linux 常用命令 – 解压文件(zip,rar,gz,tar.gz)

Linux下自带了一个unzip的程序可以解压缩文件, 解压命令是:unzip filename.zip 同样也提供了一个zip程序压缩zip文件,命令是 zip filename.zip files 会将files压缩到filename.zip 另外看看你的文件的后缀名,不同的后缀的文件解压和压缩的命令都不一样 总结一下 1.*.tar 用 tar –xvf 解压 2.*.gz 用 gzip -d或者gunzip 解压 3.*.tar.gz和*.tgz 用 tar –xzf 解压 4.*.bz

java解压缩.gz .zip .tar.gz等格式的压缩包方法总结

1..gz文件是linux下常见的压缩格式.使用 java.util.zip.GZIPInputStream即可,压缩是 java.util.zip.GZIPOutputStream 1 public static void unGzipFile(String sourcedir) { 2 String ouputfile = ""; 3 try { 4 //建立gzip压缩文件输入流 5 FileInputStream fin = new FileInputStream(source

java压缩文件解压:调用WinRAR5命令强于自己写代码实现

最近,手上维护着一个几年前的系统,技术是用的JSP+Strust2,系统提供了rar和zip两种压缩格式的解压功能,后台是用java实现的 1.解压rar格式,采用的是java-unrar-0.3.jar 2.解压zip格式,采用的是commons-compress-1.4.1.jar 但最近根据用户反馈的问题,发现系统存在两个关于压缩文件解压的问题: 1.有些压缩文件解压之后出现中文乱码: 2.有些压缩文件根本不能解压 为了弥补上述两个问题,在之前代码的基础上打了一些补丁,来解决zip压缩包乱

Linux中下载、解压、安装.tar.gz文件

一.将解压包发送到linux服务器上: 1.在windos上下载好.tar.gz文件后,通过winscp等SFTP客户端传送给linux 2.在linux中通过wget命令直接下载 #wget [选项] [下载地址] wget常用参数: -b:后台下载(默认下载到当前目录) -O:用自定义的名字保存下载文件.下载下来的文件默认会用“下载地址的最后一个“/”符号后面的字符串来命名”,而我们可以使用“-O 新文件名” 来重新命名. -limit-rate:限速下载,如wget --limit-rat

linux commands - 一次性解压多个tar.gz文件

1 echo *.tar.gz | xargs -n 1 tar -zxvf list所有tar.gz文件,然后利用xargs将其作为参数传给tar命令.-n 1表示每次传一个参数. xargs: https://www.cnblogs.com/wangqiguo/p/6464234.html 阅读目录 为什么要用xargs,问题的来源 xargs是什么,与管道有什么不同 xargs的一些有用的选项 回到顶部 为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的

linux一次性解压多个.gz或者.tar.gz文件

对于解压多个.gz文件的,用此命令: for gz in *.gz; do gunzip $gz; done 对于解压多个.tar.gz文件的,用下面命令: for tar in *.tar.gz; do tar xvf $tar; done

【Linux命令】linux一次性解压多个.gz或者.tar.gz文件

原文:linux一次性解压多个.gz或者.tar.gz文件 解压多个压缩包 对于解压多个.gz文件的,用此命令: for gz in *.gz; do gunzip $gz; done 对于解压多个.tar.gz文件的,用下面命令: for tar in *.tar.gz; do tar xvf $tar; done 扩展:tar命令 tar [-] A --catenate --concatenate | c --create | d --diff --compare | --delete |

在 Java 项目中解压7Zip特殊压缩算法文件

1 问题描述 Java Web 后端下载了一个经特殊算法压缩的 zip 文件,因为不能采用 java 本身自带的解压方式,必须采用 7Zip 来解压.所以,提到了本文中在 java web 后端调用外部 7zip exe 来解压文件的问题. 2 主要实现 2.1 定义缓冲区类 class StreamGobbler extends Thread { InputStream is; String type; public StreamGobbler(InputStream is, String t

java 压缩以及解压文件,有tar,zip,gz(gizp)和解压

package com.yabsz.decompCompr; import java.io.File; import java.util.ArrayList; import java.util.List; public class main { public static void main(String[] args) { //需要下载commons-net-ftp-2.0.jar包下载地址:http://download.csdn.net/detail/u010696272/8006739