| 493 | } |
| 494 | |
| 495 | bool sendZipStream(ByteSink& out, const std::vector<SendFile>& files, const std::vector<std::string>& dirs, FileReader& src, |
| 496 | const CancelFn& cancelled, const ProgressFn& onBytes, bool& wasCancelled) |
| 497 | { |
| 498 | wasCancelled = false; |
| 499 | |
| 500 | std::vector<ZipEntry> central; |
| 501 | central.reserve(dirs.size() + files.size()); |
| 502 | uint32_t offset = 0; |
| 503 | |
| 504 | auto sendChunk = [&](const void* data, size_t n) -> bool { |
| 505 | if (!out.sendAll(data, n)) { |
| 506 | return false; |
| 507 | } |
| 508 | offset += (uint32_t)n; |
| 509 | if (onBytes) { |
| 510 | onBytes(n); |
| 511 | } |
| 512 | return true; |
| 513 | }; |
| 514 | |
| 515 | for (const auto& dir : dirs) { |
| 516 | ZipEntry centralEntry; |
| 517 | centralEntry.name = dir; |
| 518 | centralEntry.crc = 0; |
| 519 | centralEntry.size = 0; |
| 520 | centralEntry.offset = offset; |
| 521 | centralEntry.isDirectory = true; |
| 522 | |
| 523 | std::string hdr; |
| 524 | hdr.reserve(30 + dir.size()); |
| 525 | appendLe32(hdr, 0x04034b50); |
| 526 | appendLe16(hdr, 20); |
| 527 | appendLe16(hdr, 0); |
| 528 | appendLe16(hdr, 0); |
| 529 | appendLe16(hdr, 0); |
| 530 | appendLe16(hdr, 0); |
| 531 | appendLe32(hdr, 0); |
| 532 | appendLe32(hdr, 0); |
| 533 | appendLe32(hdr, 0); |
| 534 | appendLe16(hdr, (uint16_t)dir.size()); |
| 535 | appendLe16(hdr, 0); |
| 536 | hdr.append(dir); |
| 537 | if (!sendChunk(hdr.data(), hdr.size())) { |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | central.push_back(centralEntry); |
| 542 | } |
| 543 | |
| 544 | static const size_t kBuf = 0x40000; |
| 545 | std::unique_ptr<uint8_t[]> buf(new uint8_t[kBuf]); |
| 546 | |
| 547 | for (const auto& entry : files) { |
| 548 | ZipEntry centralEntry; |
| 549 | centralEntry.name = entry.relPath; |
| 550 | centralEntry.crc = 0; |
| 551 | centralEntry.size = entry.size; |
| 552 | centralEntry.offset = offset; |