Skip to content

Instantly share code, notes, and snippets.

@ysakasin
Last active December 16, 2023 11:25
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ysakasin/2edf8d3bf55c6ebf63f82851e302b030 to your computer and use it in GitHub Desktop.
Save ysakasin/2edf8d3bf55c6ebf63f82851e302b030 to your computer and use it in GitHub Desktop.
ZIPの仕様を日本語でまとめる

.zip

Version: 6.3.4 https://support.pkware.com/display/PKZIP/APPNOTE

Index

local file header

フォーマット

Parameter Bytes Description
local file header signature 4 bytes シグネチャ 0x04034b50
version needed to extract 2 bytes link
general purpose bit flag 2 bytes link
compression method 2 bytes link
last mod file time 2 bytes link
last mod file date 2 bytes link
crc-32 4 bytes link
compressed size 4 bytes link
uncompressed size 4 bytes link
file name length 2 bytes
extra field length 2 bytes
file name (variable size)
extra field (variable size)

local file header signature

シグネチャ 0x04034b50 "PK\03\04"

バージョン番号

バージョン番号を値にする場合には value / 10 がメジャーバージョン、 value % 10 がマイナーバージョンになるようにする。

バージョン 2.1 を値にすると 21 となる。

version needed to extract

解凍に必要な最低限のバージョン。 2.1 が必要だったら値は 21 にする。

Version Feature
1.0 Default value 無圧縮のそのままのデータ
1.1 File is a volume label
2.0 File is a folder (directory)
2.0 File is compressed using Deflate compression
2.0 File is encrypted using traditional PKWARE encryption
2.1 File is compressed using Deflate64(tm)
2.5 File is compressed using PKWARE DCL Implode
2.7 File is a patch data set
4.5 File uses ZIP64 format extensions
4.6 File is compressed using BZIP2 compression*
5.0 File is encrypted using DES
5.0 File is encrypted using 3DES
5.0 File is encrypted using original RC2 encryption
5.0 File is encrypted using RC4 encryption
5.1 File is encrypted using AES encryption
5.1 File is encrypted using corrected RC2 encryption**
5.2 File is encrypted using corrected RC2-64 encryption**
6.1 File is encrypted using non-OAEP key wrapping***
6.2 Central directory encryption
6.3 File is compressed using LZMA
6.3 File is compressed using PPMd+
6.3 File is encrypted using Blowfish
6.3 File is encrypted using Twofish

general purpose bit flag

Bit 0: 暗号化されている時に立てる。

Bit 1: 圧縮メソッドのためのパラメータ

Bit 2: 圧縮メソッドのためのパラメータ

Bit 3: これが立っているとき、ローカルファイルヘッダのcrc32、compressed size、 uncompressed sizeは0になっていて、本当の値はデータディスクリプタに書かれている。

Bit 4: Deflateのためのパラメータ

Bit 5: これが立っているとき、ファイルが圧縮されたパッチデータ (compressed patched data) であることを示す。PKZIP 2.70以上で求められる。

Bit 6: これが立っているとき、Strong encryptionされていることを示す。これを使うなら、バージョンは5.0以上である必要があり、Bit 0も立てなければならない。

Bit 7: 現在は使われていない。

Bit 8: 現在は使われていない。

Bit 9: 現在は使われていない。

Bit 10: 現在は使われていない。

Bit 11: ファイルエンコーディングのためのフラグ。これが立っているとき、コメントやファイル名の文字コードはUTF-8とする。

Bit 12: PKWAREによって予約されている。

Bit 13: セントラルディレクトリが暗号化されているときに立てる。詳しくはStrong encryptionの仕様を見ること。

Bit 14: PKWAREによって予約されている。

Bit 15: PKWAREによって予約されている。

Method 6 - Imploding

BIT 1: これが立っているとき、8K sliding dictionaryを使っていることを示す。下がっているときには、4K sliding dictionaryを 使っていることを示す。

BIT 2: これが立っているとき、sliding dictionaryのエンコードに3 Shannon-Fano treesが使われていることを示す。下がっているときは2 Shannon-Fano treesが使われていることを示す。

Methods 8 and 9 - Deflating

Bit 2 Bit 1 Description
0 0 通常の圧縮オプション (-en) が使われた
0 1 Maximum圧縮オプション (-exx/-ex) が使われた
1 0 Fast圧縮オプション (-ef) が使われた
1 1 Super Fast圧縮オプション (-es) が使われた

Method 14 - LZMA

BIT 1: これが立っているとき、圧縮されたデータストリームの終端を示すために end-of-stream (EOS) が使われている。下がっているとき、EOSは存在せず、圧縮後のデータサイズを知っている必要がある。

BIT 2: 使われない。

Note

これら3つの圧縮方式以外において、Bit 1とBit 2は未定義である。

compression method

Value Method
0 The file is stored 圧縮なし
1 The file is Shrunk
2 The file is Reduced with compression factor 1
3 The file is Reduced with compression factor 2
4 The file is Reduced with compression factor 3
5 The file is Reduced with compression factor 4
6 The file is Imploded
7 Reserved for Tokenizing compression algorithm
8 The file is Deflated
9 Enhanced Deflating using Deflate64(tm)
10 PKWARE Data Compression Library Imploding (old IBM TERSE)
11 Reserved by PKWARE
12 File is compressed using BZIP2 algorithm
13 Reserved by PKWARE
14 LZMA (EFS)
15 Reserved by PKWARE
16 Reserved by PKWARE
17 Reserved by PKWARE
18 File is compressed using IBM TERSE (new)
19 IBM LZ77 z Architecture (PFS)
97 WavPack compressed data
98 PPMd version I, Rev 1

last mod file time

次の項を見ること。

last mod file date

日時の表現には MS-DOS format を用いる。標準入力からのデータに日時を付与する場合には、圧縮が開始された時刻をそのまま用いる。セントラルディレクトリを暗号化し、general purpose bit flag の Bit 13が立っている時には、ローカルヘッダのこの値は0にする。

standard MS-DOS date format

Bits Parameter
31 - 25 year
24 - 21 month
20 - 16 day
15 - 11 hour
10 - 5 min
5 - 0 seconds / 2

A brief history of time stamps (p.4 "MSDOS")

CRC-32

ファイルデータに用いる誤り検出符号である。(おそらく)元のファイルデータに用いる。 general purpose bit flag の Bit 3 もしくはBit 13が立っている時にはローカルファイルヘッダのこの値は0にしておく。

CRC-32の実装例は RFC 1952 (gzip) に記載されている。以下はRFC 1952からの引用。

/* Table of CRCs of all 8-bit messages. */
unsigned long crc_table[256];

/* Flag: has the table been computed? Initially false. */
int crc_table_computed = 0;

/* Make the table for a fast CRC. */
void make_crc_table(void)
{
  unsigned long c;
  int n, k;
  for (n = 0; n < 256; n++) {
    c = (unsigned long) n;
    for (k = 0; k < 8; k++) {
      if (c & 1) {
        c = 0xedb88320L ^ (c >> 1);
      } else {
        c = c >> 1;
      }
    }
    crc_table[n] = c;
  }
  crc_table_computed = 1;
}

unsigned long update_crc(unsigned long crc,
                         unsigned char *buf, int len)
{
  unsigned long c = crc ^ 0xffffffffL;
  int n;

  if (!crc_table_computed)
    make_crc_table();
  for (n = 0; n < len; n++) {
    c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
  }
  return c ^ 0xffffffffL;
}

/* Return the CRC of the bytes buf[0..len-1]. */
unsigned long crc(unsigned char *buf, int len)
{
  return update_crc(0L, buf, len);
}

compressed size

圧縮や暗号化後の格納されている状態でのデータサイズ。general purpose bit flag の Bit 3 が立っている時には、ローカルファイルヘッダのこの値は0にする。

ZIP64 format の利用時には値を 0xFFFFFFFF にする。

uncompressed size

元のデータサイズ。general purpose bit flag の Bit 3 もしくは Bit 13 (Strong encryptionの時) には、ローカルファイルヘッダのこの値は0にする。

ZIP64 format の利用時には値を 0xFFFFFFFF にする。

central directory header

フォーマット

Parameter Bytes Description
central file header signature 4 bytes シグネチャ 0x02014b50 "PK\01\02"
version made by 2 bytes link
version needed to extract 2 bytes link
general purpose bit flag 2 bytes link
compression method 2 bytes link
last mod file time 2 bytes link
last mod file date 2 bytes link
crc-32 4 bytes link
compressed size 4 bytes link
uncompressed size 4 bytes link
file name length 2 bytes
extra field length 2 bytes
file comment length 2 bytes
disk number start 2 bytes link
internal file attributes 2 bytes link
external file attributes 4 bytes link
relative offset of local header 4 bytes link
file name (variable size)
extra field (variable size)
file comment (variable size)

version made by

上位ビット

ファイルの属性情報の互換性を表す。

Value System
0 MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)
1 Amiga
2 OpenVMS
3 UNIX
4 VM/CMS
5 Atari ST
6 OS/2 H.P.F.S.
7 Macintosh
8 Z-System
9 CP/M
10 Windows NTFS
11 MVS (OS/390 - Z/OS)
12 VSE
13 Acorn Risc
14 VFAT
15 alternate MVS
16 BeOS
17 Tandem
18 OS/400
19 OS X (Darwin)
20 から 255 使われない

下位バイト

ファイルをエンコードしたソフトウェアが用いたZIP仕様のバージョン。

disk number start

このファイルが始まるディスクの番号。ZIP64 formatを使っている時には 0xFFFF にする。

internal file attributes

Bits 1 and 2 は PKWARE によって予約されている。

最下位bit (Bits 0) がセットされている時はASCIIもしくはテキストデータであることを示す。セットされていない時にはバイナリデータであることを示す。version 1.0 ではこのビットは用いない。

0x0002 bitに意味があるがよくわからない。

The 0x0002 bit of this field indicates, if set, that a 4 byte variable record length control field precedes each logical record indicating the length of the record. The record length control field is stored in little-endian byte order. This flag is independent of text control characters, and if used in conjunction with text data, includes any control characters in the total length of the record. This value is provided for mainframe data transfer support.

external file attributes

この値は version made by で指定したホストシステムに依存する。MS-DOSの場合、下位バイトはMS-DOSのディレクトリ属性バイトになる。標準入力からのデータの場合、この値は0になる。

MS-DOS file attributes

Bit Description
0 read only
1 hidden 隠しファイルである(システム依存)
2 system システムにとって重要なファイルである(システム依存)
3 volume label ルートディレクトリに1つだけ存在できる。このボリュームの名前。
4 directory ディレクトリであることを示す。
5 archive "dirty"かどうかを示す値なので、ZIPの実装上は不要と思われる。

FATファイル システムのしくみと操作法 / ディレクトリ構造体

Design of the FAT file system - Wikipedia

relative offset of local header

ZIPファイルの開始地点を基準とした時の、対応するローカルヘッダの位置。自己解凍ZIPを考慮した結果、このような言い回しになっていると思われる。単体のZIPで考えれば、ローカルヘッダのアドレス。

ZIP64 format の場合には 0xFFFFFFFF にする。

end of central directory record

Parameter Bytes Description
end of central dir signature 4 bytes シグネチャ 0x06054b50 "PK\05\06"
number of this disk 2 bytes link
number of the disk with the start of the central directory 2 bytes
total number of entries in the central directory on this disk 2 bytes
total number of entries in the central directory 2 bytes
size of the central directory 4 bytes link
offset of start of central directory ... 4 bytes
.ZIP file comment length 2 bytes
.ZIP file comment (variable size)

number of this disk

この終端レコードが含まれているディスクの番号。ZIP64では 0xFFFF にしておく。

number of the disk with the start of the central directory

セントラルディレクトリが始まるディスクの番号。ZIP64では 0xFFFF にしておく。

total number of entries in the central directory on this disk

このディクスに含まれるセントラルディレクトリのエントリ数。ZIP64では 0xFFFF にしておく。

total number of entries in the central directory

セントラルディレクトリの総エントリ数。ZIP64では 0xFFFF にしておく。

size of the central directory

セントラルディレクトリのサイズ。ZIP64では 0xFFFFFFFF にしておく。

offset of start of central directory with respect to the starting disk number

セントラルディレクトリが始まるディスクにおいて、セントラルディレクトリが始まる位置。ZIP64では 0xFFFFFFFF にしておく。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment