Skip to content

Commit 6367bbe

Browse files
authored
Merge pull request #2520 from Nicceboy/master
Disable compression by default when using container:get_archive method
2 parents 62afadc + 51fd6dd commit 6367bbe

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

docker/api/container.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ def export(self, container, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
699699
return self._stream_raw_result(res, chunk_size, False)
700700

701701
@utils.check_resource('container')
702-
def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
702+
def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE,
703+
encode_stream=False):
703704
"""
704705
Retrieve a file or folder from a container in the form of a tar
705706
archive.
@@ -710,6 +711,8 @@ def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
710711
chunk_size (int): The number of bytes returned by each iteration
711712
of the generator. If ``None``, data will be streamed as it is
712713
received. Default: 2 MB
714+
encode_stream (bool): Determines if data should be encoded
715+
(gzip-compressed) during transmission. Default: False
713716
714717
Returns:
715718
(tuple): First element is a raw tar data stream. Second element is
@@ -734,8 +737,13 @@ def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
734737
params = {
735738
'path': path
736739
}
740+
headers = {
741+
"Accept-Encoding": "gzip, deflate"
742+
} if encode_stream else {
743+
"Accept-Encoding": "identity"
744+
}
737745
url = self._url('/containers/{0}/archive', container)
738-
res = self._get(url, params=params, stream=True)
746+
res = self._get(url, params=params, stream=True, headers=headers)
739747
self._raise_for_status(res)
740748
encoded_stat = res.headers.get('x-docker-container-path-stat')
741749
return (

docker/models/containers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ def export(self, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
225225
"""
226226
return self.client.api.export(self.id, chunk_size)
227227

228-
def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
228+
def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE,
229+
encode_stream=False):
229230
"""
230231
Retrieve a file or folder from the container in the form of a tar
231232
archive.
@@ -235,6 +236,8 @@ def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
235236
chunk_size (int): The number of bytes returned by each iteration
236237
of the generator. If ``None``, data will be streamed as it is
237238
received. Default: 2 MB
239+
encode_stream (bool): Determines if data should be encoded
240+
(gzip-compressed) during transmission. Default: False
238241
239242
Returns:
240243
(tuple): First element is a raw tar data stream. Second element is
@@ -255,7 +258,8 @@ def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
255258
... f.write(chunk)
256259
>>> f.close()
257260
"""
258-
return self.client.api.get_archive(self.id, path, chunk_size)
261+
return self.client.api.get_archive(self.id, path,
262+
chunk_size, encode_stream)
259263

260264
def kill(self, signal=None):
261265
"""

tests/unit/models_containers_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def test_get_archive(self):
450450
container = client.containers.get(FAKE_CONTAINER_ID)
451451
container.get_archive('foo')
452452
client.api.get_archive.assert_called_with(
453-
FAKE_CONTAINER_ID, 'foo', DEFAULT_DATA_CHUNK_SIZE
453+
FAKE_CONTAINER_ID, 'foo', DEFAULT_DATA_CHUNK_SIZE, False
454454
)
455455

456456
def test_image(self):

0 commit comments

Comments
 (0)