This file describes all UnifiedArchive API.
TOC
UnifiedArchive is represented by few basic classes under \wapmorgan\UnifiedArchive namespace:
Formatskeeps information about formats support and specific format functions.Abilitieslist.UnifiedArchive- represents an archive and provides related functions.ArchiveEntry- represents information about a specific file from archive. This object can be obtained by callUnifiedArchive->getFileData()method.
Formats
$format should be one of Formats constants (such as Formats::ZIP and so on).
Full list of constants provided in the appendix of this document.
If you want to enabled specific format support, you need to install an additional program or php extension. List of
extensions that should be installed can be obtained by executing built-in cam:
./vendor/bin/cam system:drivers
All methods are static.
| Method | Arguments | Result | Description |
|---|---|---|---|
Formats::detectArchiveFormat |
string $archiveFileName, bool $contentCheck = true |
?string |
Detects a format of given archive by file name and content (when $contentCheck = true). Returns one of Formats constant or null if format is not detected. |
Formats::getFormatMimeType |
string $format |
?string |
Returns mime type for passed format. Returns null if not found. |
Formats::can |
string $format, int $ability |
boolean |
Check if any driver supports passed ability for passed format |
Formats::canOpen, Formats::canStream, Formats::canCreate, Formats::canAppend, Formats::canUpdate, Formats::canEncrypt |
string $format |
boolean |
Tests if an archive format can be opened/created/appended (add)/updated (delete)/created encrypted by any driver with current system and php configuration. |
Formats list
Formats::ZIPFormats::SEVEN_ZIPFormats::RARFormats::CABFormats::TARFormats::TAR_GZIPFormats::TAR_BZIPFormats::TAR_LZMAFormats::TAR_LZWFormats::ARJFormats::GZIPFormats::BZIPFormats::LZMAFormats::UEFIFormats::GPTFormats::MBRFormats::MSIFormats::ISOFormats::DMGFormats::UDFFormats::RPMFormats::DEB
Abilities
Abilities::OPENAbilities::OPEN_ENCRYPTEDAbilities::OPEN_VOLUMEDAbilities::GET_COMMENTAbilities::EXTRACT_CONTENTAbilities::STREAM_CONTENTAbilities::SET_COMMENTAbilities::APPENDAbilities::DELETEAbilities::CREATEAbilities::CREATE_ENCRYPTEDAbilities::CREATE_IN_STRING
UnifiedArchive
- Opening an archive
- Archive information:
UnifiedArchive->getFormatUnifiedArchive->getSizeUnifiedArchive->getCompressedSizeUnifiedArchive->getOriginalSizeUnifiedArchive->countFilesUnifiedArchive->getComment
- Extracting an archive:
- Updating an archive:
- Making an archive:
Archive opening
-
UnifiedArchive::canOpen(string $fileName, bool $passwordProtected = false): booleanTests if an archive (format is resolved by extension) can be opened with current system and php configuration. _If you want to enabled specific format support, you need to install an additional program or php extension. List of extensions that should be installed can be obtained by executing built-in
cam: ` ./vendor/bin/cam system:formats_ Returnstrueif given archive can be opened andfalse` otherwise. -
UnifiedArchive::open( string $fileName, int[] $abilities = [], ?string $password = null ): UnifiedArchive|nullOpens an archive and returns instance of
UnifiedArchive. If you provide$password, it will be used to open encrypted archive. If you provide$abilities, it will be used to determine driver for format, that supports ALL of passed abilities. In case of failure (file is not readable or format is not supported or recognized), anInvalidArgumentExceptionorUnsupportedArchiveExceptionwill be thrown. -
UnifiedArchive::getPclZipInterface(): PclzipZipInterfaceReturns a
PclzipZipInterfacehandler for an archive. It provides all PclZip functions in PclZip-like interface for an archive. -
UnifiedArchive::can(int $ability): booleanChecks if current driver supports ability for archive.
Archive information
All following methods is intended to be called to UnifiedArchive instance.
| Method | Result | Description |
|---|---|---|
UnifiedArchive::getFormat() |
string |
Returns format of archive as one of Formats constants. |
UnifiedArchive::getMimeType() |
string/null |
Returns mime type of archive. |
UnifiedArchive::getSize() |
int |
Returns size of archive file in bytes. |
UnifiedArchive::getCompressedSize() |
int |
Returns size of all stored files in archive with archive compression in bytes. This can be used to measure efficiency of format compression. |
UnifiedArchive::getOriginalSize() |
int |
Returns size of all stored files in archive without compression in bytes. This can be used to measure size that extracted files will use. |
UnifiedArchive::countFiles() |
int |
Returns number of files stored in an archive. |
UnifiedArchive::getComment() |
?string |
Returns comment of archive or null (if not supported nor present). |
Archive content
-
UnifiedArchive::getFiles(?string $filter = null): string[]Returns full list of files stored in an archive. If
$filteris passed, will return only matched byfnmatch()files. -
UnifiedArchive::hasFile(string $fileName): booleanChecks whether file is presented in an archive.
-
UnifiedArchive::getFileData(string $fileName): ArchiveEntryReturns
ArchiveEntrythat contains all specific information about file stored in archive and described later in the document. If file is not in archive,NonExistentArchiveFileExceptionis thrown. -
UnifiedArchive::getFileStream(string $fileName): resourceReturns a resource of in-archive file that can be used to get it’s content (by
fread()and so on). This method of extraction is useful for large files or when you need to read files in portions. If file is not in archive,NonExistentArchiveFileExceptionis thrown. -
UnifiedArchive::getFileContent(string $fileName): stringReturns content of in-archive file as raw string. If file is not in archive,
NonExistentArchiveFileExceptionis thrown. -
UnifiedArchive::extract(string $outputFolder): intExtracts all archive content with full paths to output folder and rewriting existing files. In case of success, number of extracted files is returned.
Throws:
ArchiveExtractionException
-
UnifiedArchive::extract( string $outputFolder, array $files, boolean $expandFilesList = false ): int|falseExtracts given files or directories to output folder. If directories is passed, you need to use
$expandFilesListfeature that will expand directory names to all nested files (e.gdirwill be expanded todir/file1, dir/file2, dir/subdir/file3). In case of success, number of extracted files is returned.Throws:
EmptyFileListExceptionArchiveExtractionException
Archive modification
-
UnifiedArchive::addFileFromString( string $inArchiveName, string $content ): booleanPacks file in an archive. If case of success,
trueis returned.Throws:
ArchiveModificationException
-
UnifiedArchive::add(array|string $fileOrFilesList): int|falsePacks given
$fileOrFilesListinto archive.$fileOrFilesListis a string or an array of files or directories. Syntax of$fileOrFilesListis described inUnifiedArchive::createmethod. If any error occurred (such as file already exists, files list is empty, …), an\Exceptionis throwing. In case of success, number of packed files will be returned.Throws:
EmptyFileListExceptionUnsupportedOperationExceptionArchiveModificationException
-
UnifiedArchive::delete( string|array $fileOrFiles, $expandFilesList = false ): int|falseDeletes passed
$fileOrFilesfrom archive.$fileOrFilesis a string with file/directory name or an array of files or directories. In case of success, number of deleted files will be returned.Throws:
EmptyFileListExceptionUnsupportedOperationExceptionArchiveModificationException
Making an archive
-
UnifiedArchive::create( array|string $files, string $archiveName, int $compressionLevel = BasicFormat::COMPRESSION_AVERAGE, ?string $password = null, ?callable $fileProgressCallable = null ): intCreates an archive with given
$fileslist and saves on disk.$filesis an array of files or directories. If file/directory passed with numeric key (e.g['file', 'directory']), then file/directory will have it’s full (not relative) path in an archive. If passed with string keys, then there’s path replacement. Example:'/var/www/original/name.log',- file /var/www/original/name.log will be store with its original name;'/var/www/site/runtime/logs',- directory contents will be stored as /var/www/site/runtime/logs, with its original name;'insideArchiveName.log' => '/var/www/original/name.log',- file /var/www/original/name.log will be stored as insideArchiveName.log in archive root;'insideArchiveDir' => '/var/www/site/runtime/logs',- directory contents will be stored in insideArchiveDir dir inside archive;'' => ['/home/user1/docs', '/home/user2/docs'],- directories user1 and user2 docs contents will be merged and stored in archive root;
In case of success, number of stored files will be returned.
Creates an archive with all content from given directory and saves archive to
$archiveName(format is resolved by extension). All files have relative path in the archive. By$compressionLevelyou can adjust compression level for files. By$passwordyou can set password for an archive. If case of success,trueis returned.Available values for compression:
BasicFormat::COMPRESSION_NONEBasicFormat::COMPRESSION_WEAKBasicFormat::COMPRESSION_AVERAGEBasicFormat::COMPRESSION_STRONGBasicFormat::COMPRESSION_MAXIMUM
Throws:
UnsupportedOperationExceptionFileAlreadyExistsExceptionEmptyFileListExceptionArchiveCreationException
-
UnifiedArchive::createInString( array|string $fileOrFiles, string $archiveFormat, int $compressionLevel = BasicDriver::COMPRESSION_AVERAGE, ?string $password = null, ?callable $fileProgressCallable = null )Creates an archive with given
$fileOrFileslist and returns as string.$fileOrFilesis an array of files or directories.
ArchiveEntry
The class represents a file from archive as result of a call to UnifiedArchive::getFileData(string $fileName).
It contains fields with file information:
string $path- full file path in archive.integer $modificationTime- time of change of the file (the integer value containing number of seconds passedboolean $isCompressed- a flag indicates if file stored with compression.integer $compressedSize- size of the file with compression in bytes.integer $uncompressedSize- size of the file without compression in bytes. since the beginning of an era of Unix).