How to virus scan a ZIP archive in Python

Archive files are like little Pandora’s boxes potentially filled with keyloggers and worms. You are going to need some ironclad protection to deal with this sort of threat, so let’s set up just such a system.

Start by installing the API client:

pip install cloudmersive-virus-api-client

We are going to call scan_file_advanced next, the structure for which you can see in this sample code block here:

from __future__ import print_functionimport timeimport cloudmersive_virus_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_virus_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_virus_api_client.ScanApi(cloudmersive_virus_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.allow_executables = true # bool | Set to false to block executable files (program code) from being allowed in the input file.  Default is false (recommended). (optional)allow_invalid_files = true # bool | Set to false to block invalid files, such as a PDF file that is not really a valid PDF file, or a Word Document that is not a valid Word Document.  Default is false (recommended). (optional)allow_scripts = true # bool | Set to false to block script files, such as a PHP files, Pythong scripts, and other malicious content or security threats that can be embedded in the file.  Set to true to allow these file types.  Default is false (recommended). (optional)allow_password_protected_files = true # bool | Set to false to block password protected and encrypted files, such as encrypted zip and rar files, and other files that seek to circumvent scanning through passwords.  Set to true to allow these file types.  Default is false (recommended). (optional)restrict_file_types = 'restrict_file_types_example' # str | Specify a restricted set of file formats to allow as clean as a comma-separated list of file formats, such as .pdf,.docx,.png would allow only PDF, PNG and Word document files.  All files must pass content verification against this list of file formats, if they do not, then the result will be returned as CleanResult=false.  Set restrictFileTypes parameter to null or empty string to disable; default is disabled. (optional)try:# Advanced Scan a file for virusesapi_response = api_instance.scan_file_advanced(input_file, allow_executables=allow_executables, allow_invalid_files=allow_invalid_files, allow_scripts=allow_scripts, allow_password_protected_files=allow_password_protected_files, restrict_file_types=restrict_file_types)pprint(api_response)except ApiException as e:print("Exception when calling ScanApi->scan_file_advanced: %s\n" % e)

Now you can customize a variety of different parameters, such as restricting file types and filtering out password protected files. Finally, run the code, and you will soon have the results for your virus scan in this format here:

"CleanResult": true,
"ContainsExecutable": true,
"ContainsInvalidFile": true,
"ContainsScript": true,
"ContainsPasswordProtectedFile": true,
"ContainsRestrictedFileFormat": true,
"VerifiedFileFormat": "string",
"FoundViruses": [
"FileName": "string",
"VirusName": "string"

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.