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"); */}
}