3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

最も小さな圧縮ファイルを作れる圧縮形式はなにか?(ネタ)

Last updated at Posted at 2025-06-11

はじめに

世の中には様々な圧縮形式があります。小さな圧縮ファイルを作るために一番重要なことは何でしょうか? 圧縮率の高い優れたアルゴリズムを使う? いいえ、違います。小さな圧縮ファイルを作るのにもっとも重要なことはデータサイズを 0 バイトにすることです。

調査

ということで調べてみました。

$ gzip -cf </dev/null | od -Ad -tx1z
0000000 1f 8b 08 00 00 00 00 00 00 03 03 00 00 00 00 00  >................<
0000016 00 00 00 00                                      >....<
0000020
$ lz4 -cf </dev/null | od -Ad -tx1z
0000000 04 22 4d 18 64 40 a7 00 00 00 00 05 5d cc 02     >."M.d@......]..<
0000015
$ bzip2 -cf </dev/null | od -Ad -tx1z
0000000 42 5a 68 39 17 72 45 38 50 90 00 00 00 00        >BZh9.rE8P.....<
0000014
$ lzip -cf </dev/null | od -Ad -tx1z
0000000 4c 5a 49 50 01 0c 00 83 ff fb ff ff c0 00 00 00  >LZIP............<
0000016 00 00 00 00 00 00 00 00 00 00 00 00 24 00 00 00  >............$...<
0000032 00 00 00 00                                      >....<
0000036
$ xz -cf </dev/null | od -Ad -tx1z
0000000 fd 37 7a 58 5a 00 00 04 e6 d6 b4 46 00 00 00 00  >.7zXZ......F....<
0000016 1c df 44 21 1f b6 f3 7d 01 00 00 00 00 04 59 5a  >..D!...}......YZ<
0000032
$ compress -cf </dev/null | od -Ad -tx1z
0000000 1f 9d 90                                         >...<
0000003

結論

ということで、最も小さな圧縮ファイルを作れる圧縮形式は伝統の compress 形式(.Z形式)で、わずか 3 バイトでした。他にも圧縮形式はありますが、これが最小な気がします。

ちなみにここで出力されたバイナリをファイルに保存すれば、ちゃんと伸張できるはずです。次の例は空の gzip ファイルを作る例です。

圧縮ファイルの作成と伸張
$ gzip -cf </dev/null > null.gz

$ ls -l
total 4
-rw-rw-r-- 1 koichi koichi 20 Jun 11 23:52 null.gz

$ gunzip null.gz

$ ls -l
total 0
-rw-rw-r-- 1 koichi koichi 0 Jun 11 23:52 null

圧縮ファイルがどのようなバイナリであるかもわかっているので、圧縮コマンドがない環境でも空の圧縮ファイルが作れます。

圧縮ファイルの作成と伸張
$ printf '\37\213\10\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0\0' > null.gz

$ ls -l
total 4
-rw-rw-r-- 1 koichi koichi 20 Jun 11 23:57 null.gz

$ gunzip null.gz

$ ls -l
total 0
-rw-rw-r-- 1 koichi koichi 0 Jun 11 23:57 null

なお、このテクニックがなんの役に立つのかは知りません。


ネタ元: Shell、ShellScriptで空のZIPファイルを作成する方法

22バイトの空のzipを作る方法
$ printf 'PK\5\6\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' > empty.zip
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?