The files in a zip archive, read-only. The archive can be on any disk.
archives = Fil.disk(adapter: Fil.Adapter.S3, bucket: "exports")
export = Fil.disk(adapter: Fil.Adapter.Zip, archive: Fil.ref(archives, "2026/q3.zip"))
{:ok, report} = Fil.read(export, "reports/q3.pdf")
{:ok, reports} = Fil.ls(export, "reports")A local archive is a ref on a Fil.Adapter.Local disk. Reads, streams, listings and URLs work as on every other disk,
and every call that would change the archive returns a Fil.AccessDeniedError with reason: :read_only. Copy a file
to another disk to change it there.
The adapter reads only the parts of the archive it needs, with ranged reads (offset: and length:), so a file in a
large archive on S3 costs a few requests and not a download of the archive (see Reads). It reads
entries that are stored or compressed with deflate, the two methods zip tools use, and archives with Zip64 sizes.
Options
:archive(struct of typeFil.Ref) - Required. The zip archive, as a ref on any disk:Fil.ref(archives, "docs.zip"). It's read throughFil, with the plugins of its disk, and it doesn't have to exist when the disk is built.
Operations
For each Fil function, this section says which calls it makes on the archive and where it departs from the
contract that every adapter follows. Anything it doesn't mention follows the
contract. Reads, streams, stats and listings first read the archive's directory (see Reads).
Directories exist where the archive has an entry for them or files under them, like on an object store.
Fil.read/3
- Reads the entry with one more ranged read after the directory, two when its local header has a longer name or extra field than the directory says.
- With
offset:andlength:, reads the local header, then only that part of a stored entry. A compressed entry is streamed and inflated from its start up to the end of the part, because deflate can't start in the middle. A range that covers the whole entry is read like the whole entry. - Checks a whole entry against the CRC-32 the archive stores, with or without
verify_checksum: true. An entry that doesn't match returns aFil.ChecksumMismatchError. - Never inflates more than the size the archive declares for the entry (see Damage).
- Reading a directory returns a
Fil.NotFoundError.
Fil.stream/3
- Reads the entry's local header with a ranged read, then streams the content with
Fil.stream/3on the archive each time the stream is read, and inflates it chunk by chunk. - With
offset:andlength:, streams only that part of a stored entry. A compressed entry is inflated from its start up to the end of the part. - Checks a whole entry against its CRC-32 after the last chunk, and raises a
Fil.ChecksumMismatchErrorthere if it doesn't match. An entry that inflates to more than its declared size raises on the chunk that passes it, so the stream never yields more than that size.
Fil.write/4
- Calls no storage and returns a
Fil.AccessDeniedErrorwithreason: :read_only.
Fil.rm/3
- Calls no storage and returns a
Fil.AccessDeniedErrorwithreason: :read_only, also for a missing file.
Fil.rm_rf/3
- Calls no storage and returns a
Fil.AccessDeniedErrorwithreason: :read_only.
Fil.stat/3
- Reads only the directory.
:mtimeis the entry's extended timestamp, or else its MS-DOS time, which zip tools write in local time, read in the local time of the node.:etagis the entry's CRC-32 and size ("crc-size"), the same in listings.:content_typeisnil, because zip archives don't store one (Fil.Plugguesses it from the extension).checksum: :crc32returns the CRC-32 from the directory. Other algorithms stream the whole entry through the checksum.- A stat on a directory returns
%Fil.Stat{type: :directory}with every other fieldnil. - A missing archive returns a
Fil.ConfigurationError, andFil.exists?/2returnsfalsefor every path on it, as for any error.
Fil.ls/3
- Reads only the directory.
- Lists a directory the archive has an entry for even when it's empty.
Fil.cp/4
- Calls no storage and returns a
Fil.AccessDeniedErrorwithreason: :read_only. - A copy to another disk reads the entry and writes it there, so it works.
Fil.rename/4
- Calls no storage and returns a
Fil.AccessDeniedErrorwithreason: :read_only. - A move to another disk returns the same error before anything is copied, and the entry stays in the archive.
Fil.url/3, Fil.signed_url/3 and Fil.signed_upload/3
- A zip archive has no URLs. Attach
Fil.Plugin.URLto build them, andFil.Plugserves the files. An upload fails whenFil.Plugwrites it.
Fil.Disk.same_storage?/2
- Compares the archive's disk the way
Fil.Disk.same_storage?/2does, and the archive's path.
Details
Reads
The disk keeps nothing between calls, so every call reads the archive's directory again: a stat of the archive, then
a ranged read of its last 64 KiB or so, which holds the end of the directory and in a small archive all of it. A
larger directory takes one more ranged read. On S3 or Azure, that's two or three requests for a stat or a listing,
and one or two more for a whole read of an entry or a part of a stored one. A stream, a part of a compressed entry
and a checksum other than :crc32 read the local header and then stream the content with Fil.stream/3 on the
archive, which on S3 is a HeadObject and a GetObject. An archive with many thousands of entries has a directory of
several megabytes, which every call reads and parses again.
A plugin that transforms reads on the archive's disk gets the whole archive for every ranged read (see Ranges), so on such a disk every read of an entry downloads the archive.
Entry names
Only entries whose names are already normalized paths are part of the disk. An entry whose name climbs above the
archive's root (../evil.txt), starts with /, has . segments, repeated slashes or control characters, is longer
than 1,024 bytes or has more than 64 levels (a/b/c.txt has 3) is left out of listings and can't be read. Two entries
with the same name are one file, the first in the archive. A name can be a file and a directory at once (a and
a/b.txt), like keys on an object store: a listing shows both, and a stat finds the file. Backslashes are part of a
name, because the zip format separates directories with / only. Names are read as UTF-8, and names that aren't valid
UTF-8 as Latin-1, with the UTF-8 flag or without (zip's own default, CP437, differs in its upper half).
Damage
The archive has to end with its end record. Bytes after it, or a prefix before the archive such as the program of a self-extracting archive, make the whole archive damaged. So does a central directory that doesn't fit the archive, or an entry that reaches into the central directory, as no zip tool writes one.
Damage to one entry fails only that entry: a local header that isn't one or whose fields move the content past the next entry's header, deflate data that can't be inflated, and content of another size than the archive declares. A deflated entry can expand a thousandfold, so the adapter stops as soon as an entry inflates past its declared size, and never holds or yields more than that.
An entry that declares its size truthfully can still be a thousand times larger than the archive, and Fil.read/3
holds all of it in memory. For archives from untrusted sources, check :size from Fil.stat/3, which reads only
the directory, before reading an entry, or use Fil.stream/3, which holds one chunk at a time.
Conformance
Nothing can be written, which breaks the contract, so this adapter doesn't run the conformance suite that the writable adapters pass.
Errors
An error of the archive's disk comes back as an error of the same kind, with the archive's error as its :reason.
Its :path and :disk are the zip disk's, like those of every other error, and the archive's are in the :reason.
| Situation | Fil error | :reason |
|---|---|---|
| a missing file or directory in the archive | Fil.NotFoundError | :enoent |
| a write, a removal, a copy within the disk, or a move | Fil.AccessDeniedError | :read_only |
| an entry whose content doesn't match its CRC-32 | Fil.ChecksumMismatchError | :checksum_mismatch |
| a damaged entry | Fil.ChecksumMismatchError | {:bad_entry, where} |
| an encrypted entry | Fil.UnsupportedError | :encrypted |
| an entry compressed with another method than deflate | Fil.UnsupportedError | {:compression, method} |
| no archive at the ref, or a directory | Fil.ConfigurationError | the archive's Fil.NotFoundError |
| a file that isn't a zip archive, or is damaged | Fil.ConfigurationError | {:bad_archive, where} |
| any other error reading the archive | the same kind of error | the archive's error |
where says what's damaged (see Damage). For an entry, it's :local_header, :deflate or :size.
For the archive, it's :no_end_record, :zip64_end_record, :central_directory, :zip64_extra, :truncated or
:no_size (its disk has no size for it). {:compression, method} has the number of the method in the zip format,
such as 12 for bzip2.
Summary
Types
@type t() :: %Fil.Adapter.Zip{archive: Fil.Ref.t()}