Shared ZIP writer. compress=false → stored (method 0); compress=true → raw-deflate (method 8). Headers carry compressed + uncompressed sizes independently; the CRC is always over the uncompressed data.
| 203 | // raw-deflate (method 8). Headers carry compressed + uncompressed sizes |
| 204 | // independently; the CRC is always over the uncompressed data. |
| 205 | QByteArray writeZipImpl(const QList<ZipEntryData>& entries, bool compress) |
| 206 | { |
| 207 | if (entries.size() > std::numeric_limits<quint16>::max()) |
| 208 | return {}; |
| 209 | |
| 210 | QByteArray out; |
| 211 | QByteArray centralDir; |
| 212 | for (const ZipEntryData& entry : entries) { |
| 213 | const QByteArray name = entry.name.toUtf8(); |
| 214 | const QByteArray& raw = entry.data; |
| 215 | |
| 216 | QByteArray deflated; |
| 217 | if (compress && !rawDeflate(raw, deflated)) |
| 218 | return {}; |
| 219 | const QByteArray& payload = compress ? deflated : raw; |
| 220 | const quint16 method = compress ? quint16(8) : quint16(0); |
| 221 | |
| 222 | if (name.size() > std::numeric_limits<quint16>::max() |
| 223 | || raw.size() > std::numeric_limits<quint32>::max() |
| 224 | || payload.size() > std::numeric_limits<quint32>::max() |
| 225 | || out.size() > std::numeric_limits<quint32>::max()) { |
| 226 | return {}; |
| 227 | } |
| 228 | |
| 229 | const quint32 localOffset = static_cast<quint32>(out.size()); |
| 230 | const quint32 checksum = crc32(0L, reinterpret_cast<const Bytef*>(raw.constData()), |
| 231 | static_cast<uInt>(raw.size())); |
| 232 | |
| 233 | appendLe32(out, 0x04034b50); |
| 234 | appendLe16(out, 20); |
| 235 | appendLe16(out, 0); |
| 236 | appendLe16(out, method); |
| 237 | appendLe16(out, 0); |
| 238 | appendLe16(out, 0); |
| 239 | appendLe32(out, checksum); |
| 240 | appendLe32(out, static_cast<quint32>(payload.size())); |
| 241 | appendLe32(out, static_cast<quint32>(raw.size())); |
| 242 | appendLe16(out, static_cast<quint16>(name.size())); |
| 243 | appendLe16(out, 0); |
| 244 | out += name; |
| 245 | out += payload; |
| 246 | |
| 247 | appendLe32(centralDir, 0x02014b50); |
| 248 | appendLe16(centralDir, 20); |
| 249 | appendLe16(centralDir, 20); |
| 250 | appendLe16(centralDir, 0); |
| 251 | appendLe16(centralDir, method); |
| 252 | appendLe16(centralDir, 0); |
| 253 | appendLe16(centralDir, 0); |
| 254 | appendLe32(centralDir, checksum); |
| 255 | appendLe32(centralDir, static_cast<quint32>(payload.size())); |
| 256 | appendLe32(centralDir, static_cast<quint32>(raw.size())); |
| 257 | appendLe16(centralDir, static_cast<quint16>(name.size())); |
| 258 | appendLe16(centralDir, 0); |
| 259 | appendLe16(centralDir, 0); |
| 260 | appendLe16(centralDir, 0); |
| 261 | appendLe16(centralDir, 0); |
| 262 | appendLe32(centralDir, 0); |
no test coverage detected