Ifada
Ifada

Reputation: 25

How to acccess a zip file stored in a bytes variable in Python 3?

I am writting a Python script that consumes a Web Service in order to access some xml file stored in a zipped folder with 3 other xml files: zipfolder/myxmlfile.xml

The Web Service provides a method "GetResponse" that can be used to get the zipped folder in the form of base64 data, as explained in the documentation.

So I use this method as the following:

response = client.service.GetResponse()

Your help is highly appreciated.

Upvotes: 0

Views: 951

Answers (2)

Ifada
Ifada

Reputation: 25

When I checked my "response" data I found out it wasn't base64, so I Base64 encoded it, then I Base64 decoded it. Finally, I wrote to a zip file and it worked.

encoded = base64.b64encode(response)
decoded = base64.b64decode(encoded)
with open('myzipfile.zip', 'wb') as f:
 f.write(decoded)

Upvotes: 0

Kostas Charitidis
Kostas Charitidis

Reputation: 3103

Try this using base64:

import base64
bin = base64.b64decode(response)
with open('your_path/your_filename.zip', 'wb') as f:
    f.write(bin)

Upvotes: 1

Related Questions