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

packagecom.yabsz.decompCompr;/*** 2010-4-20*/

importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importorg.apache.commons.compress.archivers.tar.TarArchiveEntry;importorg.apache.commons.compress.archivers.tar.TarArchiveOutputStream;importorg.apache.commons.compress.archivers.tar.TarArchiveInputStream;/*** TAR工具

*

*@author*@since1.0*/

public abstract classTarUtils {private static final String BASE_DIR = "";//符号"/"用来作为目录标识判断符

private static final String PATH = "/";private static final int BUFFER = 1024;private static final String EXT = ".tar";/*** 归档

*

*@paramsrcPath

*@paramdestPath

*@throwsException*/

public static voidarchive(String srcPath, String destPath)throwsException {

File srcFile= newFile(srcPath);

archive(srcFile, destPath);

}/*** 归档

*

*@paramsrcFile

* 源路径

*@paramdestPath

* 目标路径

*@throwsException*/

public static void archive(File srcFile, File destFile) throwsException {

TarArchiveOutputStream taos= newTarArchiveOutputStream(newFileOutputStream(destFile));

archive(srcFile, taos, BASE_DIR);

taos.flush();

taos.close();

}/*** 归档

*

*@paramsrcFile

*@throwsException*/

public static void archive(File srcFile) throwsException {

String name=srcFile.getName();//String basePath = srcFile.getParent();

String destPath = name +EXT;

archive(srcFile, destPath);

}/*** 归档文件

*

*@paramsrcFile

*@paramdestPath

*@throwsException*/

public static void archive(File srcFile, String destPath) throwsException {

archive(srcFile,newFile(destPath));

}/*** 归档

*

*@paramsrcPath

*@throwsException*/

public static void archive(String srcPath) throwsException {

File srcFile= newFile(srcPath);

archive(srcFile);

}/*** 归档

*

*@paramsrcFile

* 源路径

*@paramtaos

* TarArchiveOutputStream

*@parambasePath

* 归档包内相对路径

*@throwsException*/

private static voidarchive(File srcFile, TarArchiveOutputStream taos,

String basePath)throwsException {if(srcFile.isDirectory()) {

archiveDir(srcFile, taos, basePath);

}else{

archiveFile(srcFile, taos, basePath);

}

}/*** 目录归档

*

*@paramdir

*@paramtaos

* TarArchiveOutputStream

*@parambasePath

*@throwsException*/

private static voidarchiveDir(File dir, TarArchiveOutputStream taos,

String basePath)throwsException {

File[] files=dir.listFiles();if (files.length < 1) {

TarArchiveEntry entry= newTarArchiveEntry(basePath+ dir.getName() +PATH);

taos.putArchiveEntry(entry);

taos.closeArchiveEntry();

}for(File file : files) {//递归归档

archive(file, taos, basePath + dir.getName() +PATH);

}

}/*** 数据归档

*

*@paramdata

* 待归档数据

*@parampath

* 归档数据的当前路径

*@paramname

* 归档文件名

*@paramtaos

* TarArchiveOutputStream

*@throwsException*/

private static voidarchiveFile(File file, TarArchiveOutputStream taos,

String dir)throwsException {/*** 归档内文件名定义

*

*

 
 

* 如果有多级目录,那么这里就需要给出包含目录的文件名

* 如果用WinRAR打开归档包,中文名将显示为乱码

*

*/TarArchiveEntry entry= new TarArchiveEntry(dir +file.getName());

entry.setSize(file.length());

taos.putArchiveEntry(entry);

BufferedInputStream bis= new BufferedInputStream(newFileInputStream(

file));intcount;byte data[] = new byte[BUFFER];while ((count = bis.read(data, 0, BUFFER)) != -1) {

taos.write(data,0, count);

}

bis.close();

taos.closeArchiveEntry();

}/*** 解归档

*

*@paramsrcFile

*@throwsException*/

public static void dearchive(File srcFile) throwsException {

String basePath=srcFile.getParent();

dearchive(srcFile, basePath);

}/*** 解归档

*

*@paramsrcFile

*@paramdestFile

*@throwsException*/

public static void dearchive(File srcFile, File destFile) throwsException {

TarArchiveInputStream tais= newTarArchiveInputStream(newFileInputStream(srcFile));

dearchive(destFile, tais);

tais.close();

}/*** 解归档

*

*@paramsrcFile

*@paramdestPath

*@throwsException*/

public static voiddearchive(File srcFile, String destPath)throwsException {

dearchive(srcFile,newFile(destPath));

}/*** 文件 解归档

*

*@paramdestFile

* 目标文件

*@paramtais

* ZipInputStream

*@throwsException*/

private static voiddearchive(File destFile, TarArchiveInputStream tais)throwsException {

TarArchiveEntry entry= null;while ((entry = tais.getNextTarEntry()) != null) {//文件

String dir = destFile.getPath() + File.separator +entry.getName();

File dirFile= newFile(dir);//文件检查

fileProber(dirFile);if(entry.isDirectory()) {

dirFile.mkdirs();

}else{

dearchiveFile(dirFile, tais);

}

}

}/*** 文件 解归档

*

*@paramsrcPath

* 源文件路径

*

*@throwsException*/

public static void dearchive(String srcPath) throwsException {

File srcFile= newFile(srcPath);

dearchive(srcFile);

}/*** 文件 解归档

*

*@paramsrcPath

* 源文件路径

*@paramdestPath

* 目标文件路径

*@throwsException*/

public static voiddearchive(String srcPath, String destPath)throwsException {

File srcFile= newFile(srcPath);

dearchive(srcFile, destPath);

}/*** 文件解归档

*

*@paramdestFile

* 目标文件

*@paramtais

* TarArchiveInputStream

*@throwsException*/

private static voiddearchiveFile(File destFile, TarArchiveInputStream tais)throwsException {

BufferedOutputStream bos= newBufferedOutputStream(newFileOutputStream(destFile));intcount;byte data[] = new byte[BUFFER];while ((count = tais.read(data, 0, BUFFER)) != -1) {

bos.write(data,0, count);

}

bos.close();

}/*** 文件探针

*

*

 
 

* 当父目录不存在时,创建目录!

*

*

*@paramdirFile*/

private static voidfileProber(File dirFile) {

File parentFile=dirFile.getParentFile();if (!parentFile.exists()) {//递归寻找上级目录

fileProber(parentFile);

parentFile.mkdir();

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值