Advertisement
redandwhite

TarCompressor

Jul 11th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. package compressor;
  2.  
  3. import java.io.*;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  9. import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
  10. import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
  11. import org.apache.commons.compress.utils.IOUtils;
  12.  
  13. /**
  14.  *
  15.  * @author Administrator
  16.  */
  17. public class TarCompressor {
  18.  
  19.     private static TarArchiveOutputStream tarOut = null;
  20.     private File tarGz = null;
  21.     private File[] fileArr = null;
  22.     // this is the list of files.
  23.     // The boolean is a setting for whether subdirectories
  24.     // should be included or not. doesn't apply to files
  25.     private HashMap<File, Boolean> fileMap;
  26.  
  27.     private TarArchiveOutputStream getTarOut() throws IOException {
  28.     if (tarOut == null) {
  29.         tarOut = new TarArchiveOutputStream(new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(tarGz))));
  30.     }
  31.  
  32.     return tarOut;
  33.     }
  34.  
  35.     /**
  36.      *
  37.      * @param tarGz
  38.      */
  39.     public TarCompressor(File tarGz, HashMap<File, Boolean> fileMap) {
  40.     this.tarGz = tarGz;
  41.     this.fileMap = fileMap;
  42.     }
  43.  
  44.     public static void main(String[] args) {
  45.  
  46.     File tarGzPath = new File("testarchive.tar.gz");
  47.    
  48.     HashMap<File, Boolean> files = new HashMap<>();
  49.  
  50.     files.put(new File("Test"), true);
  51.     files.put(new File("Testing2"), false);
  52.     files.put(new File("Text.txt"), true);
  53.  
  54.     TarCompressor zu = new TarCompressor(tarGzPath, files);
  55.    
  56.     try {
  57.  
  58.         zu.createTarGzFromFileMap();
  59.     } catch (Exception e) {
  60. e.printStackTrace();
  61.     }
  62.     }
  63.  
  64.     public void createTarGzFromFileMap() throws IOException {
  65.     getTarOut();
  66. //  for (File fileOrDirectory : fileArr) {
  67. //      createTarGzFromDirectory(fileOrDirectory);
  68. //  }
  69.  
  70.     for (Map.Entry<File, Boolean> file : fileMap.entrySet()) {
  71.         System.out.println("working on: " + file.getKey() + " | recursive: " + file.getValue());
  72.         createTarGzFromDirectory(file.getKey(), file.getValue());
  73.     }
  74.  
  75.     closeTarOut();
  76.     }
  77.  
  78.     private void closeTarOut() {
  79.     try {
  80.         getTarOut().close();
  81.     } catch (IOException ex) {
  82.         ex.printStackTrace();
  83.     }
  84.     }
  85.  
  86.     private void createTarGzFromDirectory(File originalPath, boolean recursive) {
  87.     addFileToTarGz(originalPath, "", recursive);
  88.     }
  89.  
  90.     private void addFileToTarGz(File path, String base, boolean recursive) {
  91.     String entryName = base + path.getName();
  92.  
  93.     TarArchiveEntry tarEntry = new TarArchiveEntry(path, entryName);
  94.     try {
  95.         // for filenames > 100 characters
  96.         getTarOut().setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
  97.     } catch (IOException ex) {
  98.         ex.printStackTrace();
  99.     }
  100.  
  101.     if (path.isFile()) {
  102.         try {
  103.         tarOut.putArchiveEntry(tarEntry);
  104.         IOUtils.copy(new FileInputStream(path), tarOut);
  105.         tarOut.closeArchiveEntry();
  106.         } catch (IOException ex) {
  107.         ex.printStackTrace();
  108.         }
  109.     } else {
  110.         // if not a file...
  111.         if (recursive) {
  112.         File[] children = path.listFiles();
  113.         for (File child : children) {
  114.             addFileToTarGz(new File(child.getAbsolutePath()), entryName + "/", true);
  115.         }
  116.         }
  117.  
  118.     }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement