Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package compressor;
- import java.io.*;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
- import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
- import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
- import org.apache.commons.compress.utils.IOUtils;
- /**
- *
- * @author Administrator
- */
- public class TarCompressor {
- private static TarArchiveOutputStream tarOut = null;
- private File tarGz = null;
- private File[] fileArr = null;
- // this is the list of files.
- // The boolean is a setting for whether subdirectories
- // should be included or not. doesn't apply to files
- private HashMap<File, Boolean> fileMap;
- private TarArchiveOutputStream getTarOut() throws IOException {
- if (tarOut == null) {
- tarOut = new TarArchiveOutputStream(new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(tarGz))));
- }
- return tarOut;
- }
- /**
- *
- * @param tarGz
- */
- public TarCompressor(File tarGz, HashMap<File, Boolean> fileMap) {
- this.tarGz = tarGz;
- this.fileMap = fileMap;
- }
- public static void main(String[] args) {
- File tarGzPath = new File("testarchive.tar.gz");
- HashMap<File, Boolean> files = new HashMap<>();
- files.put(new File("Test"), true);
- files.put(new File("Testing2"), false);
- files.put(new File("Text.txt"), true);
- TarCompressor zu = new TarCompressor(tarGzPath, files);
- try {
- zu.createTarGzFromFileMap();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void createTarGzFromFileMap() throws IOException {
- getTarOut();
- // for (File fileOrDirectory : fileArr) {
- // createTarGzFromDirectory(fileOrDirectory);
- // }
- for (Map.Entry<File, Boolean> file : fileMap.entrySet()) {
- System.out.println("working on: " + file.getKey() + " | recursive: " + file.getValue());
- createTarGzFromDirectory(file.getKey(), file.getValue());
- }
- closeTarOut();
- }
- private void closeTarOut() {
- try {
- getTarOut().close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- private void createTarGzFromDirectory(File originalPath, boolean recursive) {
- addFileToTarGz(originalPath, "", recursive);
- }
- private void addFileToTarGz(File path, String base, boolean recursive) {
- String entryName = base + path.getName();
- TarArchiveEntry tarEntry = new TarArchiveEntry(path, entryName);
- try {
- // for filenames > 100 characters
- getTarOut().setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- if (path.isFile()) {
- try {
- tarOut.putArchiveEntry(tarEntry);
- IOUtils.copy(new FileInputStream(path), tarOut);
- tarOut.closeArchiveEntry();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- } else {
- // if not a file...
- if (recursive) {
- File[] children = path.listFiles();
- for (File child : children) {
- addFileToTarGz(new File(child.getAbsolutePath()), entryName + "/", true);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement