Skip to content

Commit 51fd6dd

Browse files
committed
Disable compression by default when using get_archive method
Signed-off-by: Niklas Saari <niklas.saari@tutanota.com>
1 parent 030af62 commit 51fd6dd

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
@@ -694,7 +694,8 @@ def export(self, container, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
694694
return self._stream_raw_result(res, chunk_size, False)
695695

696696
@utils.check_resource('container')
697-
def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
697+
def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE,
698+
encode_stream=False):
698699
"""
699700
Retrieve a file or folder from a container in the form of a tar
700701
archive.
@@ -705,6 +706,8 @@ def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
705706
chunk_size (int): The number of bytes returned by each iteration
706707
of the generator. If ``None``, data will be streamed as it is
707708
received. Default: 2 MB
709+
encode_stream (bool): Determines if data should be encoded
710+
(gzip-compressed) during transmission. Default: False
708711
709712
Returns:
710713
(tuple): First element is a raw tar data stream. Second element is
@@ -729,8 +732,13 @@ def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
729732
params = {
730733
'path': path
731734
}
735+
headers = {
736+
"Accept-Encoding": "gzip, deflate"
737+
} if encode_stream else {
738+
"Accept-Encoding": "identity"
739+
}
732740
url = self._url('/containers/{0}/archive', container)
733-
res = self._get(url, params=params, stream=True)
741+
res = self._get(url, params=params, stream=True, headers=headers)
734742
self._raise_for_status(res)
735743
encoded_stat = res.headers.get('x-docker-container-path-stat')
736744
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)