Skip to content

Commit 63f693b

Browse files
committed
Remove duplicated code to add a file to a tar
This remove the duplicated code to handle executable file and copy of the file content into the archive. It will also allow to fix the symlink without duplicating the fix in 3 places. Related to issue #532
1 parent 098f74d commit 63f693b

2 files changed

Lines changed: 14 additions & 29 deletions

File tree

src/main/java/com/github/dockerjava/core/util/CompressArchiveUtil.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ private CompressArchiveUtil() {
2828
// utility class
2929
}
3030

31-
static void putTarEntry(TarArchiveOutputStream tarOutputStream, TarArchiveEntry tarEntry, Path file)
31+
static void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, Path file, String entryName)
3232
throws IOException {
33-
tarEntry.setSize(Files.size(file));
34-
tarOutputStream.putArchiveEntry(tarEntry);
35-
try (InputStream input = new BufferedInputStream(Files.newInputStream(file))) {
36-
ByteStreams.copy(input, tarOutputStream);
37-
tarOutputStream.closeArchiveEntry();
33+
TarArchiveEntry archiveEntry = (TarArchiveEntry) tarArchiveOutputStream.createArchiveEntry(file.toFile(), entryName);
34+
if (file.toFile().canExecute()) {
35+
archiveEntry.setMode(archiveEntry.getMode() | 0755);
3836
}
37+
tarArchiveOutputStream.putArchiveEntry(archiveEntry);
38+
if (file.toFile().isFile()) {
39+
try (InputStream input = new BufferedInputStream(Files.newInputStream(file))) {
40+
ByteStreams.copy(input, tarArchiveOutputStream);
41+
}
42+
}
43+
tarArchiveOutputStream.closeArchiveEntry();
3944
}
4045

4146
private static TarArchiveOutputStream buildTarStream(Path outputPath, boolean gZipped) throws IOException {
@@ -69,11 +74,7 @@ public static void tar(Path inputPath, Path outputPath, boolean gZipped, boolean
6974

7075
try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputPath, gZipped)) {
7176
if (!Files.isDirectory(inputPath)) {
72-
TarArchiveEntry tarEntry = new TarArchiveEntry(inputPath.getFileName().toString());
73-
if (inputPath.toFile().canExecute()) {
74-
tarEntry.setMode(tarEntry.getMode() | 0755);
75-
}
76-
putTarEntry(tarArchiveOutputStream, tarEntry, inputPath);
77+
addFileToTar(tarArchiveOutputStream, inputPath, inputPath.getFileName().toString());
7778
} else {
7879
Path sourcePath = inputPath;
7980
if (!childrenOnly) {
@@ -95,19 +96,7 @@ public static File archiveTARFiles(File base, Iterable<File> files, String archi
9596
new FileOutputStream(tarFile))))) {
9697
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
9798
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);
109-
}
110-
tos.closeArchiveEntry();
99+
addFileToTar(tos, file.toPath(), relativize(base, file));
111100
}
112101
}
113102

src/main/java/com/github/dockerjava/core/util/TarDirWalker.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
3636
if (attrs.isSymbolicLink()) { // symbolic link to folder
3737
return FileVisitResult.CONTINUE;
3838
}
39-
TarArchiveEntry tarEntry = new TarArchiveEntry(FilePathUtil.relativize(basePath, file));
40-
if (file.toFile().canExecute()) {
41-
tarEntry.setMode(tarEntry.getMode() | 0755);
42-
}
43-
CompressArchiveUtil.putTarEntry(tarArchiveOutputStream, tarEntry, file);
39+
CompressArchiveUtil.addFileToTar(tarArchiveOutputStream, file, FilePathUtil.relativize(basePath, file));
4440
return FileVisitResult.CONTINUE;
4541
}
4642

0 commit comments

Comments
 (0)