|
2 | 2 |
|
3 | 3 | import static com.github.dockerjava.core.FilePathUtil.relativize; |
4 | 4 |
|
| 5 | +import java.io.BufferedInputStream; |
5 | 6 | import java.io.BufferedOutputStream; |
6 | 7 | import java.io.File; |
| 8 | +import java.io.FileNotFoundException; |
7 | 9 | import java.io.FileOutputStream; |
8 | 10 | import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.OutputStream; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
9 | 15 | import java.util.zip.GZIPOutputStream; |
10 | 16 |
|
11 | 17 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
12 | 18 | import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; |
| 19 | +import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; |
13 | 20 | import org.apache.commons.io.FileUtils; |
14 | 21 |
|
| 22 | +import com.google.common.io.ByteStreams; |
| 23 | +import com.google.common.io.Closeables; |
| 24 | + |
15 | 25 | public class CompressArchiveUtil { |
16 | 26 |
|
| 27 | + static void putTarEntry(TarArchiveOutputStream tarOutputStream, TarArchiveEntry tarEntry, Path file) throws IOException { |
| 28 | + tarEntry.setSize(Files.size(file)); |
| 29 | + tarOutputStream.putArchiveEntry(tarEntry); |
| 30 | + InputStream input = new BufferedInputStream(Files.newInputStream(file)); |
| 31 | + try { |
| 32 | + ByteStreams.copy(input, tarOutputStream); |
| 33 | + tarOutputStream.closeArchiveEntry(); |
| 34 | + } finally { |
| 35 | + input.close(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Recursively tar file |
| 41 | + * |
| 42 | + * @param inputPath file path can be directory |
| 43 | + * @param outputPath where to put the archived file |
| 44 | + * @param childrenOnly if inputPath is directory and if childrenOnly is true, the archive will contain all of its children, else the archive contains unique |
| 45 | + * entry which is the inputPath itself |
| 46 | + * @param gZipped compress with gzip algorithm |
| 47 | + */ |
| 48 | + public static void tar(Path inputPath, Path outputPath, boolean gZipped, boolean childrenOnly) throws IOException { |
| 49 | + if (!Files.exists(inputPath)) { |
| 50 | + throw new FileNotFoundException("File not found " + inputPath); |
| 51 | + } |
| 52 | + FileUtils.touch(outputPath.toFile()); |
| 53 | + OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputPath)); |
| 54 | + if (gZipped) { |
| 55 | + outputStream = new GzipCompressorOutputStream(outputStream); |
| 56 | + } |
| 57 | + TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(outputStream); |
| 58 | + tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); |
| 59 | + try { |
| 60 | + if (!Files.isDirectory(inputPath)) { |
| 61 | + putTarEntry(tarArchiveOutputStream, new TarArchiveEntry(inputPath.getFileName().toString()), inputPath); |
| 62 | + } else { |
| 63 | + Path sourcePath = inputPath; |
| 64 | + if (!childrenOnly) { |
| 65 | + // In order to have the dossier as the root entry |
| 66 | + sourcePath = inputPath.getParent(); |
| 67 | + } |
| 68 | + Files.walkFileTree(inputPath, new TarDirWalker(sourcePath, tarArchiveOutputStream)); |
| 69 | + } |
| 70 | + tarArchiveOutputStream.flush(); |
| 71 | + } finally { |
| 72 | + Closeables.close(tarArchiveOutputStream, true); |
| 73 | + } |
| 74 | + } |
| 75 | + |
17 | 76 | public static File archiveTARFiles(File base, Iterable<File> files, String archiveNameWithOutExtension) |
18 | 77 | throws IOException { |
19 | 78 | File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar"); |
|
0 commit comments