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();
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);
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\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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.