Generate an Advanced ZIP Archive in PHP

Cloudmersive
2 min readAug 23, 2021

When interacting with partners and clients, it’s always important to make a good impression across all aspects of the relationship — this includes the presentation of data and content. In this brief tutorial, we will demonstrate how to use an API in PHP to create an advanced ZIP archive that will not only compress files into a more manageable storage size, but will also provide organizational options to control the structure of the resulting archive.

We will start things off by running this command to install the SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

Once the installation is complete, we will need to input the necessary information into the following request:

{
"FilesInZip": [
{
"FileName": "string",
"FileContents": "string"
}
],
"DirectoriesInZip": [
{
"DirectoryName": "string",
"DirectoriesInDirectory": [
null
],
"FilesInDirectory": [
{
"FileName": "string",
"FileContents": "string"
}
]
}
]
}

After this, we are ready to add the request and the API key into the below example code:

<?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
);
$request = new \Swagger\Client\Model\CreateZipArchiveRequest(); // \Swagger\Client\Model\CreateZipArchiveRequest | Input request
try {
$result = $apiInstance->zipArchiveZipCreateAdvanced($request);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ZipArchiveApi->zipArchiveZipCreateAdvanced: ', $e->getMessage(), PHP_EOL;
}
?>

And with that, we will have a compact and neat package to store or share as needed.

--

--

Cloudmersive

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