Compressing and decompressing zip archives on the command line requires a command line program that knows how to process zip archives. There are a number of programs that can do this. Some common ones are ‘zip’, ‘unzip’ and ‘7-Zip’. These can be installed on most Linux systems using a package manager. For Windows, there is no built-in command available for working with zip files, but ‘7-Zip’ can be installed and made to work on the command line. This might involve adding the path to the ‘7-Zip’ executable (7z.exe) to the PATH environment variable. The following are some example command lines using ‘7-Zip’:
7z a archive.zip file.txt
Add a single file to an archive (archive.zip). If the archive does not exist yet, it will be created. If the file already exists in the archive, it will be overwritten.
7z a archive.zip *.txt
Add all files with the extension ‘.txt’ in the current directory to an archive.
7z a -r archive.zip folder
Add all files in a folder (folder) to an archive. This command will recursively add all files, so the entire directory structure will be in the archive. 7-zip maintains the folder structure, so the root of the archive will only contain a single folder called folder.
7z a -r ../archive.zip .
Recursively add all files and folders in the current directory to an archive. This can be done if you don’t want the folder name in the archive. (However, the above command is usually more convenient and intuitive.) In this case, the archive is created in the folder below the current one. It can be created in the current folder, but if the archive already exists, the existing archive will be included in the new one.
find folder -name “*.txt” | xargs 7z a archive.zip
Recursively add all ‘.txt’ files within folder to an archive. The directory structure will be preserved in the zip file. The root of the archive will contain only ‘folder’.
7z a -psecret secret.zip secret.txt
Create a password-protected archive. These archives are encrypted and require a password to unzip.
7z x archive.zip
Extract archive.zip to the current folder.