PHP code example of kiwilan / php-archive

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

    

kiwilan / php-archive example snippets


$archive = Archive::read('path/to/archive.zip');

$files = $archive->getFileItems(); // ArchiveItem[]
$count = $archive->getCount(); // int of files count

$images = $archive->filter('jpeg'); // ArchiveItem[] with `jpeg` in their path
$metadataXml = $archive->find('metadata.xml'); // First ArchiveItem with `metadata.xml` in their path
$content = $archive->getContents($metadataXml); // `metadata.xml` file content

$paths = $archive->extract('/path/to/directory', [$metadataXml]); // string[] of extracted files paths
$paths = $archive->extractAll('/path/to/directory'); // string[] of extracted files paths

$archive = Archive::read('path/to/file.pdf');

$pdf = $archive->getPdf(); // Metadata of PDF

$content = $archive->getContents($archive->getFirst()); // PDF page as image
$text = $archive->getText($archive->getFirst()); // PDF page as text

$archive = Archive::readFromString($string);

$archive = Archive::readFromString($string, extension: 'zip');

$archive = Archive::read('path/to/password-protected-archive.zip', 'password');

$archive = Archive::read($path)->overrideBinaryPath('/opt/homebrew/bin/7z');

$archive = Archive::read('path/to/file.zip');
$stat = $archive->stat();

$stat->getPath(); // Path of file
$stat->getDeviceNumber(); // Device number
$stat->getInodeNumber(); // Inode number
$stat->getInodeProtectionMode(); // Inode protection mode
$stat->getNumberOfLinks(); // Number of links
$stat->getUserId(); // User ID
$stat->getGroupId(); // Group ID
$stat->getDeviceType(); // Device type
$stat->getSize(); // Size of file
$stat->getLastAccessAt(); // Last access time
$stat->getCreatedAt(); // Creation time
$stat->getModifiedAt(); // Last modification time
$stat->getBlockSize(); // Block size
$stat->getNumberOfBlocks(); // Number of blocks
$stat->getStatus(); // Status

$archive = Archive::make('path/to/archive.zip');
$files = [
    'path/to/file/in/archive-file1.txt' => 'path/to/real-file1.txt',
    'path/to/file/in/archive-file2.txt' => 'path/to/real-file2.txt',
    'path/to/file/in/archive-file3.txt' => 'path/to/real-file3.txt',
];

foreach ($files as $pathInArchive => $pathToRealFile) {
    $archive->addFile($pathInArchive, $pathToRealFile);
}
$archive->addFromString('test.txt', 'Hello World!');
$archive->addDirectory('./directory', 'path/to/directory');
$archive->save();

$archive = Archive::make('path/to/archive.zip');
$archive->addFromString('test.txt', 'Hello World!');
$archive->save();
bash
composer