java tar.gz_Java压缩解压(tar.gz)

package com.hxm.learn.util;

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

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

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

import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;

import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;

import java.io.*;

/**

* @ClassName FileUtil

* @Description {@link 'https://blog.csdn.net/u013066244/article/details/72783575'}

* @Author hxm

* @Date 2020-02-29 00:22

**/

public class FileUtil {

/**

* 归档

* @param entry

* @throws IOException

* @author yutao

* @return

* @date 2017年5月27日下午1:48:23

*/

private static String archive(String entry) throws IOException {

File file = new File(entry);

TarArchiveOutputStream

tos = new TarArchiveOutputStream(new FileOutputStream(file.getAbsolutePath() + ".tar"));

String base = file.getName();

if(file.isDirectory()){

archiveDir(file, tos, base);

}else{

archiveHandle(tos, file, base);

}

tos.close();

return file.getAbsolutePath() + ".tar";

}

/**

* 递归处理,准备好路径

* @param file

* @param tos

* @param basePath

* @throws IOException

* @author yutao

* @date 2017年5月27日下午1:48:40

*/

private static void archiveDir(File file, TarArchiveOutputStream tos, String basePath) throws IOException {

File[] listFiles = file.listFiles();

for(File fi : listFiles){

if(fi.isDirectory()){

archiveDir(fi, tos, basePath + File.separator + fi.getName());

}else{

archiveHandle(tos, fi, basePath);

}

}

}

/**

* 具体归档处理(文件)

* @param tos

* @param fi

* @param basePath

* @throws IOException

* @author yutao

* @date 2017年5月27日下午1:48:56

*/

private static void archiveHandle(TarArchiveOutputStream tos, File fi, String basePath) throws IOException {

TarArchiveEntry tEntry = new TarArchiveEntry(basePath + File.separator + fi.getName());

tEntry.setSize(fi.length());

tos.putArchiveEntry(tEntry);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fi));

byte[] buffer = new byte[1024];

int read = -1;

while((read = bis.read(buffer)) != -1){

tos.write(buffer, 0 , read);

}

bis.close();

tos.closeArchiveEntry();//这里必须写,否则会失败

}

/**

* 把tar包压缩成gz

* @param path

* @throws IOException

* @author yutao

* @return

* @date 2017年5月27日下午2:08:37

*/

public static String compressArchive(String path) throws IOException{

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));

GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(path + ".gz")));

byte[] buffer = new byte[1024];

int read = -1;

while((read = bis.read(buffer)) != -1){

gcos.write(buffer, 0, read);

}

gcos.close();

bis.close();

return path + ".gz";

}

/**

* 解压

* @param archive

* @author yutao

* @throws IOException

* @date 2017年5月27日下午4:03:29

* */

private static void unCompressArchiveGz(String archive) throws IOException {

File file = new File(archive);

BufferedInputStream bis =

new BufferedInputStream(new FileInputStream(file));

String fileName =

file.getName().substring(0, file.getName().lastIndexOf("."));

String finalName = file.getParent() + File.separator + fileName;

BufferedOutputStream bos =

new BufferedOutputStream(new FileOutputStream(finalName));

GzipCompressorInputStream gcis =

new GzipCompressorInputStream(bis);

byte[] buffer = new byte[1024];

int read = -1;

while((read = gcis.read(buffer)) != -1){

bos.write(buffer, 0, read);

}

gcis.close();

bos.close();

unCompressTar(finalName);

}

/**

* 解压tar

* @param finalName

* @author yutao

* @throws IOException

* @date 2017年5月27日下午4:34:41

*/

private static void unCompressTar(String finalName) throws IOException {

File file = new File(finalName);

String parentPath = file.getParent();

TarArchiveInputStream tais =

new TarArchiveInputStream(new FileInputStream(file));

TarArchiveEntry tarArchiveEntry = null;

while((tarArchiveEntry = tais.getNextTarEntry()) != null){

String name = tarArchiveEntry.getName();

File tarFile = new File(parentPath, name);

if(!tarFile.getParentFile().exists()){

tarFile.getParentFile().mkdirs();

}

BufferedOutputStream bos =

new BufferedOutputStream(new FileOutputStream(tarFile));

int read = -1;

byte[] buffer = new byte[1024];

while((read = tais.read(buffer)) != -1){

bos.write(buffer, 0, read);

}

bos.close();

}

tais.close();

file.delete();//删除tar文件

}

public static void main(String[] args) throws Exception {

// String entry = "/Users/hxm/Downloads/temp";//需要压缩的文件夹

// String archive = archive(entry);//生成tar包

// String path = compressArchive(archive);//生成gz包

// System.out.println(path);

unCompressArchiveGz("/Users/hxm/Downloads/temp.tar.gz");

}

}

文章来源: www.oschina.net,作者:itazi,版权归原作者所有,如需转载,请联系作者。

原文链接:https://my.oschina.net/itazi/blog/3179456

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值