@@ -112,12 +112,6 @@ public String toString() {
112
112
.append (remotePath ).toString ();
113
113
}
114
114
115
- private InputStream buildUploadStream (String hostResource , boolean dirChildrenOnly ) throws IOException {
116
- Path toUpload = Files .createTempFile ("docker-java" , ".tar.gz" );
117
- CompressArchiveUtil .tar (Paths .get (hostResource ), toUpload , true , dirChildrenOnly );
118
- return Files .newInputStream (toUpload );
119
- }
120
-
121
115
/**
122
116
* @throws com.github.dockerjava.api.exception.NotFoundException
123
117
* No such container
@@ -130,16 +124,30 @@ public Void exec() throws NotFoundException {
130
124
throw new DockerClientException (
131
125
"Only one of host resource or tar input stream should be defined to perform the copy, not both" );
132
126
}
133
- // We compress the given path, call exec so that the stream is consumed and then close it our self
134
- try (InputStream uploadStream = buildUploadStream (this .hostResource , this .dirChildrenOnly )) {
127
+ // create TAR package for the given path so docker can consume it
128
+ Path toUpload = null ;
129
+ try {
130
+ toUpload = Files .createTempFile ("docker-java" , ".tar.gz" );
131
+ CompressArchiveUtil .tar (Paths .get (hostResource ), toUpload , true , dirChildrenOnly );
132
+ } catch (IOException createFileIOException ) {
133
+ if (toUpload != null ) {
134
+ // remove tmp docker-javaxxx.tar.gz
135
+ toUpload .toFile ().delete ();
136
+ }
137
+ throw new DockerClientException ("Unable to perform tar on host resource " + this .hostResource , createFileIOException );
138
+ }
139
+ // send the tar stream, call exec so that the stream is consumed and then closed by try-with-resources
140
+ try (InputStream uploadStream = Files .newInputStream (toUpload )) {
135
141
this .tarInputStream = uploadStream ;
136
142
return super .exec ();
137
143
} catch (IOException e ) {
138
- throw new DockerClientException ("Unable to perform tar on host resource " + this .hostResource , e );
144
+ throw new DockerClientException ("Unable to read temp file " + toUpload .toFile ().getAbsolutePath (), e );
145
+ } finally {
146
+ // remove tmp docker-javaxxx.tar.gz
147
+ toUpload .toFile ().delete ();
139
148
}
140
149
} else if (this .tarInputStream == null ) {
141
- throw new DockerClientException (
142
- "One of host resource or tar input stream must be defined to perform the copy" );
150
+ throw new DockerClientException ("One of host resource or tar input stream must be defined to perform the copy" );
143
151
}
144
152
// User set a stream, so we will just consume it and let the user close it by him self
145
153
return super .exec ();
0 commit comments