1 package org.gffs.compression;
3 import java.io.BufferedOutputStream;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.nio.file.Files;
9 import java.nio.file.Paths;
10 import java.nio.file.attribute.PosixFilePermission;
11 import java.util.HashSet;
12 import org.apache.commons.compress.archivers.ArchiveEntry;
13 import org.apache.commons.compress.archivers.ArchiveInputStream;
14 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
15 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
16 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
17 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
18 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
24 static private Log _logger = LogFactory.getLog(
UnpackTar.class);
42 throw new IOException(
"Directory " + dest.getAbsolutePath() +
" already exists. Unpacking exiting");
46 ArchiveEntry tarEntry = tarIn.getNextEntry();
47 while (tarEntry !=
null) {
50 int defaultMode = 0750;
54 mode = ((TarArchiveEntry) tarEntry).getMode();
57 mode = ((ZipArchiveEntry) tarEntry).getUnixMode();
63 if (_logger.isTraceEnabled())
64 _logger.debug(
"The mode on '" + tarEntry.getName() +
"' is " + Integer.toOctalString(mode));
65 if (grantUserPermsToGroup) {
66 int temp = mode & 0700;
69 if (_logger.isTraceEnabled())
70 _logger.debug(
"Now mode on '" + tarEntry.getName() +
"' is " + Integer.toOctalString(mode));
75 File destPath =
new File(dest, tarEntry.getName());
76 if (_logger.isTraceEnabled())
77 _logger.debug(
"working on: " + destPath.getCanonicalPath());
78 if (tarEntry.isDirectory()) {
81 destPath.createNewFile();
84 byte[] btoRead =
new byte[8192];
85 BufferedOutputStream bout =
new BufferedOutputStream(
new FileOutputStream(destPath));
87 boolean wroteAnything =
false;
88 while ((len = tarIn.read(btoRead)) != -1) {
89 if (_logger.isTraceEnabled())
90 _logger.debug(
"read " + len +
" bytes");
92 bout.write(btoRead, 0, len);
95 _logger.error(
"zero bytes read from: " + destPath.getCanonicalPath());
101 HashSet<PosixFilePermission> perms =
new HashSet<PosixFilePermission>();
103 if ((mode & 0400) != 0)
104 perms.add(PosixFilePermission.OWNER_READ);
105 if ((mode & 0200) != 0)
106 perms.add(PosixFilePermission.OWNER_WRITE);
107 if ((mode & 0100) != 0)
108 perms.add(PosixFilePermission.OWNER_EXECUTE);
110 if ((mode & 0040) != 0)
111 perms.add(PosixFilePermission.GROUP_READ);
112 if ((mode & 0020) != 0)
113 perms.add(PosixFilePermission.GROUP_WRITE);
114 if ((mode & 0010) != 0)
115 perms.add(PosixFilePermission.GROUP_EXECUTE);
117 if ((mode & 0004) != 0)
118 perms.add(PosixFilePermission.OTHERS_READ);
119 if ((mode & 0002) != 0)
120 perms.add(PosixFilePermission.OTHERS_WRITE);
121 if ((mode & 0001) != 0)
122 perms.add(PosixFilePermission.OTHERS_EXECUTE);
124 Files.setPosixFilePermissions(Paths.get(destPath.getCanonicalPath()), perms);
125 tarEntry = tarIn.getNextEntry();
130 public synchronized static void uncompressTarGZ(File tarFile, File dest,
boolean grantUserPermsToGroup)
throws IOException
132 TarArchiveInputStream tarIn =
new TarArchiveInputStream(
new GzipCompressorInputStream(
new FileInputStream(tarFile)));
137 public synchronized static void uncompressTar(File tarFile, File dest,
boolean grantUserPermsToGroup)
throws IOException
139 TarArchiveInputStream tarIn =
new TarArchiveInputStream(
new FileInputStream(tarFile));
144 public synchronized static void uncompressZip(File zipFile, File dest,
boolean grantUserPermsToGroup)
throws IOException
146 ZipArchiveInputStream tarIn =
new ZipArchiveInputStream(
new FileInputStream(zipFile));
151 static public void main(String[] args)
throws Throwable
153 if (args.length != 2) {
154 System.err.println(
"USAGE: UnpackTar {tar.gz file} {output location}");
160 }
catch (Throwable t) {
161 _logger.error(
"failed to uncompress tar file " + args[0] +
" into " + args[1]);
165 System.out.println(
"successfully uncompressed tar file " + args[0] +
" into " + args[1]);
static synchronized void uncompressTarGZ(File tarFile, File dest, boolean grantUserPermsToGroup)
static synchronized void uncompressZip(File zipFile, File dest, boolean grantUserPermsToGroup)
static synchronized void uncompressTar(File tarFile, File dest, boolean grantUserPermsToGroup)
static void main(String[] args)
static void uncompressArchive(ArchiveInputStream tarIn, File dest, archiveType archType, boolean grantUserPermsToGroup)