How to Validate a Zip Archive File in Python
Even when they come from somewhere we know we can trust, it’s critical to validate all Zip files before they’re either unzipped or enter long/short term data storage (to be unzipped at a later date). Thankfully, you can easily validate Zip files with only a few lines of code in your Python application: you just need to implement the free-to-use API solution provided below. This API will provide a response body indicating if the file is valid, and if not, it’ll supply a wealth of information regarding any errors or associated warnings it found. In addition, it’ll let you know if the file is password protected, which — if unexpected — is a big red flag.
To utilize this API, we’ll begin by running the below command to install the SDK:
pip install cloudmersive-convert-api-client
Next, we’ll add the imports and 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.ValidateDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
try:
# Validate a Zip Archive file (zip)
api_response = api_instance.validate_document_zip_validation(input_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling ValidateDocumentApi->validate_document_zip_validation: %s\n" % e)
That’s all there is to it! To complete your API call, you’ll just need to include a free-tier Cloudmersive API key, which you can get by registering a free account on our website (this provides a limit of 800 API calls per month & no additional commitments).