How to Create a Structured ZIP Archive in Python
While ZIP archives are ideal for compressing information into a more manageable and shareable format, they aren’t always the most organized option. Fortunately, this tutorial highlights an API that can be run in Python to create an advanced ZIP archive that allows you to construct the organization of the input files and folders.
We will start things off by installing the API client:
pip install cloudmersive-convert-api-client
Once the installation is complete, you will need to fill out the information in your request:
{
"FilesInZip": [
{
"FileName": "string",
"FileContents": "string"
}
],
"DirectoriesInZip": [
{
"DirectoryName": "string",
"DirectoriesInDirectory": [
null
],
"FilesInDirectory": [
{
"FileName": "string",
"FileContents": "string"
}
]
}
]
}
Now, you can input the request into the following code to call the function:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ZipArchiveApi(cloudmersive_convert_api_client.ApiClient(configuration))
request = cloudmersive_convert_api_client.CreateZipArchiveRequest() # CreateZipArchiveRequest | Input requesttry:
# Compress files and folders to create a new zip archive with advanced options
api_response = api_instance.zip_archive_zip_create_advanced(request)
pprint(api_response)
except ApiException as e:
print("Exception when calling ZipArchiveApi->zip_archive_zip_create_advanced: %s\n" % e)
This will deliver a structured ZIP archive in seconds flat. To retrieve your API key, visit the Cloudmersive website to register for a free account; this will provide 800 calls/month across our entire library of APIs.