Compresser fichiers et dossiers avec zip

How to ZIP Files and Directories

Extrait de l’article :

To zip one or more files, specify the files you want to add to the archive separated by space, as shown below:

zip filename1 filename2 filename3
adding: filename1 (deflated 63%)
adding: filename2 (stored 0%)
adding: filename3 (deflated 38%)

By default, the zip command prints the names of the files added to the archive and the compression method. We’ll explain the compression methods and levels later in this guide.

If the archive name doesn’t end with .zip, the extension is added automatically unless the archive name contains a dot. zip filename will create an archive with the same name as would zip archivename filename.

To suppress the output of the zip command, use the -q option:

zip -q filename1 filename2 filename3

Often, you’ll create a zip archive of a directory including the content of subdirectories. The -r option allows you to traverse the whole directory structure recursively:

zip -r directory_name

You can also add multiple files and directories in the same archive:

zip -r directory_name1 directory_name2 file1 file1

Compression Methods and Levels

The default compression method of Zip is deflate. If the zip utility determines that a file cannot be compressed, it simply stores the file in the archive without compressing it using the store method. In most Linux distributions, the zip utility also supports the bzip2 compression method.

To specify a compression method, use the -Z option.

zip -r -Z bzip2 directory_name
adding: sub_dir/ (stored 0%)
adding: sub_dir/file1 (bzipped 52%)
adding: sub_dir/file2 (bzipped 79%)

The zip command allows you to specify a compression level using a number prefixed with a dash from 0 to 9. The default compression level is -6. When using -0, all files will be stored without compression. -9 will force the zip command to use an optimal compression for all files.

For example, to use the compression level -9, you would type something like this:

zip -9 -r directory_name

The higher the compression level, the more CPU-intensive the zip process is, and it will take more time to complete.

Creating a Password Protected ZIP file

If you have sensitive information that needs to be stored in the archive, you can encrypt it using the -e option:

zip -e directory_name

The command will be prompted to enter and verify the archive password:

Enter password:
Verify password: