// zipw.hpp — minimal ZIP writer (stored entries, no compression). The archives // bundle already-binary artifacts (STEP, STL) for a single download; deflate // would buy little and cost a dependency. #pragma once #include #include #include #include namespace zipw { inline uint32_t crc32(const std::string& data) { static uint32_t table[256]; static bool init = false; if (!init) { for (uint32_t i = 0; i < 256; ++i) { uint32_t c = i; for (int k = 0; k < 8; ++k) c = (c & 1) ? 0xEDB88320u ^ (c >> 1) : c >> 1; table[i] = c; } init = true; } uint32_t c = 0xFFFFFFFFu; for (unsigned char ch : data) c = table[(c ^ ch) & 0xFF] ^ (c >> 8); return c ^ 0xFFFFFFFFu; } // Build a ZIP from (name, content) pairs. Entries are stored uncompressed; // fixed DOS timestamp (determinism beats mtime fidelity here). inline std::string makeZip(const std::vector>& files) { std::string out, central; auto put16 = [](std::string& s, uint16_t v) { s.push_back((char)(v & 0xFF)); s.push_back((char)(v >> 8)); }; auto put32 = [](std::string& s, uint32_t v) { for (int i = 0; i < 4; ++i) s.push_back((char)((v >> (8 * i)) & 0xFF)); }; const uint16_t dosTime = 0, dosDate = (uint16_t)((2026 - 1980) << 9 | 1 << 5 | 1); for (const auto& [name, data] : files) { uint32_t offset = (uint32_t)out.size(); uint32_t crc = crc32(data); // local file header put32(out, 0x04034b50); put16(out, 20); put16(out, 0); put16(out, 0); put16(out, dosTime); put16(out, dosDate); put32(out, crc); put32(out, (uint32_t)data.size()); put32(out, (uint32_t)data.size()); put16(out, (uint16_t)name.size()); put16(out, 0); out += name; out += data; // central directory entry put32(central, 0x02014b50); put16(central, 20); put16(central, 20); put16(central, 0); put16(central, 0); put16(central, dosTime); put16(central, dosDate); put32(central, crc); put32(central, (uint32_t)data.size()); put32(central, (uint32_t)data.size()); put16(central, (uint16_t)name.size()); put16(central, 0); put16(central, 0); put16(central, 0); put16(central, 0); put32(central, 0); put32(central, offset); central += name; } uint32_t cdOffset = (uint32_t)out.size(); out += central; // end of central directory put32(out, 0x06054b50); put16(out, 0); put16(out, 0); put16(out, (uint16_t)files.size()); put16(out, (uint16_t)files.size()); put32(out, (uint32_t)central.size()); put32(out, cdOffset); put16(out, 0); return out; } } // namespace zipw