How to load and read ZIP data in PHP using TurboDepot

Working with ZIP files in PHP can be essential for various applications, from handling file uploads to managing backups efficiently. The ZipObject class from the TurboDepot library provides a convenient way to interact with ZIP files, allowing you to load, extract, and read data from them effortlessly. In this guide, we'll walk you through the process of loading and reading ZIP data using the ZipObject class.

Prerequisites

Before we begin, make sure you have the TurboDepot library installed and configured in your PHP environment. Download the latest TurboCommons and TurboDepot phar files, place them on your project as dependencies and run the following code:

require 'path/to/your/dependencies/folder/turbocommons-php-X.X.X.phar';
require 'path/to/your/dependencies/folder/turbodepot-php-X.X.X.phar';

use org\turbodepot\src\main\php\model\ZipObject;

Also be sure that the php extension is installed and enabled in your php runtime

Loading ZIP Data

The ZipObject class offers multiple methods for loading ZIP data:

Loading from a file path

You can load a ZIP file from a system path using the loadPath method:

$zipObject = new ZipObject();
$zipPath = '/path/to/your/archive.zip';

try {
    $zipObject->loadPath($zipPath);
    echo "ZIP file loaded successfully.";
} catch (UnexpectedValueException $e) {
    echo "Error loading ZIP file: " . $e->getMessage();
}

Loading from binary data

If you have ZIP data as a binary string, you can load it using the loadBinary method:

$zipObject = new ZipObject();
$binaryData = file_get_contents('/path/to/your/archive.zip');

try {
    $zipObject->loadBinary($binaryData);
    echo "ZIP data loaded successfully.";
} catch (UnexpectedValueException $e) {
    echo "Error loading ZIP data: " . $e->getMessage();
}

Loading from Base64 encoded data

Similarly, if your ZIP data is encoded in Base64, you can load it using the loadBase64 method:

$zipObject = new ZipObject();
$base64Data = base64_encode(file_get_contents('/path/to/your/archive.zip'));

try {
    $zipObject->loadBase64($base64Data);
    echo "Base64 encoded ZIP data loaded successfully.";
} catch (UnexpectedValueException $e) {
    echo "Error loading Base64 encoded ZIP data: " . $e->getMessage();
}

Reading ZIP data

Once the ZIP data is loaded, you can perform various operations, such as listing contents and reading specific entries.

Counting elements inside the zip file

To get the total number of items in the ZIP archive, use the countContents method:

if ($zipObject->isLoaded()) {
    $numItems = $zipObject->countContents();
    echo "Total items in ZIP: $numItems";
} else {
    echo "ZIP file is not loaded.";
}

Listing contents

You can list all the entries in the ZIP archive using the listContents method:

if ($zipObject->isLoaded()) {
    $contents = $zipObject->listContents();
    foreach ($contents as $entry) {
        echo "Entry: $entry\n";
    }
} else {
    echo "ZIP file is not loaded.";
}

Notice that the listContents method will give you a list of relative paths corresponding to each entry

Reading entry data

To read the data from a specific entry in the ZIP archive, use the readEntry method:

$entryName = 'example.txt';

try {
    $data = $zipObject->readEntry($entryName);
    echo "Content of $entryName:\n$data";
} catch (UnexpectedValueException $e) {
    echo "Error reading entry: " . $e->getMessage();
}

Conclusion

Using the ZipObject class from the TurboDepot library, you can easily load, extract, and read data from ZIP files in PHP. By following the methods outlined in this guide, you can efficiently manage ZIP data within your applications, enhancing functionality and user experience.

That concludes our tutorial on loading and reading ZIP data in PHP using the TurboDepot library. If you have any questions or encounter any issues, feel free to consult the TurboDepot documentation or reach out to the community for assistance. Happy coding!