程式扎記: [ Java 常見問題 ] Compress directory to tar.gz with commons compress

標籤

2015年3月25日 星期三

[ Java 常見問題 ] Compress directory to tar.gz with commons compress

Source From Here 
Question 
I'm running into a problem using the Commons compress library to create a tar.gz of a directory. 

How-To 
Below is sample code: 
  1. package howto;  
  2.   
  3. import java.io.BufferedOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8.   
  9. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;  
  10. import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;  
  11. import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;  
  12. import org.apache.commons.compress.utils.IOUtils;  
  13.   
  14. public class TarDirectory {  
  15.     private static void AddFileToTarGz(TarArchiveOutputStream tOut, File f, String base) throws IOException {                          
  16.         String entryName = base + f.getName();  
  17.         System.out.printf("\tAdd entry...%s\n", entryName);  
  18.         TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);  
  19.         tOut.putArchiveEntry(tarEntry);  
  20.   
  21.         if (f.isFile())   
  22.         {  
  23.             IOUtils.copy(new FileInputStream(f), tOut);  
  24.             tOut.closeArchiveEntry();  
  25.         } else {  
  26.             tOut.closeArchiveEntry();  
  27.             File[] children = f.listFiles();  
  28.             if (children != null){  
  29.                 for (File child : children)   
  30.                 {                     
  31.                     AddFileToTarGz(tOut, child, entryName + "/");  
  32.                 }  
  33.             }  
  34.         }  
  35.     }  
  36.       
  37.     public static void main(String[] args) throws Exception{  
  38.         if(args.length<2)  
  39.         {  
  40.             System.out.printf("\t[Info] Please give argument1 as directory; argument2 as output tar file.\n");  
  41.             return;  
  42.         }  
  43.           
  44.         File outFile = new File(args[1]);  
  45.         File dir = new File(args[0]);  
  46.           
  47.         FileOutputStream fOut=null;  
  48.         BufferedOutputStream bOut=null;  
  49.         GzipCompressorOutputStream gzOut=null;  
  50.         TarArchiveOutputStream tOut=null;  
  51.         try{              
  52.             fOut = new FileOutputStream(outFile);  
  53.             bOut = new BufferedOutputStream(fOut);  
  54.             gzOut = new GzipCompressorOutputStream(bOut);  
  55.             tOut = new TarArchiveOutputStream(gzOut);  
  56.             AddFileToTarGz(tOut, dir , "");  
  57.         } finally {  
  58.             if(tOut!=null) {  
  59.                 tOut.finish();  
  60.                 tOut.close();  
  61.                 gzOut.close();  
  62.                 bOut.close();  
  63.                 fOut.close();  
  64.             }                          
  65.         }  
  66.     }  
  67. }  
Execution example: 
$ ls dir
abc.txt test.txt
$ java -jar TestJava7.jar
[Info] Please give argument1 as directory; argument2 as output tar file.
$ java -jar TestJava7.jar dir dir.tar.gz
Add entry...dir
Add entry...dir/abc.txt
Add entry...dir/test.txt

$ cp dir.tar.gz abc
$ tar -xvf abc/dir.tar.gz
dir/
dir/abc.txt
dir/test.txt

沒有留言:

張貼留言

網誌存檔