Skip to content

Commit c33729c

Browse files
armijnhemelfolkertdev
authored andcommitted
add test cases for ZIP files where the local file header and central
directory have different data in the "compression method" field.
1 parent c4a7bef commit c33729c

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Mismatched compression in local file header and central directory
2+
3+
When processing entries in a ZIP file it is assumed that the local file header
4+
and the central directory match, which does not need to be the case. One field
5+
that is in both is the `compression method`. If these do not match, then the
6+
different programs don't behave in the same way.
7+
8+
The two files `test_mismatched_compression_central.zip` and
9+
`test_mismatched_compression_local.zip` are modified versions of `test.zip` and
10+
both contain a file (compressed with "deflate") where the compression method in
11+
the header has been changed to "bzip". The first file has the wrong value in
12+
the central directory, the other in the the local file header.
13+
14+
Extracting these with different programs reveals some interesting behaviour:
15+
16+
```
17+
$ unzip test_mismatched_compression_central.zip
18+
Archive: test_mismatched_compression_central.zip
19+
This is a zipfile comment.
20+
inflating: test.txt
21+
extracting: gophercolor16x16.png
22+
```
23+
The files are correctly extracted, and the exit code of the program is `0`
24+
(success):
25+
26+
```
27+
$ echo $?
28+
0
29+
```
30+
31+
This means that `unzip` ignores the `compression method` field in the central
32+
directory and only looks at the local file header.
33+
34+
When unpacking the one with the value changed in the local file header `unzip`
35+
is not successful:
36+
37+
```
38+
$ unzip test_mismatched_compression_local.zip
39+
Archive: test_mismatched_compression_local.zip
40+
This is a zipfile comment.
41+
bunzipping: test.txt
42+
```
43+
44+
and the process ends there. Only a 0 byte file `test.txt` is extracted and the
45+
exit code is `6`, meaning there was an error:
46+
47+
```
48+
$ echo $?
49+
6
50+
```
51+
52+
`p7zip` has similar behaviour, although it reports an error in both cases.
53+
54+
```
55+
$ 7z x test_mismatched_compression_central.zip
56+
57+
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
58+
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Core(TM) i7-6770HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)
59+
60+
Scanning the drive for archives:
61+
1 file, 1170 bytes (2 KiB)
62+
63+
Extracting archive: test_mismatched_compression_central.zip
64+
65+
ERRORS:
66+
Headers Error
67+
68+
--
69+
Path = test_mismatched_compression_central.zip
70+
Type = zip
71+
ERRORS:
72+
Headers Error
73+
Physical Size = 1170
74+
Comment = This is a zipfile comment.
75+
76+
77+
78+
Archives with Errors: 1
79+
80+
Open Errors: 1
81+
```
82+
83+
When the error is in the central directory the file `test.txt` is successfully
84+
unpacked.
85+
86+
```
87+
$ 7z x test_mismatched_compression_local.zip
88+
89+
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
90+
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Core(TM) i7-6770HQ CPU @ 2.60GHz (506E3),ASM,AES-NI)
91+
92+
Scanning the drive for archives:
93+
1 file, 1170 bytes (2 KiB)
94+
95+
Extracting archive: test_mismatched_compression_local.zip
96+
97+
ERRORS:
98+
Headers Error
99+
100+
--
101+
Path = test_mismatched_compression_local.zip
102+
Type = zip
103+
ERRORS:
104+
Headers Error
105+
Physical Size = 1170
106+
Comment = This is a zipfile comment.
107+
108+
ERROR: Data Error : test.txt
109+
110+
Sub items Errors: 1
111+
112+
Archives with Errors: 1
113+
114+
Open Errors: 1
115+
116+
Sub items Errors: 1
117+
```
118+
119+
When the error is in the local file header the file `test.txt` is not unpacked
120+
and instead a 0 byte file is created.
1.14 KB
Binary file not shown.
1.14 KB
Binary file not shown.

0 commit comments

Comments
 (0)