java解压tar.gz_Java解压缩文件tar.gz工具类实现

压缩格式的应用场景:

网络传输数据文件时,单个文件速度比较慢,可以使用tar.gz打包,然后传输,这样大大的提高了传输效率,也保证了整体文件的完整性。

下面是Java语言中解压缩tar.gz文件的实现。

实现工具类:package com.what21.compress.gzip;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.zip.GZIPInputStream;

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

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

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

/**

* 解压缩文件:tar.gz

*/

public class UnGZipUtil {

/**

* @param tarZipSource 源文件

* @param targetDir 目标目录

*/

public static void decompress(String tarZipSource, String targetDir) {

FileInputStream fInput = null;

BufferedInputStream bufInput = null;

ArchiveInputStream archiveInput = null;

try {

// 文件流

fInput = new FileInputStream(tarZipSource);

// 缓冲流

bufInput = new BufferedInputStream(fInput);

// GZIP压缩流

GZIPInputStream gzipInput = new GZIPInputStream(bufInput);

// tar压缩格式(tar类型)

ArchiveStreamFactory aStramFactory = new ArchiveStreamFactory();

archiveInput = aStramFactory.createArchiveInputStream("tar", gzipInput);

// tar压缩文件条目

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

while (entry != null) {

// 条目名称

String entryName = entry.getName();

if(entry.isDirectory()){

// 如果当前条目是目录

createDir(targetDir,entryName,1);

}else if(entry.isFile()){

// 如果当前条目是文件

String fullFileName = createDir(targetDir,entryName,2);

// 输出文件

warpOutput(fullFileName,archiveInput);

}

// 下一个条目

entry = (TarArchiveEntry) archiveInput.getNextEntry();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

close(archiveInput);

close(bufInput);

close(fInput);

}

}

/**

* @param file

* @param input

* @throws IOException

*/

private static void warpOutput(String file,InputStream input)

throws IOException {

FileOutputStream fOuput = new FileOutputStream(file);

BufferedOutputStream bufOutput = new BufferedOutputStream(fOuput);

int b = -1;

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

bufOutput.write(b);

}

bufOutput.flush();

bufOutput.close();

}

/**

* @param input

*/

public static void close(InputStream input){

// 静默关闭处理

try {

if (null != input) {

input.close();

}

input = null;

} catch (IOException e) { }

}

/**

* @param baseDir 根目录

* @param entry 压缩包条目

* @param type 类型:1、目录;2、文件

* @return

*/

public static String createDir(String baseDir,String entry,int type){

// 拆分名称

String[] items = entry.split("/");

String fullFilePath = baseDir;

for (int i = 0; i 

String item = items[i];

fullFilePath = fullFilePath + File.separator + item;

if(type==2){

if(i!=items.length-1){

// 如果目录不存在,就创建

File tmpFile = new File(fullFilePath);

if(!tmpFile.exists()){

tmpFile.mkdir();

}

}

}else{

// 如果目录不存在,就创建

File tmpFile = new File(fullFilePath);

if(!tmpFile.exists()){

tmpFile.mkdir();

}

}

}

// 返回目录全路径

File fullFile = new File(fullFilePath);

return fullFile.getAbsolutePath();

}

/**

* @param args

*/

public static void main(String[] args) {

long start = System.currentTimeMillis();

String base = "D:/Apps/Server/What21FtpServer1.0/Resources/admin/";

UnGZipUtil.decompress(base + "20160818.tar.gz",base);

long end = System.currentTimeMillis();

System.out.println("解压共用了:" + (end - start) + "ms");

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值