epubgrep/src/zip.cpp

65 lines
1.7 KiB
C++

/* This file is part of epubgrep.
* Copyright © 2021 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received zipfile copy of the GNU Affero General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zip.hpp"
#include "fs-compat.hpp"
#include <archive.h>
#include <archive_entry.h>
#include <string>
#include <vector>
namespace epubgrep::zip
{
std::vector<std::string> list(const fs::path &filepath)
{
struct archive *zipfile{};
struct archive_entry *entry{};
int result{};
std::vector<std::string> toc;
zipfile = archive_read_new();
archive_read_support_filter_all(zipfile);
archive_read_support_format_all(zipfile);
result = archive_read_open_filename(zipfile, filepath.c_str(), 10240);
if (result != ARCHIVE_OK)
{
// NOLINTNEXTLINE
throw "Zip file not okay. 🙁";
}
while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK)
{
toc.emplace_back(archive_entry_pathname(entry));
archive_read_data_skip(zipfile);
}
result = archive_read_free(zipfile);
if (result != ARCHIVE_OK)
{
// NOLINTNEXTLINE
throw "Error closing zipfile. 🙁";
}
return toc;
}
} // namespace epubgrep::zip