How to extract Files and Folders from a Zip Archive in Python

Zip archives are ubiquitous these days, making it all the more important to have compatibility for them in your project, whatever it may be. I’m guessing you don’t have hours to kill on setting this up from scratch, so I’ll be showing you the easy-as-pie method instead.

Get the Cloudmersive Documents and Conversion API client with pip install:

pip install cloudmersive-convert-api-client

We will next call zip_archive_zip_extract, which should be done using a newly instantiated API, like so:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.ZipArchiveApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Extract, decompress files and folders from a zip archiveapi_response = api_instance.zip_archive_zip_extract(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ZipArchiveApi->zip_archive_zip_extract: %s\n" % e)

And now you have zip extraction. Simple.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store