How to Unlock a ZIP File in PHP
Using just a few lines of PHP code, we can easily call a free API to unlock password our protected ZIP files.
This will help streamline the process of programmatically extracting important content from secure ZIP archives.
To structure our API call, we can start by installing the SDK. Let’s run the below command from our command line to install via Composer:
composer require cloudmersive/cloudmersive_document_convert_api_client
Next, let’s grab a free Cloudmersive API key to authorize our requests. This will allow us to make up to 800 API calls per month with no commitments (once we reach that limit, our total will simply reset the following month).
Now let’s copy the below code to call the function. We can load our ZIP file via the $input_file
variable, and we can supply the archive password via the $zip_password
variable:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\ZipArchiveApi(
new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$zip_password = "zip_password_example"; // string | Required; Password for the input archive
try {
$result = $apiInstance->zipArchiveZipDecrypt($input_file, $zip_password);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ZipArchiveApi->zipArchiveZipDecrypt: ', $e->getMessage(), PHP_EOL;
}
?>
After that, we’re all done — no more code required! Now we have a simple method for unlocking password protected ZIP archives.