| 462 | } |
| 463 | |
| 464 | bool sendZipStream(ByteSink& out, const std::vector<SendFile>& files, const std::vector<std::string>& dirs, FileReader& src, |
| 465 | const CancelFn& cancelled, const ProgressFn& onBytes, bool& wasCancelled) |
| 466 | { |
| 467 | wasCancelled = false; |
| 468 | |
| 469 | std::vector<ZipEntry> central; |
| 470 | central.reserve(dirs.size() + files.size()); |
| 471 | uint32_t offset = 0; |
| 472 | |
| 473 | auto sendChunk = [&](const void* data, size_t n) -> bool { |
| 474 | if (!out.sendAll(data, n)) { |
| 475 | return false; |
| 476 | } |
| 477 | offset += (uint32_t)n; |
| 478 | if (onBytes) { |
| 479 | onBytes(n); |
| 480 | } |
| 481 | return true; |
| 482 | }; |
| 483 | |
| 484 | for (const auto& dir : dirs) { |
| 485 | ZipEntry centralEntry; |
| 486 | centralEntry.name = dir; |
| 487 | centralEntry.crc = 0; |
| 488 | centralEntry.size = 0; |
| 489 | centralEntry.offset = offset; |
| 490 | centralEntry.isDirectory = true; |
| 491 | |
| 492 | std::string hdr; |
| 493 | hdr.reserve(30 + dir.size()); |
| 494 | appendLe32(hdr, 0x04034b50); |
| 495 | appendLe16(hdr, 20); |
| 496 | appendLe16(hdr, 0); |
| 497 | appendLe16(hdr, 0); |
| 498 | appendLe16(hdr, 0); |
| 499 | appendLe16(hdr, 0); |
| 500 | appendLe32(hdr, 0); |
| 501 | appendLe32(hdr, 0); |
| 502 | appendLe32(hdr, 0); |
| 503 | appendLe16(hdr, (uint16_t)dir.size()); |
| 504 | appendLe16(hdr, 0); |
| 505 | hdr.append(dir); |
| 506 | if (!sendChunk(hdr.data(), hdr.size())) { |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | central.push_back(centralEntry); |
| 511 | } |
| 512 | |
| 513 | static const size_t kBuf = 0x40000; |
| 514 | std::unique_ptr<uint8_t[]> buf(new uint8_t[kBuf]); |
| 515 | |
| 516 | for (const auto& entry : files) { |
| 517 | ZipEntry centralEntry; |
| 518 | centralEntry.name = entry.relPath; |
| 519 | centralEntry.crc = 0; |
| 520 | centralEntry.size = entry.size; |
| 521 | centralEntry.offset = offset; |