1 package org.gffs.compression;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.Iterator;
9 import org.apache.commons.compress.archivers.ArchiveEntry;
10 import org.apache.commons.compress.archivers.ArchiveOutputStream;
11 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
12 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
13 import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
14 import org.apache.commons.io.FileUtils;
15 import org.apache.commons.io.IOUtils;
16 import org.apache.commons.io.filefilter.IOFileFilter;
17 import org.apache.commons.io.filefilter.TrueFileFilter;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
23 static private Log _logger = LogFactory.getLog(
PackTar.class);
28 public static String
stripOutPrefix(String prefix, String longName)
throws IOException
30 int indy = longName.indexOf(prefix, 0);
32 throw new IOException(
"failure to find prefix in string, prefix=" + prefix +
" and string=" + longName);
33 return longName.substring(indy + prefix.length());
42 public static void compressArchive(ArchiveOutputStream tarOut, String prefix, File source)
throws IOException
44 _logger.debug(
"entered into compress archive on source " + source +
" prefix " + prefix +
" and tarout " + tarOut);
46 if (!source.exists()) {
48 String msg =
"Directory " + source.getAbsolutePath() +
" doesn't exist yet. Cannot pack.";
50 throw new IOException(msg);
54 if (source.isFile()) {
56 if (_logger.isDebugEnabled())
57 _logger.debug(
"adding a file to the archive (chopped): " + choppedName);
58 ArchiveEntry
f = tarOut.createArchiveEntry(source, choppedName);
59 tarOut.putArchiveEntry(
f);
60 IOUtils.copy(
new FileInputStream(source), tarOut);
61 tarOut.closeArchiveEntry();
62 }
else if (source.isDirectory()) {
65 if (_logger.isDebugEnabled())
66 _logger.debug(
"iterating over a directory to add its contents to the archive: " + source);
68 Iterator<File> spidey = FileUtils.iterateFiles(source,
new IOFileFilter()
71 public boolean accept(File arg0)
77 public boolean accept(File arg0, String arg1)
81 }, TrueFileFilter.INSTANCE);
84 while (spidey.hasNext()) {
87 if (_logger.isTraceEnabled())
88 _logger.debug(
"recursing on item: " + item);
93 String msg =
"source is not a file or directory although it exists. unknown how to process.";
95 throw new IOException(msg);
105 if (toChop.endsWith(
"/"))
111 public synchronized static void compressTarGZ(File tarFile, File dest)
throws IOException
113 TarArchiveOutputStream tarOut =
new TarArchiveOutputStream(
new GzipCompressorOutputStream(
new FileOutputStream(tarFile)));
118 public synchronized static void compressTar(File tarFile, File dest)
throws IOException
120 TarArchiveOutputStream tarOut =
new TarArchiveOutputStream(
new FileOutputStream(tarFile));
125 public synchronized static void compressZip(File zipFile, File dest)
throws IOException
127 ZipArchiveOutputStream tarOut =
new ZipArchiveOutputStream(
new FileOutputStream(zipFile));
132 static public void main(String[] args)
throws Throwable
135 if (args.length != 2) {
136 System.err.println(
"USAGE: PackTar {tar.gz file} {source location}");
142 }
catch (Throwable t) {
143 _logger.error(
"failed to compress tar file " + args[0] +
" from " + args[1]);
147 System.out.println(
"successfully compressed archive file " + args[0] +
" from " + args[1]);
static void main(String[] args)
static void compressArchive(ArchiveOutputStream tarOut, String prefix, File source)
static synchronized void compressZip(File zipFile, File dest)
static String findAppropriatePrefix(String toChop)
static synchronized void compressTarGZ(File tarFile, File dest)
static synchronized void compressTar(File tarFile, File dest)
static String stripOutPrefix(String prefix, String longName)