| 439 | } |
| 440 | |
| 441 | bool ZipFile::readFileToStream(const char* filename, Print& out, const size_t chunkSize) { |
| 442 | const ScopedOpenClose zip{*this}; |
| 443 | if (!zip) return false; |
| 444 | |
| 445 | FileStatSlim fileStat = {}; |
| 446 | if (!loadFileStatSlim(filename, &fileStat)) return false; |
| 447 | |
| 448 | const long fileOffset = getDataOffset(fileStat); |
| 449 | if (fileOffset < 0) return false; |
| 450 | |
| 451 | file.seek(fileOffset); |
| 452 | const auto deflatedDataSize = fileStat.compressedSize; |
| 453 | const auto inflatedDataSize = fileStat.uncompressedSize; |
| 454 | |
| 455 | if (fileStat.method == ZIP_METHOD_STORED) { |
| 456 | // no deflation, just read content |
| 457 | const auto buffer = static_cast<uint8_t*>(malloc(chunkSize)); |
| 458 | if (!buffer) { |
| 459 | LOG_ERR("ZIP", "Failed to allocate memory for buffer"); |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | size_t remaining = inflatedDataSize; |
| 464 | while (remaining > 0) { |
| 465 | const size_t dataRead = file.read(buffer, remaining < chunkSize ? remaining : chunkSize); |
| 466 | if (dataRead == 0) { |
| 467 | LOG_ERR("ZIP", "Could not read more bytes"); |
| 468 | free(buffer); |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | if (out.write(buffer, dataRead) != dataRead) { |
| 473 | LOG_ERR("ZIP", "Failed to write all output bytes to stream"); |
| 474 | free(buffer); |
| 475 | return false; |
| 476 | } |
| 477 | remaining -= dataRead; |
| 478 | } |
| 479 | |
| 480 | free(buffer); |
| 481 | return true; |
| 482 | } |
| 483 | |
| 484 | if (fileStat.method == ZIP_METHOD_DEFLATED) { |
| 485 | auto* fileReadBuffer = static_cast<uint8_t*>(malloc(chunkSize)); |
| 486 | if (!fileReadBuffer) { |
| 487 | LOG_ERR("ZIP", "Failed to allocate memory for zip file read buffer"); |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | auto* outputBuffer = static_cast<uint8_t*>(malloc(chunkSize)); |
| 492 | if (!outputBuffer) { |
| 493 | LOG_ERR("ZIP", "Failed to allocate memory for output buffer"); |
| 494 | free(fileReadBuffer); |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | ZipInflateCtx ctx; |
nothing calls this directly
no test coverage detected