Aller au contenu

Archiver et compresser

tar

Créer une archive tar: tar -cvf

$ tar -cvf archive.tar .profile .bashrc test-dossier/

Afficher le contenu de l'archive sans l'extraire: tar -tf

$ tar -tf archive.tar
.profile
.bashrc
test-dossier/
test-dossier/test-copy-scp.txt

Ajouter un fichier à l'archive: tar -rvf

$ tar -rvf archive.tar .bash_history

$ tar -tf archive.tar
.profile
.bashrc
test-dossier/
test-dossier/test-copy-scp.txt
.bash_history

Extraire les fichiers de l'archive: tar -xvf

$ tar -xvf archive.tar

tgz

Extraire les fichiers de l'archive: tar zxvf

$ tar zxvf archive.tgz

gzip

Compresser avec gzip:

$ gzip archive.tar

$ ls
archive.tar.gz

Decompresser avec gunzip:

$ gunzip archive.tar.gz

$ ls
archive.tar

Voir le contenu de l'archive avec zcat:

bzip2

Compresser avec bzip2:

$ bzip2 archive.tar

$ ls
archive.tar.bz2

Decompresser avec bunzip2:

$ bunzip2 archive.tar.bz2

$ ls archive.tar

zip

Compresser avec zip:

zip <archive.zip> <file1> <file2>
zip <archive.zip> <file*>
$ zip p10k.zip p1Ok.zsh
  adding: p1Ok.zsh (deflated 63%)

Options:

  • -q : pas des sortie écran
  • -Z bzip2 : méthode de compression bzip2 (deflate par défaut)
  • -9 ... -0 : niveau de compression (maxi -> mini)
  • -e : avec mot-de-passe

Compresser un dossier avec zip:

$ zip -r scripts.zip scripts/
  adding: scripts/ (stored 0%)
  adding: scripts/@eaDir/ (stored 0%)
  adding: scripts/@eaDir/ffmpeg@SynoResource (deflated 84%)
  adding: scripts/@eaDir/ffmpeg@SynoEAStream (deflated 35%)
  adding: scripts/ffmpeg (deflated 64%)
  adding: scripts/ffmpeg-convertMP4v3.sh (deflated 50%)
  adding: scripts/running3.sh (deflated 48%)
  adding: scripts/ffmpeg-convertMP4v4.sh (deflated 49%)
  adding: scripts/ffmpeg-convertMP4v5.sh (deflated 56%)

Exclure les fichiers ressources invisibles (“MACOSX” “.Filename” “.ds store”):

$ zip -r -X scripts.zip scripts/

Décompresser avec zip:

$ unzip scripts.zip
Archive:  scripts.zip
   creating: scripts/
   creating: scripts/@eaDir/
  inflating: scripts/@eaDir/ffmpeg@SynoResource
  inflating: scripts/@eaDir/ffmpeg@SynoEAStream
  inflating: scripts/ffmpeg
  inflating: scripts/ffmpeg-convertMP4v3.sh
  inflating: scripts/running3.sh
  inflating: scripts/ffmpeg-convertMP4v4.sh
  inflating: scripts/ffmpeg-convertMP4v5.sh

Voir le contenu d'une archive:

$ unzip -l vegas.zip
Archive:  vegas.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  10-30-2018 13:43   overlays/
    15520  10-30-2018 13:43   vegas.css
    23957  10-30-2018 13:43   vegas.js
     9459  10-30-2018 13:43   vegas.min.css
    16251  10-30-2018 13:43   vegas.min.css.map
    10383  10-30-2018 13:43   vegas.min.js
    36089  10-30-2018 13:43   vegas.min.js.map
      100  10-30-2018 13:43   overlays/01.png
      100  10-30-2018 13:43   overlays/02.png
       98  10-30-2018 13:43   overlays/03.png
      100  10-30-2018 13:43   overlays/04.png
      102  10-30-2018 13:43   overlays/05.png
      100  10-30-2018 13:43   overlays/06.png
      104  10-30-2018 13:43   overlays/07.png
      106  10-30-2018 13:43   overlays/08.png
      118  10-30-2018 13:43   overlays/09.png
---------                     -------
   112587                     16 files

Extraire dans un dossier différent:

$ unzip -l vegas.zip -d $HOME/Desktop

Dézipper une archive protégée:

$ unzip -P password vegas.zip

tzst (Z Standard)

$ sudo apt install zstd

$ brew install zstd

Compresser:

# Tous les .sh individuellement
$ zstd *.sh
 26 files compressed :44.75%   (  24.7 KiB =>   11.1 KiB)

# 1 archive pour tous les .sh
$ tar --zstd -cf scripts.tar.zst *.sh

# Niveaux de compression: -1 à -19
# Niveaux ultra (19 à 22): --ultra -22

Décompresser:

$ unzstd scripts.tar.zst
$ zstd -d scripts.tar.zst
scripts.tar.zst     : 117248 bytes

$ tar --zstd -xf scripts.tar.zst

BetterZip (macOS)

Peazip (Windows)

7-zip-zstd (command-line)

dmg (macOS)

Créer:

$ hdiutil create -format UDZO -srcfolder hubiC/ hubic.dmg
.........
created: /Users/bruno/hubic.dmg

Différents formats d'image:

  • UDZO – Compressed image (default)
  • UDRO – Read only image
  • UDBZ – Better compressed image
  • UDRW – Read/Write image
  • UDTO – DVD disk image

Monter:

$ hdiutil attach hubic.dmg
Calcul de la somme de contrôle de Protective Master Boot Record (MBR : 0)…
Protective Master Boot Record (MBR : : vérifiée   CRC32 $DC586CEA
Calcul de la somme de contrôle de GPT Header (Primary GPT Header : 1) GPT Header (Primary GPT Header : 1) : vérifiée   CRC32 $4EE88DC7
Calcul de la somme de contrôle de GPT Partition Data (Primary GPT Table : 2)…
GPT Partition Data (Primary GPT Tabl : vérifiée   CRC32 $42D33ACB
Calcul de la somme de contrôle de  (Apple_Free : 3)                    (Apple_Free : 3) : vérifiée   CRC32 $00000000
Calcul de la somme de contrôle de disk image (Apple_HFS : 4)…
.........................................................................................................................
          disk image (Apple_HFS : 4) : vérifiée   CRC32 $D4406A43
Calcul de la somme de contrôle de  (Apple_Free : 5)                    (Apple_Free : 5) : vérifiée   CRC32 $00000000
Calcul de la somme de contrôle de GPT Partition Data (Backup GPT Table : 6)…
GPT Partition Data (Backup GPT Table : vérifiée   CRC32 $42D33ACB
Calcul de la somme de contrôle de GPT Header (Backup GPT Header : 7)  GPT Header (Backup GPT Header : 7) : vérifiée   CRC32 $16D559B6
vérifiée   CRC32 $BE421D06
/dev/disk3              GUID_partition_scheme
/dev/disk3s1            Apple_HFS                          /Volumes/hubiC

Voir:

$ ls -lah /Volumes/hubiC/
total 8
drwxr-xr-x  11 bruno  staff   442B 12 aoû 12:05 .
drwxr-xr-x@  6 root   wheel   204B 12 aoû 12:07 ..
drwxr-xr-x   2 bruno  staff    68B  6 aoû 17:39 Documents
drwxr-xr-x   3 bruno  staff   102B  6 aoû 17:39 HubiC
-rw-r--r--   1 bruno  staff   669B  6 aoû 17:39 HubiC readme.txt
drwxr-xr-x   2 bruno  staff    68B  6 aoû 17:39 Images
drwxr-xr-x   2 bruno  staff    68B  6 aoû 17:39 Videos
drwxr-xr-x   9 bruno  staff   306B  6 aoû 17:39 scripts plex 0.9xx
drwxr-xr-x   9 bruno  staff   306B  6 aoû 17:39 scripts plex 1.0xx

Ejecter:

$ hdiutil eject /Volumes/hubiC/
"disk3" unmounted.
"disk3" ejected.

Archiver et compresser en même temps (gzip)

Archiver et compresser en gzip: tar -zcvf

$ tar -zcvf archive.tar.gz .bashrc .profile test-dossier/
.bashrc
.profile
test-dossier/
test-dossier/test-copy-scp.txt

$ ls
archive.tar.gz

Voir le contenu de l'archive gzip: tar -ztf

$ tar -ztf archive.tar.bz2

Décompresser: tar -xzvf

$ tar -xzvf archive.tar.gz
.bashrc
.profile
test-dossier/
test-dossier/test-copy-scp.txt

$ ls -la
drwxr-xr-x 3 root root 4096 Aug 6 07:45 .
drwx—— 8 root root 4096 Aug 6 07:18 ..
-rw-r–r– 1 root root 953 Aug 5 11:04 .bashrc
-rw-r–r– 1 root root 497 Aug 5 10:15 .profile
-rw-r–r– 1 root root 925 Aug 6 07:42 archive.tar.gz
drwxr-xr-x 2 root root 4096 Aug 6 07:04 test-dossier

# décompresser dans un dossier cible
$ tar -xzvf archive.tar.gz -C /tmp/

Archiver et compresser en même temps (bz2)

Décompresser: tar -xf (+ v pour mode verbose)

$ tar -xf nextcloud-11.0.2.tar.bz2
$ ls -la

total 137156

d---------+ 1 root   root       308 Apr 15 16:58 .
d---------+ 1 root   root       306 Apr 15 16:57 ..
----------+ 1 bruno  users  3045723 Oct 20 07:07 gitlist-0.5.0.tar.gz
drwxr-xr-x  1 nobody 65534      414 Feb 26 20:44 nextcloud
----------+ 1 bruno  users 38598274 Apr 15 09:48 nextcloud-11.0.2.tar.bz2

Extraire un seul fichier d'une archive tar.gz

Extrait le fichier README de l'archive automysqlbackup-v3.0_rc6.tar.gz

$ tar --extract --file=automysqlbackup-v3.0_rc6.tar.gz README

Extrait le fichier book.enchant.html du dossier php-chunked-xhtml de l'archive phpmanualfr.tar.gz

$ tar --extract --file=php_manual_fr.tar.gz php-chunked-xhtml/book.enchant.html

Extraire plusieurs fichiers d'archive:

$ tar xvf php_manual_fr.tar.gz php-chunked-xhtml/book.enchant.html php-chunked-xhtml/function.mysql-connect.html

Extraire un seul fichier d'une archive zip

Connaitre la liste des fichiers de l'archive:

$ unzip -l zenphoto-zenphoto-1.4.14.zip

Extraire des fichiers de l'archive:

$ unzip zenphoto-zenphoto-1.4.14.zip zenphoto-zenphoto-1.4.14/zp-core/zp-extensions/zenpage/zenpage.css zenphoto-zenphoto-1.4.14/zp-core/zp-extensions/tinymce4/skins/lightgray/img/anchor.gif
Archive:  zenphoto-zenphoto-1.4.14.zip
4c21854e7a7950ec8d9644a959da019d9781d36c
  inflating: zenphoto-zenphoto-1.4.14/zp-core/zp-extensions/tinymce4/skins/lightgray/img/anchor.gif
  inflating: zenphoto-zenphoto-1.4.14/zp-core/zp-extensions/zenpage/zenpage.css

Dézipper tous les fichiers d'un dossier:

$ unzip '.zip'

Dernière mise à jour: March 4, 2022