Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Compressing Files With 7zip CLI

7zip is a really useful tool when you want to send .zip or .7z archives to other users. If the other side does not know what 7zip is, then you can still use it to create zip files. I also prefer to use the 7zip files instead of zip files for encrypted archives because it forces the other side to use the 7zip tool, which will automatically recognize if the archive is password protected. For some reason the default Windows tool will not notice if the archive is password protected, and you have to manually tell it.

Steps

Create Archive (Basic)

7z a \
  output-filename.7z \
  input-folder

Extract Archive (Basic)

7z x my-file.7z

You may find e instead of x when Googling "extraction", but you probably want x which takes into account full-paths, which is what you are probably used to.

Specify Compression Level

You can specify the compression level with -mx. It takes a value between 0-9. E.g. to compress the most, but take longer, use 9, and to compress the least, but do the job quickly, use 0.

7z a \
  -mx=9 \
  output-filename.7z \
  input-folder

Fast Encrypt (Including Filenames)

The following command will create an encrypted 7zip file as fast as it can (least compression), encrypting not only the files, but the filenames as well. It will prompt you to enter a password securely, rather than you passing it in the commmand.

7z a \
  -t7z \
  -mx=0 \
  -mhe \
  -p \
  output-filename.7z \
  input-folder

The -p parameter tells 7zip that we are encrypting the archive with a password. Adding the -mhe switch is what tells 7zip to also encrypt the filenames.

References

Last updated: 23rd December 2020
First published: 23rd December 2020

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch