@@ -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
0 commit comments