How to Validate a ZIP File in PHP

Cloudmersive
2 min readApr 2, 2024

--

Using a few lines of code, we can easily call a free API to validate ZIP archive files.

We’ll get information about any errors or warnings found in our document in our API response, and we’ll also find out if our ZIP file is password protected.

To begin, let’s execute the below command from our command line to install the SDK with composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next, let’s grab a free Cloudmersive API key to authorize our API calls. This will allow us to make a limit of 800 API calls per month with no commitments (our total will reset the following month once we reach it).

Lastly, let’s call the function to validate our ZIP file (we’ll use our Zip file path to load our file):

<?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\ValidateDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->validateDocumentZipValidation($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ValidateDocumentApi->validateDocumentZipValidation: ', $e->getMessage(), PHP_EOL;
}
?>

That’s all the code we’ll need! Now we can quickly and easily validate ZIP archive files with minimal code.

--

--

Cloudmersive

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