Coquitlam, a city outside Vancouver, publishes its topographic survey as one 206 MB zip on S3. I wanted two files out of it: the spot elevations (1.9 million points) and the contour lines. Downloading 206 MB to read 14 MB is a waste of bandwidth and time, and S3 objects answer HTTP Range requests. So you can read the end of the zip, find the entries you want, and fetch only those bytes.

Actual run: 206,112,082 bytes on the server, 18 entries in the archive, about 13.8 MB fetched, 3.4 s wall clock, 1,912,050 elevation points, range -4.05 to 1,108.16 m.

Why it works

A zip is built for this. The tail of the file holds an end-of-central-directory record (EOCD), the EOCD points to the central directory, and the central directory lists every entry with its compression method, compressed size, and the offset of its local header. The local header in turn tells you how long its name and extra fields are, which is all you need to compute where the compressed bytes start.

So: fetch the tail, parse the EOCD, fetch the central directory, pick your entries, fetch each local header, then fetch and inflate each entry. Deflate streams inside a zip are raw deflate, no zlib wrapper, which in Python means zlib.decompress(data, -15).

The code

import struct, zlib, urllib.request URL = "https://coquitlam-spatial.s3-us-west-2.amazonaws.com/Topography/SHP/Coquitlam_Topo_SHP.zip" def size_of(url): req = urllib.request.Request(url, method="HEAD") with urllib.request.urlopen(req) as r: return int(r.headers["Content-Length"]) def get_range(url, start, end): req = urllib.request.Request(url, headers={"Range": f"bytes={start}-{end}"}) with urllib.request.urlopen(req) as r: return r.read() def list_entries(url, tail=4_000_000): size = size_of(url) buf = get_range(url, max(0, size - tail), size - 1) i = buf.rfind(b"PK\x05\x06") if i < 0: raise SystemExit("no EOCD in the last %d bytes" % tail) cd_size, cd_off = struct.unpack("<II", buf[i+12:i+20]) cd = get_range(url, cd_off, cd_off + cd_size - 1) entries, p = [], 0 while p + 46 <= len(cd) and cd[p:p+4] == b"PK\x01\x02": method, = struct.unpack("<H", cd[p+10:p+12]) csize, = struct.unpack("<I", cd[p+20:p+24]) nlen, elen, clen = struct.unpack("<HHH", cd[p+28:p+34]) lho, = struct.unpack("<I", cd[p+42:p+46]) name = cd[p+46:p+46+nlen].decode() entries.append((name, method, csize, lho)) p += 46 + nlen + elen + clen return entries def read_entry(url, method, csize, lho): loc = get_range(url, lho, lho + 29) nlen, elen = struct.unpack("<HH", loc[26:30]) start = lho + 30 + nlen + elen comp = get_range(url, start, start + csize - 1) return comp if method == 0 else zlib.decompress(comp, -15) for name, method, csize, lho in list_entries(URL): print(f"{name:45s} method={method} compressed={csize:>10,d}") 

Then read the two you want:

by_name = {e[0]: e for e in list_entries(URL)} shp = read_entry(URL, *by_name["DEM_Points.shp"][1:]) dbf = read_entry(URL, *by_name["DEM_Points.dbf"][1:]) 

Spot elevations live in the DBF as a fixed-width field, 53 bytes per record with a 161-byte header. Elevation is the first 32 bytes of each record after the 1-byte deletion flag:

import struct nrec = struct.unpack("<I", dbf[4:8])[0] hlen = struct.unpack("<H", dbf[8:10])[0] rlen = struct.unpack("<H", dbf[10:12])[0] elevs = [float(dbf[hlen + k*rlen + 1 : hlen + k*rlen + 33]) for k in range(nrec)] 

Caveats, all of them real

Range support is not guaranteed. A server that ignores the Range header returns 200 and the whole file. Check r.status (or the Content-Range header) before you trust the bytes.

Zip64. Archives over 4 GB, or with more than 65,535 entries, put 0xFFFFFFFF in the size and offset fields and keep the real values in a Zip64 extra field. This code does not handle that. I have a 4.9 GB LiDAR archive on the same bucket and it is exactly that case, so it would need the extra field parsed from the central directory record.

The tail has to be big enough. If the central directory is larger than the tail you fetched, you will not find the EOCD. 4 MB is generous for a few thousand entries; check cd_size and refetch if you are near the limit.

The central directory is authoritative, not the local header. Read names from the central directory. The local header's name and extra lengths are only there so you can compute the data offset.

Compression must be 0 or 8. Stored or deflate covers everything I have met in government shapefile zips; anything else needs a different decoder.

Total: about 60 lines of stdlib. For a 206 MB file where you want 14 MB, the difference is a few seconds against a download that takes minutes and needs the disk.