2 Copyright (C) 2024 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "dcpomatic_assert.h"
23 #include "exceptions.h"
25 #include <dcp/filesystem.h>
26 #include <dcp/scope_guard.h>
28 #include <boost/filesystem.hpp>
34 using std::runtime_error;
35 using std::shared_ptr;
39 Unzipper::Unzipper(boost::filesystem::path file)
42 _zip = zip_open(dcp::filesystem::fix_long_path(file).string().c_str(), ZIP_RDONLY, &error);
44 throw FileError("could not open ZIP file", file);
56 Unzipper::get(string const& filename)
58 auto file = zip_fopen(_zip, filename.c_str(), 0);
60 throw runtime_error(String::compose(_("Could not find file %1 in ZIP file"), filename));
63 dcp::ScopeGuard sg = [file]() { zip_fclose(file); };
65 int constexpr maximum = 65536;
67 dcp::ArrayData data(maximum);
68 int remaining = maximum;
69 uint8_t* next = data.data();
71 while (remaining > 0) {
72 auto read = zip_fread(file, next, remaining);
75 } else if (read == -1) {
76 throw runtime_error("Could not read from ZIP file");
84 throw runtime_error("File from ZIP is too big");
87 return string(reinterpret_cast<char*>(data.data()), maximum - remaining);