2525import java .nio .file .Path ;
2626import java .util .ArrayList ;
2727import java .util .List ;
28+ import java .util .Map ;
2829import java .util .zip .GZIPOutputStream ;
2930
3031import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
3132import org .apache .commons .compress .archivers .tar .TarArchiveOutputStream ;
3233import org .testcontainers .shaded .org .apache .commons .io .FileUtils ;
3334import org .testcontainers .shaded .org .apache .commons .io .IOUtils ;
3435
36+ import software .xdev .testcontainers .imagebuilder .transfer .fcm .FileContentModifier ;
37+
3538
3639/**
3740 * Forked from {@link org.testcontainers.shaded.com.github.dockerjava.core.util.CompressArchiveUtil} to allow file
@@ -48,8 +51,7 @@ public TransferArchiveTARCompressor withContentModifier(final FileContentModifie
4851 }
4952
5053 public File archiveTARFiles (
51- final File base ,
52- final Iterable <Path > filesPaths ,
54+ final Map <Path , String > filesToTransfer ,
5355 final String archiveNameWithOutExtension ) throws IOException
5456 {
5557 final File tarFile = new File (FileUtils .getTempDirectoryPath (), archiveNameWithOutExtension + ".tar" );
@@ -62,9 +64,9 @@ public File archiveTARFiles(
6264 tos .setLongFileMode (3 );
6365 tos .setBigNumberMode (2 );
6466
65- for (final Path filePath : filesPaths )
67+ for (final Map . Entry < Path , String > fileData : filesToTransfer . entrySet () )
6668 {
67- this .addFileToTar (tos , filePath , FastFilePathUtil . relativize ( base . toPath ( ), filePath ));
69+ this .addFileToTar (tos , fileData . getKey ( ), fileData . getValue ( ));
6870 }
6971 }
7072
@@ -74,34 +76,34 @@ public File archiveTARFiles(
7476 @ SuppressWarnings ("checkstyle:MagicNumber" )
7577 protected void addFileToTar (
7678 final TarArchiveOutputStream tarArchiveOutputStream ,
77- final Path file ,
78- final String entryName
79+ final Path sourePath ,
80+ final String targetPath
7981 ) throws IOException
8082 {
8183 try
8284 {
83- if (Files .isSymbolicLink (file ))
85+ if (Files .isSymbolicLink (sourePath ))
8486 {
85- final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry (entryName , (byte )50 );
86- tarArchiveEntry .setLinkName (Files .readSymbolicLink (file ).toString ());
87+ final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry (targetPath , (byte )50 );
88+ tarArchiveEntry .setLinkName (Files .readSymbolicLink (sourePath ).toString ());
8789 tarArchiveOutputStream .putArchiveEntry (tarArchiveEntry );
8890 return ;
8991 }
9092
9193 final TarArchiveEntry tarArchiveEntry =
92- (TarArchiveEntry )tarArchiveOutputStream .createArchiveEntry (file .toFile (), entryName );
93- if (file .toFile ().canExecute ())
94+ (TarArchiveEntry )tarArchiveOutputStream .createArchiveEntry (sourePath .toFile (), targetPath );
95+ if (sourePath .toFile ().canExecute ())
9496 {
9597 tarArchiveEntry .setMode (tarArchiveEntry .getMode () | 493 );
9698 }
9799
98- if (!file .toFile ().isFile ())
100+ if (!sourePath .toFile ().isFile ())
99101 {
100102 tarArchiveOutputStream .putArchiveEntry (tarArchiveEntry );
101103 return ;
102104 }
103105
104- try (final InputStream input = this .createInputStreamForFile (file , tarArchiveEntry ))
106+ try (final InputStream input = this .createInputStreamForFile (sourePath , targetPath , tarArchiveEntry ))
105107 {
106108 // put it after it was modified
107109 tarArchiveOutputStream .putArchiveEntry (tarArchiveEntry );
@@ -114,17 +116,20 @@ protected void addFileToTar(
114116 }
115117 }
116118
117- protected InputStream createInputStreamForFile (final Path filePath , final TarArchiveEntry tarArchiveEntry )
119+ protected InputStream createInputStreamForFile (
120+ final Path sourePath ,
121+ final String targetPath ,
122+ final TarArchiveEntry tarArchiveEntry )
118123 throws IOException
119124 {
120125 for (final FileContentModifier fcm : this .fileContentModifiers )
121126 {
122- final InputStream is = fcm .apply (filePath , tarArchiveEntry );
127+ final InputStream is = fcm .apply (sourePath , targetPath , tarArchiveEntry );
123128 if (is != null )
124129 {
125130 return is ;
126131 }
127132 }
128- return new BufferedInputStream (Files .newInputStream (filePath ));
133+ return new BufferedInputStream (Files .newInputStream (sourePath ));
129134 }
130135}
0 commit comments