|
18 | 18 |
|
19 | 19 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
20 | 20 | import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; |
| 21 | +import org.apache.commons.compress.archivers.tar.TarConstants; |
21 | 22 | import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; |
22 | 23 | import org.apache.commons.io.FileUtils; |
23 | 24 |
|
@@ -95,17 +96,27 @@ public static File archiveTARFiles(File base, Iterable<File> files, String archi |
95 | 96 | new FileOutputStream(tarFile))))) { |
96 | 97 | tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); |
97 | 98 | for (File file : files) { |
98 | | - TarArchiveEntry tarEntry = new TarArchiveEntry(file); |
99 | | - tarEntry.setName(relativize(base, file)); |
100 | | - |
101 | | - if (!file.isDirectory() && file.canExecute()) { |
102 | | - tarEntry.setMode(tarEntry.getMode() | 0755); |
103 | | - } |
104 | | - |
105 | | - tos.putArchiveEntry(tarEntry); |
106 | | - |
107 | | - if (!file.isDirectory()) { |
108 | | - FileUtils.copyFile(file, tos); |
| 99 | + // If file is a symbolic link |
| 100 | + if (Files.isSymbolicLink(file.toPath())) { |
| 101 | + Path target = Files.readSymbolicLink(file.toPath()); |
| 102 | + String targetEntryName = target.toFile().getName(); |
| 103 | + TarArchiveEntry tarEntry = new TarArchiveEntry(relativize(base.toPath(), file.toPath()), TarConstants.LF_SYMLINK); |
| 104 | + tarEntry.setLinkName(targetEntryName); |
| 105 | + tos.putArchiveEntry(tarEntry); |
| 106 | + |
| 107 | + } else { |
| 108 | + TarArchiveEntry tarEntry = new TarArchiveEntry(file); |
| 109 | + tarEntry.setName(relativize(base, file)); |
| 110 | + |
| 111 | + if (!file.isDirectory() && file.canExecute()) { |
| 112 | + tarEntry.setMode(tarEntry.getMode() | 0755); |
| 113 | + } |
| 114 | + |
| 115 | + tos.putArchiveEntry(tarEntry); |
| 116 | + |
| 117 | + if (!file.isDirectory()) { |
| 118 | + FileUtils.copyFile(file, tos); |
| 119 | + } |
109 | 120 | } |
110 | 121 | tos.closeArchiveEntry(); |
111 | 122 | } |
|
0 commit comments