/
unzip.ex
436 lines (355 loc) · 13.9 KB
/
unzip.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
defmodule Unzip do
@moduledoc """
Module to get files out of a zip. Works with local and remote files
## Overview
Unzip tries to solve problem of accessing files from a zip which is
not local (Aws S3, sftp etc). It does this by simply separating file
system and zip implementation. Anything which implements
`Unzip.FileAccess` can be used to get zip contents. Unzip relies on
the ability to seek and read of the file, This is due to the nature
of zip file. Files from the zip are read on demand.
## Usage
# Unzip.LocalFile implements Unzip.FileAccess
zip_file = Unzip.LocalFile.open("foo/bar.zip")
# `new` reads list of files by reading central directory found at the end of the zip
{:ok, unzip} = Unzip.new(zip_file)
# Alternatively if you have the zip file in memory as binary you can
# directly pass it to `Unzip.new(binary)` to unzip
#
# {:ok, unzip} = Unzip.new(<<binary>>)
# returns list of files along with metadata
file_entries = Unzip.list_entries(unzip)
# returns decompressed file stream
stream = Unzip.file_stream!(unzip, "baz.png")
Supports STORED and DEFLATE compression methods. Does not support zip64 specification yet
"""
require Logger
alias Unzip.FileAccess
alias Unzip.FileBuffer
alias Unzip.RangeTree
@chunk_size 65_000
@typedoc """
Struct holding zip related metadata, returned by the `new/1`.
Public fields:
* `zip` - Zip struct passed to `new/1`
Remaining fields are private and must not be accessed directly.
"""
@type t :: %__MODULE__{
zip: struct(),
cd_list: map()
}
defstruct [:zip, :cd_list]
defmodule Error do
defexception [:message]
end
defmodule Entry do
@moduledoc """
File metadata returned by `Unzip.list_entries/1`
* `:file_name` - (string) File name with complete path. Directory files will have `/` at the end of their name
* `:last_modified_datetime` - (NaiveDateTime) last modified date and time of the file. It will be set to `nil` if datetime is invalid
* `:compressed_size` - (positive integer) Compressed file size in bytes
* `:uncompressed_size` - (positive integer) Uncompressed file size in bytes
"""
@type t :: %__MODULE__{
file_name: String.t(),
last_modified_datetime: NaiveDateTime.t() | nil,
compressed_size: pos_integer(),
uncompressed_size: pos_integer()
}
defstruct [
:file_name,
:last_modified_datetime,
:compressed_size,
:uncompressed_size
]
end
@doc """
Reads zip metadata from the passed zip file.
`zip` must implement `Unzip.FileAccess` protocol.
Fetches the list of files present in the zip and other metadata by
reading central directory found at the end of the zip.
Returns `Unzip` struct which contains passed `zip` struct and zip
metadata.
"""
@spec new(Unzip.FileAccess.t()) :: {:ok, t} | {:error, term()}
def new(zip) do
with {:ok, eocd} <- find_eocd(zip),
{:ok, entries} <- read_cd_entries(zip, eocd) do
{:ok, %Unzip{zip: zip, cd_list: entries}}
end
end
@doc """
Returns list of files metadata. This does not make `pread` call as metadata is already by `new/1`.
See `Unzip.Entry` for metadata fields
"""
@spec list_entries(t) :: list(Entry.t())
def list_entries(unzip) do
Enum.map(unzip.cd_list, fn {_, entry} ->
%Entry{
file_name: entry.file_name,
last_modified_datetime: entry.last_modified_datetime,
compressed_size: entry.compressed_size,
uncompressed_size: entry.uncompressed_size
}
end)
end
@type stream_options :: {:chunk_size, pos_integer()}
@doc """
Returns decompressed file as a stream of stream of [iodata](https://hexdocs.pm/elixir/IO.html#module-io-data). `file_path` *must* be the complete file path within the zip. The file entry is read in chunks, then decompressed in a streaming fashion.
### Options
* `chunk_size` - Chunks are read from the source of the size specified by `chunk_size`. This is *not* the size of the chunk returned by `file_stream!` since the chunk size varies after decompressing the. Useful when reading from the source is expensive and you want optimize by increasing the chunk size. Defaults to `65_000`
"""
@spec file_stream!(t, String.t()) :: Enumerable.t()
@spec file_stream!(t, String.t(), [stream_options]) :: Enumerable.t()
def file_stream!(%Unzip{zip: zip, cd_list: cd_list}, file_path, opts \\ []) do
unless Map.has_key?(cd_list, file_path) do
raise Error, message: "File #{inspect(file_path)} not present in the zip"
end
entry = Map.fetch!(cd_list, file_path)
local_header = pread!(zip, entry.local_header_offset, 30)
case local_header do
<<0x04034B50::little-32, _::little-32, compression_method::little-16, _::little-128,
file_path_length::little-16, extra_field_length::little-16>> ->
offset = entry.local_header_offset + 30 + file_path_length + extra_field_length
stream!(zip, offset, entry.compressed_size, opts)
|> decompress(compression_method)
|> crc_check(entry.crc)
_ ->
raise Error, message: "Invalid local zip header"
end
end
defp stream!(file, offset, size, opts) do
end_offset = offset + size
Stream.unfold(offset, fn
offset when offset >= end_offset ->
nil
offset ->
chunk_size = Keyword.get(opts, :chunk_size, @chunk_size)
next_offset = min(offset + chunk_size, end_offset)
data = pread!(file, offset, next_offset - offset)
{data, next_offset}
end)
end
defp decompress(stream, 0x8) do
stream
|> Stream.transform(
fn ->
z = :zlib.open()
:ok = :zlib.inflateInit(z, -15)
z
end,
fn data, z -> {[:zlib.inflate(z, data)], z} end,
fn z ->
:zlib.inflateEnd(z)
:zlib.close(z)
end
)
end
defp decompress(stream, 0x0), do: stream
defp decompress(_stream, compression_method) do
raise Error, message: "Compression method #{compression_method} is not supported"
end
defp crc_check(stream, expected_crc) do
stream
|> Stream.transform(
fn -> :erlang.crc32(<<>>) end,
fn data, crc -> {[data], :erlang.crc32(crc, data)} end,
fn crc ->
unless crc == expected_crc do
raise Error, message: "CRC mismatch. expected: #{expected_crc} got: #{crc}"
end
end
)
end
defp read_cd_entries(zip, eocd) do
with {:ok, file_buffer} <-
FileBuffer.new(
zip,
@chunk_size,
eocd.cd_offset + eocd.cd_size,
eocd.cd_offset,
:forward
) do
parse_cd(file_buffer, %{entries: %{}, range_tree: RangeTree.new()})
end
end
defp parse_cd(%FileBuffer{buffer_position: pos, limit: limit}, %{entries: entries})
when pos >= limit,
do: {:ok, entries}
defp parse_cd(buffer, acc) do
with {:ok, chunk, buffer} <- FileBuffer.next_chunk(buffer, 46),
{:match_chunk,
<<0x02014B50::little-32, _::little-32, flag::little-16, compression_method::little-16,
mtime::little-16, mdate::little-16, crc::little-32, compressed_size::little-32,
uncompressed_size::little-32, file_name_length::little-16,
extra_field_length::little-16, comment_length::little-16, _::little-64,
local_header_offset::little-32>>} <- {:match_chunk, chunk},
{:ok, buffer} <- FileBuffer.move_forward_by(buffer, 46),
{:ok, file_name, buffer} <- FileBuffer.next_chunk(buffer, file_name_length),
{:ok, buffer} <- FileBuffer.move_forward_by(buffer, file_name_length),
{:ok, extra_fields, buffer} <- FileBuffer.next_chunk(buffer, extra_field_length),
{:ok, buffer} <- FileBuffer.move_forward_by(buffer, extra_field_length),
{:ok, _file_comment, buffer} <- FileBuffer.next_chunk(buffer, comment_length),
{:ok, buffer} <- FileBuffer.move_forward_by(buffer, comment_length) do
entry = %{
bit_flag: flag,
compression_method: compression_method,
last_modified_datetime: to_datetime(<<mdate::16>>, <<mtime::16>>),
crc: crc,
compressed_size: compressed_size,
uncompressed_size: uncompressed_size,
local_header_offset: local_header_offset,
# TODO: we should treat binary as "IBM Code Page 437" encoded string if GP flag 11 is not set
file_name: file_name
}
entry =
if need_zip64_extra?(entry) do
merge_zip64_extra(entry, extra_fields)
else
entry
end
case add_entry(acc, file_name, entry) do
{:error, _} = error -> error
acc -> parse_cd(buffer, acc)
end
else
{:error, :invalid_count} ->
{:error, "Invalid zip file, invalid central directory"}
{:match_chunk, _binary} ->
{:error, "Invalid zip file, invalid central directory file header"}
{:error, _error} = error ->
error
end
end
defp add_entry(%{entries: entries, range_tree: range_tree}, file_name, entry) do
if RangeTree.overlap?(range_tree, entry.local_header_offset, entry.compressed_size) do
{:error, "Invalid zip file, found overlapping zip entries"}
else
%{
entries: Map.put(entries, file_name, entry),
range_tree: RangeTree.insert(range_tree, entry.local_header_offset, entry.compressed_size)
}
end
end
defp need_zip64_extra?(%{
compressed_size: cs,
uncompressed_size: ucs,
local_header_offset: offset
}) do
Enum.any?([cs, ucs, offset], &(&1 == 0xFFFFFFFF))
end
@zip64_extra_field_id 0x0001
defp merge_zip64_extra(entry, extra) do
zip64_extra =
find_extra_fields(extra)
|> Map.fetch!(@zip64_extra_field_id)
{entry, zip64_extra} =
if entry[:uncompressed_size] == 0xFFFFFFFF do
<<uncompressed_size::little-64, zip64_extra::binary>> = zip64_extra
{%{entry | uncompressed_size: uncompressed_size}, zip64_extra}
else
{entry, zip64_extra}
end
{entry, zip64_extra} =
if entry[:compressed_size] == 0xFFFFFFFF do
<<compressed_size::little-64, zip64_extra::binary>> = zip64_extra
{%{entry | compressed_size: compressed_size}, zip64_extra}
else
{entry, zip64_extra}
end
{entry, _zip64_extra} =
if entry[:local_header_offset] == 0xFFFFFFFF do
<<local_header_offset::little-64, zip64_extra::binary>> = zip64_extra
{%{entry | local_header_offset: local_header_offset}, zip64_extra}
else
{entry, zip64_extra}
end
entry
end
defp find_extra_fields(extra, result \\ %{})
defp find_extra_fields(<<>>, result), do: result
defp find_extra_fields(
<<id::little-16, size::little-16, data::binary-size(size), rest::binary>>,
result
) do
find_extra_fields(rest, Map.put(result, id, data))
end
defp find_eocd(zip) do
with {:ok, file_buffer} <- FileBuffer.new(zip, @chunk_size),
{:ok, eocd, file_buffer} <- find_eocd(file_buffer, 0) do
case find_zip64_eocd(file_buffer) do
{:ok, zip64_eocd} ->
{:ok, zip64_eocd}
_ ->
{:ok, eocd}
end
end
end
@zip64_eocd_locator_size 20
@zip64_eocd_size 56
defp find_zip64_eocd(file_buffer) do
with {:ok, chunk, file_buffer} <-
FileBuffer.next_chunk(file_buffer, @zip64_eocd_locator_size),
true <- zip64?(chunk) do
<<0x07064B50::little-32, _::little-32, eocd_offset::little-64, _::little-32>> = chunk
{:ok,
<<0x06064B50::little-32, _::64, _::16, _::16, _::32, _::32, _::64,
total_entries::little-64, cd_size::little-64,
cd_offset::little-64>>} = pread(file_buffer.file, eocd_offset, @zip64_eocd_size)
{:ok, %{total_entries: total_entries, cd_size: cd_size, cd_offset: cd_offset}}
else
_ ->
false
end
end
defp zip64?(<<0x07064B50::little-32, _::little-128>>), do: true
defp zip64?(_), do: false
# Spec has variable length comment at the end of zip after EOCD, so
# EOCD can anywhere in the zip file. To avoid exhaustive search, we
# limit search space to last 5Mb. If we don't find EOCD within that
# we assume it's an invalid zip
@eocd_seach_limit 5 * 1024 * 1024
defp find_eocd(_file_buffer, consumed) when consumed > @eocd_seach_limit,
do: {:error, "Invalid zip file, missing EOCD record"}
@eocd_header_size 22
defp find_eocd(file_buffer, consumed) do
with {:ok, chunk, file_buffer} <- FileBuffer.next_chunk(file_buffer, @eocd_header_size) do
case chunk do
<<0x06054B50::little-32, _ignore::little-48, total_entries::little-16, cd_size::little-32,
cd_offset::little-32, ^consumed::little-16>> ->
{:ok, buffer} = FileBuffer.move_backward_by(file_buffer, @eocd_header_size)
{:ok, %{total_entries: total_entries, cd_size: cd_size, cd_offset: cd_offset}, buffer}
chunk when byte_size(chunk) < @eocd_header_size ->
{:error, "Invalid zip file, missing EOCD record"}
_ ->
{:ok, buffer} = FileBuffer.move_backward_by(file_buffer, 1)
find_eocd(buffer, consumed + 1)
end
end
end
defp to_datetime(<<year::7, month::4, day::5>>, <<hour::5, minute::6, second::5>>) do
case NaiveDateTime.new(1980 + year, month, day, hour, minute, second * 2) do
{:ok, datetime} ->
datetime
_ ->
nil
end
end
defp pread!(file, offset, length) do
case pread(file, offset, length) do
{:ok, term} -> term
{:error, reason} when is_binary(reason) -> raise Error, message: reason
{:error, reason} -> raise Error, message: inspect(reason)
end
end
defp pread(file, offset, length) do
case FileAccess.pread(file, offset, length) do
{:ok, term} when is_binary(term) ->
{:ok, term}
{:error, reason} ->
{:error, reason}
_ ->
{:error, "Invalid data returned by pread/3. Expected binary"}
end
end
end