java压缩成.tar_Java 实现*.tar.gz文件压缩和解压

importorg.apache.commons.compress.archivers.tar.TarArchiveEntry;importorg.apache.commons.compress.archivers.tar.TarArchiveOutputStream;importorg.apache.commons.compress.utils.IOUtils;importorg.apache.tools.tar.TarEntry;importorg.apache.tools.tar.TarInputStream;importjava.io.*;importjava.util.zip.GZIPInputStream;importjava.util.zip.GZIPOutputStream;/***@Title:GZIPUtil.java*@Description:gzip file compression and decompression tool classes*@authorzp*@date*@versionV1.0*/public classGZIPUtil {

//in order to gzip file to tar.gz,we need to get *.tar file/****@Title:pack*@Description:Copy a set of files into tar packages*@paramsourcesAn array of original files to be packed*@paramtargetDefine the file after the tar package*@returnFile return the tar file*@throws*/public staticFile pack(File[] sources,File target){

FileOutputStream out= null;try{

out= newFileOutputStream(target);} catch(FileNotFoundException e1) {

e1.printStackTrace();}

TarArchiveOutputStream os = newTarArchiveOutputStream(out);for(File file : sources) {

try{

os.putArchiveEntry(newTarArchiveEntry(file));IOUtils.copy(newFileInputStream(file),os);os.closeArchiveEntry();} catch(FileNotFoundException e) {

e.printStackTrace();} catch(IOException e) {

e.printStackTrace();}

}

if(os != null) {

try{

os.flush();os.close();} catch(IOException e) {

e.printStackTrace();}

}

returntarget;}

//get *.tar.gz file/****@Title:compress*@Description:Compress the file with gzip*@paramsourcethe files needed to be compress*@returnFile the *.gz file*@throws*/public staticFile compress(File source) {

File target = newFile(source.getName() + ".gz");FileInputStream in= null;GZIPOutputStream out= null;try{

in= newFileInputStream(source);out= newGZIPOutputStream(newFileOutputStream(target));byte[] array = new byte[1024];intnumber= -1;while((number= in.read(array,0,array.length)) != -1) {

out.write(array,0,number);}

} catch(FileNotFoundException e) {

e.printStackTrace();return null;} catch(IOException e) {

e.printStackTrace();return null;} finally{

if(in!= null) {

try{

in.close();} catch(IOException e) {

e.printStackTrace();return null;}

}

if(out!= null) {

try{

out.close();} catch(IOException e) {

e.printStackTrace();return null;}

}

}

returntarget;}

/*** Unzip the tar.gz file*@paramfilethe object file needed to be unzip*@paramoutputDir*@throwsIOException*/public static voidunTarGz(String file,String outputDir) throwsIOException{

TarInputStream tarIn= null;try{

tarIn= newTarInputStream(newGZIPInputStream(

newBufferedInputStream(newFileInputStream(file))),1024* 2);createDirectory(outputDir,null);//创建输出目录TarEntry entry= null;while( (entry= tarIn.getNextEntry()) != null){

if(entry.isDirectory()){//是目录entry.getName();createDirectory(outputDir,entry.getName());//创建空目录}else{//是文件File tmpFile = newFile(outputDir + "/"+ entry.getName());createDirectory(tmpFile.getParent() + "/",null);//创建输出目录OutputStream out= null;try{

out= newFileOutputStream(tmpFile);intlength= 0;byte[] b = new byte[2048];while((length= tarIn.read(b)) != -1){

out.write(b,0,length);}

}catch(IOException ex){

throwex;}finally{

if(out!=null)

out.close();}

}

}

}catch(IOException ex){

throw newIOException("解压归档文件出现异常",ex);} finally{

try{

if(tarIn!= null){

tarIn.close();}

}catch(IOException ex){

throw newIOException("关闭tarFile出现异常",ex);}

}

}

/*** Create a file directory*@paramoutputDir*@paramsubDir*/public static voidcreateDirectory(String outputDir,String subDir){

File file= newFile(outputDir);if(!(subDir == null|| subDir.trim().equals(""))){//子目录不为空file= newFile(outputDir + "/"+ subDir);}

if(!file.exists()){

if(!file.getParentFile().exists())

file.getParentFile().mkdirs();file.mkdirs();}

}

public static voidmain(String[] args) throwsIOException {

//sources:Gets an array of files that require tar compressionFile[] sources = newFile[] {newFile("C:\\Users\\Administrator\\Desktop\\sql\\test.sql"),newFile("C:\\Users\\Administrator\\Desktop\\sql\\weibo2.txt")};//Define a the *.tar file ,the file in sources will be tar to the targetFile target = newFile("C:\\Users\\Administrator\\Desktop\\release_package.tar");//compress the files into *.tar.gzcompress(pack(sources,target));//Extract the * .tar.gz file,need the *.tar.gz and the outputDirunTarGz("D:\\file\\work\\tools\\mengJun\\release_package.tar.gz","C:\\Users\\Administrator\\Desktop\\新建文件夹(2)");/* unGzipFile("C:\\Users\\Administrator\\Desktop\\release_package.tar.gz"); */}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值