19
19
import io .kubernetes .client .openapi .models .V1Pod ;
20
20
import io .kubernetes .client .util .exception .CopyNotSupportedException ;
21
21
import java .io .BufferedInputStream ;
22
+ import java .io .ByteArrayInputStream ;
22
23
import java .io .File ;
23
24
import java .io .FileInputStream ;
24
25
import java .io .FileOutputStream ;
@@ -179,15 +180,7 @@ public void copyFileToPod(
179
180
throws ApiException , IOException {
180
181
181
182
// Run decoding and extracting processes
182
- String parentPath = destPath .getParent () != null ? destPath .getParent ().toString () : "." ;
183
- final Process proc =
184
- this .exec (
185
- namespace ,
186
- pod ,
187
- new String [] {"sh" , "-c" , "base64 -d | tar -xmf - -C " + parentPath },
188
- container ,
189
- true ,
190
- false );
183
+ final Process proc = execCopyToPod (namespace , pod , container , destPath );
191
184
192
185
// Send encoded archive output stream
193
186
File srcFile = new File (srcPath .toUri ());
@@ -205,6 +198,39 @@ public void copyFileToPod(
205
198
}
206
199
}
207
200
201
+ public void copyFileToPod (
202
+ String namespace , String pod , String container , byte [] src , Path destPath )
203
+ throws ApiException , IOException {
204
+
205
+ // Run decoding and extracting processes
206
+ final Process proc = execCopyToPod (namespace , pod , container , destPath );
207
+
208
+ try (ArchiveOutputStream archiveOutputStream =
209
+ new TarArchiveOutputStream (new Base64OutputStream (proc .getOutputStream (), true , 0 , null ))) {
210
+
211
+ ArchiveEntry tarEntry = new TarArchiveEntry (new File (destPath .getFileName ().toString ()));
212
+ ((TarArchiveEntry ) tarEntry ).setSize (src .length );
213
+
214
+ archiveOutputStream .putArchiveEntry (tarEntry );
215
+ ByteStreams .copy (new ByteArrayInputStream (src ), archiveOutputStream );
216
+ archiveOutputStream .closeArchiveEntry ();
217
+ } finally {
218
+ proc .destroy ();
219
+ }
220
+ }
221
+
222
+ private Process execCopyToPod (String namespace , String pod , String container , Path destPath )
223
+ throws ApiException , IOException {
224
+ String parentPath = destPath .getParent () != null ? destPath .getParent ().toString () : "." ;
225
+ return this .exec (
226
+ namespace ,
227
+ pod ,
228
+ new String [] {"sh" , "-c" , "base64 -d | tar -xmf - -C " + parentPath },
229
+ container ,
230
+ true ,
231
+ false );
232
+ }
233
+
208
234
private boolean isTarPresentInContainer (String namespace , String pod , String container )
209
235
throws ApiException , IOException {
210
236
final Process proc =
0 commit comments