LoginSignup
31
26

Windows 11 では 7z をコマンドラインでも圧縮・解凍できるようになっていた

Last updated at Posted at 2024-05-31

Windows 11 23H2 以降、エクスプローラーが 7z、tar などの解凍に対応しました。さらに、Build 25992 では圧縮も可能となったようです。7-zip を追加インストールしなくてもこれらのアーカイブを取り扱えるのは嬉しいですよね。

new-archive-formats 1

一方で、PowerShell の Expand-Archive コマンドレットは拡張されておらず、ZIP 形式の解凍しかできません。

Expand-Archive .\archive.7z .\
GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\Program Files\WindowsApps\Microsoft.PowerShell_7.…
OperationStopped: File 'E:\test\archive.7z' does not appear to be a valid zip archive.

せっかく標準機能で 7z が取り扱えるようになったのですから、コマンドラインからも操作してみたいですよね。どうにかできないものか。

Windows 11 に同梱されている bsdtar (libarchive) をコマンドラインから利用する

実は、Windows 10 1803 以降には tar コマンドが同梱されています。この tar コマンドは GNU tar ではなく、FreeBSD 版 tar (libarchive) です:

tar.exe --help
tar.exe(bsdtar): manipulate archive files
First option must be a mode specifier:
  -c Create  -r Add/Replace  -t List  -u Update  -x Extract
Common Options:
  -b #  Use # 512-byte records per I/O block
  -f <filename>  Location of archive (default \\.\tape0)
  -v    Verbose
  -w    Interactive
Create: tar.exe -c [options] [<file> | <dir> | @<archive> | -C <dir> ]
  <file>, <dir>  add these items to archive
  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma
  --format {ustar|pax|cpio|shar}  Select archive format
  --exclude <pattern>  Skip files that match pattern
  -C <dir>  Change to <dir> before processing remaining files
  @<archive>  Add entries from <archive> to output
List: tar.exe -t [options] [<patterns>]
  <patterns>  If specified, list only entries that match
Extract: tar.exe -x [options] [<patterns>]
  <patterns>  If specified, extract only entries that match
  -k    Keep (don't overwrite) existing files
  -m    Don't restore modification times
  -O    Write entries to stdout, don't restore to disk
  -p    Restore permissions (including ACLs, owner, file flags)
bsdtar 3.6.2 - libarchive 3.6.2 zlib/1.2.5.f-ipp liblzma/5.2.5 bz2lib/1.0.8 libzstd/1.5.4

そのため、tar -c を実行しても GNU tar でおなじみの 空のアーカイブ作成はご容赦願います という独特な表現を見ることはできません:

Ubuntu 上の GNU tar v1.34
tar -c
tar: 空のアーカイブ作成はご容赦願います
より詳しい情報は 'tar --help' または 'tar --usage' で.
Ubuntu 上の FreeBSD 版 tar v3.6.0
bsdtar -c
bsdtar: no files or directories specified
Windows 11 に付属の FreeBSD 版 tar v3.6.2
tar.exe -c
tar.exe: no files or directories specified

FreeBSD 版 tar は、tar や gz はもちろん、7z 等の多様なアーカイブ形式に対応しています:

## Supported Formats

Currently, the library automatically detects and reads the following formats:

  * Old V7 tar archives
  * POSIX ustar
  * GNU tar format (including GNU long filenames, long link names, and sparse files)
  * Solaris 9 extended tar format (including ACLs)
  * POSIX pax interchange format
  * POSIX octet-oriented cpio
  * SVR4 ASCII cpio
  * Binary cpio (big-endian or little-endian)
  * PWB binary cpio
  * ISO9660 CD-ROM images (with optional Rockridge or Joliet extensions)
  * ZIP archives (with uncompressed or "deflate" compressed entries, including support for encrypted Zip archives)
  * ZIPX archives (with support for bzip2, ppmd8, lzma and xz compressed entries)
  * GNU and BSD 'ar' archives
  * 'mtree' format
  * 7-Zip archives
  * Microsoft CAB format
  * LHA and LZH archives
  * RAR and RAR 5.0 archives (with some limitations due to RAR's proprietary status)
  * XAR archives

The library also detects and handles any of the following before evaluating the archive:

  * uuencoded files
  * files with RPM wrapper
  * gzip compression
  * bzip2 compression
  * compress/LZW compression
  * lzma, lzip, and xz compression
  * lz4 compression
  * lzop compression
  * zstandard compression

The library can create archives in any of the following formats:

  * POSIX ustar
  * POSIX pax interchange format
  * "restricted" pax format, which will create ustar archives except for entries that require pax extensions (for long filenames, ACLs, etc).
  * Old GNU tar format
  * Old V7 tar format
  * POSIX octet-oriented cpio
  * SVR4 "newc" cpio
  * Binary cpio (little-endian)
  * PWB binary cpio
  * shar archives
  * ZIP archives (with uncompressed or "deflate" compressed entries)
  * GNU and BSD 'ar' archives
  * 'mtree' format
  * ISO9660 format
  * 7-Zip archives
  * XAR archives

When creating archives, the result can be filtered with any of the following:

  * uuencode
  * gzip compression
  * bzip2 compression
  * compress/LZW compression
  * lzma, lzip, and xz compression
  * lz4 compression
  * lzop compression
  * zstandard compression

2

というわけで、tar.exe を叩くだけでコマンドラインから 7z を圧縮・解凍できてしまうのです:

libarchive.gif

エクスプローラの 7z 対応も libarchive を利用しているため、順序としてはコマンドラインで先に対応していたことになります。私も Windows 10 の頃から tar.exe が同梱されていたことは知っていたのですが、それが FreeBSD 版 tar で、ここまで広範囲をカバーできるものとは認識していませんでした。3

とはいえ、開発者にとって嬉しいのは Linux との相互運用性という意味で tar および gz、bzip2 への対応でしょう。4 本件に限らず、最近の Windows の開発者への寄り添い具合には目を見張るものがあります。この調子で開発者フレンドリーな姿勢を継続してほしいですね。

参考リンク

  1. Announcing Windows 11 Insider Preview Build 25992 (Canary Channel) | Windows Insider Blog

  2. https://github.com/libarchive/libarchive/blob/v3.6.2/README.md

  3. 実際、Windows 10 の tar.exe では 7z を解凍しようとしても LZMA codec is unsupported となってしまうようです。tar.exe --help の結果からも、liblzma が含まれていないことが確認できます。

  4. ちなみに .tgz.tbz2 はエクスプローラーで開けましたが、 .tbz は非対応でした。

31
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
26