PHP code example of ntdash / zipstore

1. Go to this page and download the library: Download ntdash/zipstore library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ntdash / zipstore example snippets


use ZipStore\Store;

// Create a store
$store = new Store();

// Add files
$store->addFile("path/to/file.json");
$store->addFiles([
    "path/to/video.webm",
    "path/to/sample.png"
]);

// Snapshot the store's current entry list into an independent OpenedStore.
// The Store remains open — further addFile() calls won't affect this snapshot.
$openedStore = $store->open();

// Read bytes from any position
$offset = 0;
$bytes = 1024 * 1024 * 4; // 4 MiB

$openedStore->seek($offset);
$buffer = $openedStore->read($bytes);

// current offset after seek and read
echo $openedStore->tell();  // 4194304 

// Read with specific offset
$buffer = $openedStore->read(length: 1024 * 1024 * 4, offset: 1024);

// Default read size is 512 KiB 
$buffer = $openedStore->read(offset: 2048);

// Check if end of file
if ($openedStore->eof()) {
    echo "Reached end of ZIP file";
}

// Get total ZIP file size
$totalSize = $openedStore->getSize();

$store->addFile(
    "path/to/original-file.txt",
    "custom-name-in-zip.txt"
);

use ZipStore\Store;

// Append numerical suffix to duplicates (default)
$store = new Store(Store::DUP_APPEND_NUM);

// Overwrite previous entry with same name
$store = new Store(Store::DUP_OVERWRITE);

// Throw exception on duplicates
$store = new Store(Store::DUP_FAILED | Store::STRICT);

$opened = $store->open(); 

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="archive.zip"');
header('Content-Length: ' . $opened->getSize());

$opened->passthru();

use ZipStore\Entry;
use ZipStore\Store;
use ZipStore\EntryArgument;

// Register your custom entry file class
EntryArgument::setEntryFileClass(CustomEntryFile::class);

// Now when you add entries, they'll use your custom handler
$store = new Store();
$store->addFile('custom-data-identifier');
// Snapshot the current entry list — returns an independent OpenedStore cursor
$opened = $store->open();

use ZipStore\OpenedStore;

// SEEK_SET (0): Absolute position from start
$opened->seek(1024, SEEK_SET);

// SEEK_CUR (1): Relative to current position
$opened->seek(512, SEEK_CUR);

// SEEK_END (2): Relative to end of file
$opened->seek(-1024, SEEK_END);

// No special options (default)
$store = new Store(Store::NO_EXTRA);

// Append numbers to duplicate names
$store = new Store(Store::DUP_APPEND_NUM);

// Overwrite duplicate names
$store = new Store(Store::DUP_OVERWRITE);

// Strict mode: throw exceptions on invalid entries
$store = new Store(Store::STRICT);

// Combine options
$store = new Store(Store::DUP_APPEND_NUM | Store::STRICT);



use ZipStore\Store;

$store = new Store();

// Add multiple files
$store->addFile('/var/www/document.pdf', 'document.pdf');
$store->addFile('/var/www/image.jpg', 'image.jpg');
$store->addFile('/var/www/video.mp4', 'video.mp4');

$opened = $store->open();

// Stream to browser
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="download.zip"');
header('Content-Length: ' . $opened->getSize());
header('Cache-Control: public, must-revalidate');

$opened->passthru();

$store = new Store();
$stream = fopen("backup.zip", "w"); 

foreach (glob('/data/backups/*.log') as $logFile) {
    $store->addFile($logFile);
}

$opened = $store->open();

$opened->writeToStream($stream, resetOffset: true);

use ZipStore\Entry;
use ZipStore\Store;
use ZipStore\EntryArgument;


// First, define your custom file handler
class CustomEntryFile implements ZipStoreEntryFile {

    public function __construct(private readonly int $id) {
       // extra ops (eg. validation, ...)
    }

    ...

    // a simple implementation of read API ch();
        return $result ? $result['data'] : false;
    }

    ...
}

// register your custom file handler
EntryArgument::setEntryFileClass(CustomEntryFile::class);

$store = new Store();

// Add database records as ZIP entries
foreach ($database->query('SELECT id, name FROM files') as $row) {
    $store->addFile($row['id'], sprintf('%s-%d.bin', $row['name'], $row['id']));
}

$opened = $store->open();

// Stream to output
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="database-export.zip"');
header('Content-Length: ' . $opened->getSize());


// equivalent (current internal) of OpenedStore@passthru() 
$opened->writeTo(path: "php://output", resetOffset: true);