How to use the command 7za (with examples)
7za is a file archiver with a high compression ratio. It is similar to ‘7z’ but supports fewer file types and is cross-platform. It is a powerful command-line tool for creating, extracting, and managing compressed archives.
Use case 1: Archive a file or directory
Code:
7za a path/to/archive.7z path/to/file_or_directory
Motivation:
Archiving files or directories is useful when we want to reduce their size for storage or transfer purposes. The ‘a’ option in the command specifies that we want to create an archive.
Explanation:
a
: The ‘a’ option informs 7za that we want to create an archive.path/to/archive.7z
: Specifies the path and filename of the archive file to be created.path/to/file_or_directory
: Specifies the path to the file or directory we want to include in the archive.
Example output:
Compressing path/to/file_or_directory
Everything is Ok
Use case 2: Encrypt an existing archive (including file names)
Code:
7za a path/to/encrypted.7z -ppassword -mhe=on path/to/archive.7z
Motivation:
Encrypting an existing archive helps protect sensitive data during storage or transmission. The -ppassword option sets the password for encryption.
Explanation:
a
: Indicates that we want to add files to an archive.path/to/encrypted.7z
: Specifies the path and filename of the encrypted archive that will be created.-ppassword
: Sets the password for the encrypted archive.-mhe=on
: Enables file names encryption.
Example output:
Compressing path/to/archive.7z
Encrypting path/to/archive.7z
Everything is Ok
Use case 3: Extract an archive preserving the original directory structure
Code:
7za x path/to/archive.7z
Motivation:
Extraction of an archive with preserving the directory structure allows us to maintain the organization of the extracted files as they were originally archived.
Explanation:
x
: Indicates that we want to extract files from an archive.path/to/archive.7z
: Specifies the path and filename of the archive to be extracted.
Example output:
Extracting archive: path/to/archive.7z
Everything is Ok
Use case 4: Extract an archive to a specific directory
Code:
7za x path/to/archive.7z -opath/to/output
Motivation:
Extracting an archive to a specific directory allows us to control where the extracted files are stored instead of extracting them to the current directory.
Explanation:
x
: Indicates that we want to extract files from an archive.path/to/archive.7z
: Specifies the path and filename of the archive to be extracted.-opath/to/output
: Specifies the path of the directory where we want to extract the files.
Example output:
Extracting archive: path/to/archive.7z to path/to/output
Everything is Ok
Use case 5: Extract an archive to stdout
Code:
7za x path/to/archive.7z -so
Motivation:
Extracting an archive to stdout allows us to redirect the extracted files to another command or capture the output programmatically.
Explanation:
x
: Indicates that we want to extract files from an archive.path/to/archive.7z
: Specifies the path and filename of the archive to be extracted.-so
: Redirects the output to stdout.
Example output:
Contents of path/to/archive.7z
...
Use case 6: Archive using a specific archive type
Code:
7za a -t7z|bzip2|gzip|lzip|tar|... path/to/archive.7z path/to/file_or_directory
Motivation:
Archiving using a specific archive type allows us to choose the compression algorithm that best suits our needs.
Explanation:
a
: Indicates that we want to create an archive.-t7z|bzip2|gzip|lzip|tar|...
: Specifies the archive type to be used. Replace...
with the desired archive type.path/to/archive.7z
: Specifies the path and filename of the archive to be created.path/to/file_or_directory
: Specifies the path to the file or directory we want to include in the archive.
Example output:
Compressing path/to/file_or_directory using the specified archive type
Everything is Ok
Use case 7: List the contents of an archive
Code:
7za l path/to/archive.7z
Motivation:
Listing the contents of an archive allows us to view the files present in the archive without extracting them.
Explanation:
l
: Indicates that we want to list the contents of an archive.path/to/archive.7z
: Specifies the path and filename of the archive we want to list.
Example output:
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ---------
2022-01-01 08:00:00 ....A 123 456 path/to/file.txt
Use case 8: Set the level of compression
Code:
7za a path/to/archive.7z -mx=0|1|3|5|7|9 path/to/file_or_directory
Motivation:
Setting the level of compression allows us to balance the trade-off between compression ratio and speed when creating an archive. A higher value results in better compression but slower performance.
Explanation:
a
: Indicates that we want to create an archive.path/to/archive.7z
: Specifies the path and filename of the archive to be created.-mx=0|1|3|5|7|9
: Sets the level of compression. Choose a value between 0 (no compression) and 9 (maximum compression).path/to/file_or_directory
: Specifies the path to the file or directory we want to include in the archive.
Example output:
Compressing path/to/file_or_directory with the specified compression level
Everything is Ok
Conclusion:
The ‘7za’ command is a versatile tool for managing compressed archives. By understanding and utilizing its various options and arguments, you can efficiently create, extract, and manipulate archives for various use cases.