A PHP package to compress and decompress files using 7zip CLI.
Install the package via Composer:
composer require verseles/sevenzip
To compress a file or directory:
$sevenZip = new SevenZip();
$sevenZip->format('7z')
->source('/path/to/source/file/or/directory')
->target('/path/to/archive.7z')
->compress();
- Format: You can set the compression format using the
format()
method. Well known formats include7z
,zip
,tar
,lz4
,lz5
,bzip2
, andzstd
. Your OS needs to support the selected format. - Compression Level: You can adjust the compression level using the
faster()
,slower()
,mx()
, andultra()
methods. - Custom Flags: You can add custom compression flags using the
addFlag()
method. - Progress Callback: You can set a progress callback using the
progress()
method to monitor the compression progress. - Tar Before: You can enable option to create a tar archive before compressing using the
tarBefore()
method. This is useful for preserving file permissions and attributes.
Warning
The format support depends on your system, architecture, etc.
You can always use format()
method to set your custom format.
Any set
format()
which starts withtar.
will automatically enabletarBefore()
. To disable add->forceTarBefore(false)
To extract an archive:
$sevenZip = new SevenZip();
$sevenZip->source('/path/to/archive.7z')
->target('/path/to/extract/directory')
->extract();
If the extracted file is a single tar archive, it will be extracted and the tar file deleted. To avoid this behavior, add
->autoUntar(false)
You can encrypt the compressed archive using a password:
$sevenZip = new SevenZip();
$sevenZip->source('/path/to/source/file/or/directory')
->target('/path/to/encrypted_archive.7z')
->encrypt('your_password')
->compress();
By default, the file names are also encrypted (not possible in zip format). If you want to disable file name encryption, you can use
the notEncryptNames()
method:
$sevenZip->notEncryptNames();
For ZIP archives, you can specify the encryption method using the setZipEncryptionMethod()
method. Available options are 'ZipCrypto' (not secure), '
AES128', 'AES192', or 'AES256'. The default is 'AES256'.
$sevenZip->setZipEncryptionMethod('AES256');
To decrypt an encrypted archive during extraction:
$sevenZip = new SevenZip();
$sevenZip->source('/path/to/encrypted_archive.7z')
->target('/path/to/extract/directory')
->decrypt('your_password')
->extract();
SevenZip allows you to include or exclude specific files when compressing or extracting archives.
To include specific files in the archive, use the include
method:
$sevenZip->include('*.avg')->compress();
To exclude specific files from the archive, use the exclude
method:
$sevenZip->exclude(['*.md', '*.pdf'])->compress();
Note that you can use both include
and exclude
methods together to fine-tune the files included in the archive.
You can pass a single file pattern, an array of file patterns or the path to a txt file with a list of patterns inside to the
exclude
andinclude
methods.
You can check if a specific format or multiple formats are supported by the current 7-Zip installation using the checkSupport method:
$sevenZip = new SevenZip();
// Check if a single format is supported
if ($sevenZip->checkSupport('zip')) {
echo "ZIP format is supported.";
} else {
echo "ZIP format is not supported.";
}
// Check if multiple formats are supported
if ($sevenZip->checkSupport(['zip', 'tar', '7z'])) {
echo "ZIP, TAR, and 7Z formats are supported.";
} else {
echo "One or more formats are not supported.";
}
- Full support for add flags (7z switches)
- Add custom support for gz, xz, etc. by using tar flags
- Use tar to keep original file permissions and other attributes
- Auto untar on extraction
- Filter files by patterns
- Encrypt and decrypt
- Test files using 7z test command
- Detect supported formats by the OS
- Add built-in binaries for mac and linux
-
Use docker for PHPUnit testsnot needed with built-in binaries - Use native zstd, brotli, and others as fallback for these formats
Contributions are welcome! If you'd like to contribute to this project, please follow these steps:
- Fork the repository
- Create a new branch for your feature or bug fix
- Make your changes and commit them with descriptive commit messages
- Push your changes to your forked repository
- Submit a pull request to the main repository
Please ensure that your code follows the project's coding style and conventions. Also, include appropriate tests for your changes.
To run the tests, execute the following command:
composer test
Here is the Documentation / API section of the README, updated with missing public methods and ordered alphabetically:
Adds a compression flag.
Parameters
$flag
: The compression flag to be added.$value
(optional): The value for the flag.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->addFlag('mfb', 64);
Checks if the given extension(s) are supported by the current 7-Zip installation.
Parameters
$extensions
: The extension or an array of extensions to check.
Returns: Returns true if all the given extensions are supported, false otherwise.
Example
$sevenZip = new SevenZip();
// Check if a single format is supported
$isZipSupported = $sevenZip->checkSupport('zip');
// Check if multiple formats are supported
$areFormatsSupported = $sevenZip->checkSupport(['zip', 'tar', '7z']);
Compresses a file or directory.
Returns: the command output on success.
Throws
InvalidArgumentException
: If target path, or source path is not set.
Example
$sevenZip->compress();
Configures no compression (copy only) settings based on the specified format.
Returns: The current instance for method chaining.
Decrypts the data using the provided password.
Parameters
$password
: The password to decrypt the data.
Returns: The current instance of this class.
Encrypts the data using the provided password.
Parameters
$password
: The password to encrypt the data.
Returns: The current instance of this class.
Extracts an archive.
Returns: the command output on success.
Throws
InvalidArgumentException
: If format, archive path, or extract path is not set.
Example
$sevenZip->extract();
Sets the compression level to faster.
Returns: The current instance of the SevenZip class.
Sets whether to delete the source archive after successful extraction.
Parameters
$delete
: Whether to delete the source archive after extraction. Default istrue
.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->deleteSourceAfterExtract();
Retrieves information about the specified archive file.
Returns: The file information output from the 7-Zip command.
Throws
InvalidArgumentException
: If the archive path is not set.
Example
$info = $sevenZip->fileInfo();
Lists the contents of the specified archive file.
Returns: The file list output from the 7-Zip command.
Throws
InvalidArgumentException
: If the archive path is not set.
Example
$list = $sevenZip->fileList();
Formats flags and values into an array of strings suitable for passing to 7-Zip commands.
Parameters
$flagsAndValues
: An associative array of flags and their corresponding values. If the value is null, the flag will be added without an equal sign.
Returns: An array of formatted flag strings.
Example
$formattedFlags = $sevenZip->flagrize(['m0' => 'lzma2', 'mx' => 9]);
// Output: ['-m0=lzma2', '-mx=9']
Sets the archive format.
Parameters
$format
: The compression format to be used.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->format('7z');
Gets the custom compression flags.
Returns: The custom compression flags that have been added.
Gets whether or not file names are encrypted.
Returns: Whether or not file names are encrypted, or null if not set.
Gets the archive format.
Returns: The compression format to be used.
Returns information about 7-Zip, formats, codecs, and hashers.
Returns: The output from the 7-Zip command.
Gets the last reported progress.
Returns: The last reported progress percentage.
Gets the password used for encryption or decryption.
Returns: The password used for encryption or decryption, or null if not set.
Gets the path to the 7-Zip executable file.
Returns: Path to the 7-Zip executable file.
Gets the source path for compression/extraction.
Returns: The path to the source file or directory for compression or extraction.
Gets all supported format extensions from the given array.
Parameters
$formats
(optional): The array of format data. If not provided, the built-in information from 7-Zip will be used. Returns: An array of supported format extensions.
Example
$sevenZip = new SevenZip();
$supportedFormats = $sevenZip->getSupportedFormatExtensions();
if (in_array('zip', $supportedFormats)) {
echo "ZIP format is supported.";
} else {
echo "ZIP format is not supported.";
}
Gets the target path for compression/extraction.
Returns: The path to the target file or directory for compression or extraction.
Gets the encryption method used for ZIP archives.
Returns: The encryption method used for ZIP archives.
Excludes files from the archive based on the provided patterns.
Parameters
$patterns
: A single file pattern, an array of file patterns, or the path to a txt file with a list of patterns.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->exclude(['*.md', '*.pdf']);
Includes only the specified files in the archive based on the provided patterns.
Parameters
$patterns
: A single file pattern, an array of file patterns, or the path to a txt file with a list of patterns.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->include('*.avg');
Prints the information about 7-Zip, formats, codecs, and hashers.
Sets the dictionary size for the compression algorithm.
Parameters
$size
: The dictionary size.
Returns: The current instance of the SevenZip class.
Sets the size of the Fast Bytes for the compression algorithm.
Parameters
$bytes
: The size of the Fast Bytes. The default value (when set) is 64.
Returns: The current instance of the SevenZip class.
Sets the compression method for ZIP format.
Parameters
$method
: The compression method to be used. Can be 'Copy', 'Deflate', 'Deflate64', 'BZip2', 'LZMA', 'PPMd'.
Returns: The current instance of the SevenZip class.
Sets the memory limit for compression.
Parameters
$size
: The memory limit in megabytes or as a string (e.g., '32m').
Returns: The current instance of the SevenZip class.
Example
$sevenZip->mmem(32); // Set memory limit to 32 MB
Sets the number of CPU threads to use for compression.
Parameters
$threads
: The number of CPU threads to use, or 'on' or 'off'.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->mmt('on'); // Use all available CPU threads
$sevenZip->mmt(4); // Use 4 CPU threads
Sets the compression method.
Parameters
$method
: The compression method to be used.
Returns: The current instance of the SevenZip class.
Sets the number of passes for compression.
Parameters
$number
: The number of passes for compression.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->mpass(15); // Use 15 compression passes
Enables or disables solid compression mode.
Parameters
$on
: Whether to enable or disable solid compression mode. Can be a boolean, 'on', or 'off'.
Returns: The current instance of the SevenZip class.
Sets the compression level using the -mx
flag.
Parameters
$level
: The compression level to be used.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->mx(9); // Maximum compression
Sets the file analysis level.
Parameters
$level
: The file analysis level.
Returns: The current instance of the SevenZip class.
Disables encryption of file names.
Returns: The current instance of the SevenZip class.
Sets the progress callback using a fluent interface.
Parameters
$callback
: The callback function to be called during the compression progress.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->progress(function ($progress) {
echo "Progress: {$progress}%\n";
});
Removes a compression flag.
Parameters
$flag
: The compression flag to be removed.
Returns: The current instance of the SevenZip class.
Resets the property values to their original state.
Returns: The current instance of the SevenZip class.
Sets the custom compression flags.
Parameters
$customFlags
: The custom compression flags to be used.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->setCustomFlags(['mx' => 9, 'mfb' => 64]);
Sets whether or not to encrypt file names.
Parameters
$encryptNames
: Whether or not to encrypt file names.
Returns: The current instance of the SevenZip class.
Sets the archive format.
Parameters
$format
: The compression format to be used.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->setFormat('zip');
Sets the password for encryption or decryption.
Parameters
$password
: The password to be used for encryption or decryption.
Returns: The current instance of the SevenZip class.
Sets the progress callback.
Parameters
$callback
: The callback function to be called during the compression progress.
Returns: The current instance of the SevenZip class.
Sets the path to the 7-Zip executable file.
Parameters
$sevenZipPath
: Path to the 7-Zip executable file.
Returns: The current instance of the SevenZip class.
Sets the source path for compression/extraction.
Parameters
$path
: The path to the source file or directory for compression or extraction.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->setSourcePath('/path/to/source/file/or/directory');
Sets the target path for compression/extraction.
Parameters
$path
: The path to the target file or directory for compression or extraction.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->setTargetPath('/path/to/archive.7z');
Sets the encryption method for ZIP archives.
Parameters
$method
: The encryption method to be used. Can be 'ZipCrypto' (not secure), 'AES128', 'AES192', or 'AES256'.
Returns: The current instance of the SevenZip class.
Sets the compression level to slower.
Returns: The current instance of the SevenZip class.
Sets the source path for the compression or extraction operation.
Parameters
$path
: The source path.
Returns: The current instance of the SevenZip class.
Sets the target path for compression/extraction using a fluent interface.
Parameters
$path
: The path to the target file or directory for compression or extraction.
Returns: The current instance of the SevenZip class.
Enables creating a tar archive before compressing, preserving file permissions and attributes by default.
Parameters
$keepFileInfo
(optional): Whether to preserve file permissions and attributes when creating the tar archive. Default istrue
.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->tarBefore();
Sets whether to force creating a tar archive before compressing, regardless of the format.
Parameters
$force
: Whether to force creating a tar archive before compressing.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->forceTarBefore(true);
Sets whether to preserve file permissions and attributes when creating a tar archive before compressing.
Parameters
$keepFileInfo
: Whether to preserve file permissions and attributes.
Returns: The current instance of the SevenZip class.
Example
$sevenZip->keepFileInfoOnTar(false);
Gets whether forcing tar before compression is enabled.
Returns: Whether forcing tar before compression is enabled.
Gets whether preserving file permissions and attributes when creating a tar archive is enabled.
Returns: Whether preserving file permissions and attributes is enabled.
Checks if the source has already been tarred.
Returns: Whether the source has already been tarred.
Configures maximum compression settings based on the specified format.
Returns: The current instance for method chaining.
This package is open-sourced software licensed under the MIT license.
About 7zip binaries: Most of the source code is under the GNU LGPL license. The unRAR code is under a mixed license with GNU LGPL + unRAR restrictions. Check the license for details.