PHP code example of glucnac / ziparchivemanager

1. Go to this page and download the library: Download glucnac/ziparchivemanager 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/ */

    

glucnac / ziparchivemanager example snippets


use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;
use GlucNAc\ZipArchiveManager\ZipArchiveManager;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder
    ->new('test.zip')
    ->addFiles([
        '/path/to/file1.txt',
        '/path/to/dir/file2.txt',
    ])
    ->addFile('/path/to/file3.txt')
    ->build();

use GlucNAc\ZipArchiveManager\ZipArchiveManager;
use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder->buildWithFiles('test.zip', [
    '/path/to/file1.txt',
    '/path/to/dir/file2.txt',
    '/path/to/file3.txt',
]);

use GlucNAc\ZipArchiveManager\ZipArchiveManager;
use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder->buildWithFiles('test.zip', [
    '/path/to/file1.txt' => 'file1.txt',
    '/path/to/dir/file2.txt' => 'dir/file2.txt',
    '/path/to/file3.txt' => 'file3.txt',
]);

use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;
use GlucNAc\ZipArchiveManager\ZipArchiveManager;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder->buildWithFiles(
    'test.zip',
    ArchivableFileManager::getArchivableFilesFromPath('/path/to'),
);

use GlucNAc\ZipArchiveManager\ZipArchiveManager;
use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder
    ->new('test.zip')
    ->addFiles([
        '/path/to/file1.txt',
        '/path/to/dir/file2.txt',
    ])
    ->addFile('/path/to/file3.txt')
    ->build(false);

// Do something with the archive

// Close the archive: this will save the archive to the storage
$zipArchiveManager->close($zipArchive);

use GlucNAc\ZipArchiveManager\ZipArchiveManager;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');

// Assume the archive exists at /path/to/storage/archive/test.zip
$zipArchive = $zipArchiveManager->open('test.zip');

use GlucNAc\ZipArchiveManager\ZipArchiveManager;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');

// Assume the archive exists at /path/to/storage/archive/test.zip
$zipArchive = $zipArchiveManager->open('test.zip');

$zipArchiveManager->extractFiles($zipArchive, '/path/to/extracted/files/dir');

interface ArchivableFileInterface
{
    public function getFullPath(): string;
    public function getFileName(): string;
    public function getExtension(): string;

    /**
     * This method is used to get the name of the file inside the archive. It can be useful to rename
     * the file on the fly, or to put it in a subdirectory by returning a relative path.
     */
    public function getEntryName(): string;
    public function setEntryName(string|null $entryName): static;
}

$archivableFile = new ArchivableFile('/path/to/file1.txt');

$archivableFile->getFullPath();  // /path/to/file1.txt
$archivableFile->getFileName();  // file1.txt
$archivableFile->getExtension(); // txt
$archivableFile->getEntryName(); // /path/to/file1.txt (by default, the entry name equals the full path)

use GlucNAc\ZipArchiveManager\ArchivableFileManager;

$archivableFile = ArchivableFileManager::getArchivableFileFromPath('/path/to/file1.txt');
$archivableFile->setEntryName('file1.txt'); // This will be the path of the file in the archive

use GlucNAc\ZipArchiveManager\ArchivableFileManager;

[$file1, $file2, $file3] = ArchivableFileManager::getArchivableFilesFromPath('/path/to');

// By default, when using the ArchivableFileManager::getArchivableFilesFromPath method,
// the entry name of the files will be relative to the path passed to the method.
$file1->getEntryName(); // file1.txt
$file2->getEntryName(); // dir/file2.txt
$file3->getEntryName(); // file3.txt

use GlucNAc\ZipArchiveManager\ArchivableFile;

$splFileInfo = new SplFileInfo('/path/to/file.txt');

$archivableFile = SplFileInfoToArchivableFileTransformer::getArchivableFile($splFileInfo);