| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "zipdatasource.h" |
| 5 | |
| 6 | #include <QByteArray> |
| 7 | #include <QIODevice> |
| 8 | #include <QString> |
| 9 | #include <algorithm> |
| 10 | #include <format> |
| 11 | #include <iterator> |
| 12 | #include <quazip.h> |
| 13 | #include <quazipfile.h> |
| 14 | #include <stdexcept> |
| 15 | |
| 16 | ZipDataSource::ZipDataSource(std::filesystem::path const &path) noexcept |
| 17 | : path_(path) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | std::string ZipDataSource::source() const |
| 22 | { |
| 23 | return path_.string(); |
| 24 | } |
| 25 | |
| 26 | bool ZipDataSource::read(std::string const &internalDataPath, |
| 27 | std::vector<char> &data) |
| 28 | { |
| 29 | if (!internalDataPath.empty()) { |
| 30 | QuaZip zip(QString::fromStdString(source())); |
| 31 | if (zip.open(QuaZip::mdUnzip)) { |
| 32 | if (zip.setCurrentFile(QString::fromStdString(internalDataPath))) { |
| 33 | |
| 34 | QuaZipFile file(&zip); |
| 35 | if (file.open(QIODevice::ReadOnly)) { |
| 36 | |
| 37 | data.clear(); |
| 38 | auto fileData = file.readAll(); |
| 39 | std::copy(fileData.cbegin(), fileData.cend(), std::back_inserter(data)); |
| 40 | |
| 41 | file.close(); |
| 42 | zip.close(); |
| 43 | return true; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | zip.close(); |
| 48 | } |
| 49 | else { |
| 50 | throw std::runtime_error(std::format("Failed to open file {}", source())); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return false; |
| 55 | } |