Expand archive reader
This commit is contained in:
@@ -66,6 +66,15 @@ namespace OpenVulkano
|
|||||||
m_open = true;
|
m_open = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ArchiveReader::Open(const std::filesystem::path& archiveFile)
|
||||||
|
{
|
||||||
|
PrepOpen();
|
||||||
|
ChkErr(archive_read_open_filename(m_archive, archiveFile.c_str(), BUFFER_SIZE));
|
||||||
|
ReadNextHeader();
|
||||||
|
return HasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool ArchiveReader::Open(const char* archiveFile)
|
bool ArchiveReader::Open(const char* archiveFile)
|
||||||
{
|
{
|
||||||
if (archiveFile[0] == '\0')
|
if (archiveFile[0] == '\0')
|
||||||
@@ -176,20 +185,14 @@ namespace OpenVulkano
|
|||||||
ArchiveType::Type type = *m_archiveType;
|
ArchiveType::Type type = *m_archiveType;
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case ArchiveType::ZIP:
|
case ArchiveType::SEVEN_ZIP: return archive_read_support_format_7zip;
|
||||||
return archive_read_support_format_zip;
|
case ArchiveType::ZIP: return archive_read_support_format_zip;
|
||||||
case ArchiveType::SEVEN_ZIP:
|
case ArchiveType::CPIO: return archive_read_support_format_cpio;
|
||||||
return archive_read_support_format_7zip;
|
case ArchiveType::ISO: return archive_read_support_format_iso9660;
|
||||||
case ArchiveType::CPIO:
|
case ArchiveType::TAR: return archive_read_support_format_tar;
|
||||||
return archive_read_support_format_cpio;
|
case ArchiveType::WARC: return archive_read_support_format_warc;
|
||||||
case ArchiveType::ISO:
|
case ArchiveType::XAR: return archive_read_support_format_xar;
|
||||||
return archive_read_support_format_iso9660;
|
case ArchiveType::SHAR: break; // nothing to do
|
||||||
case ArchiveType::TAR:
|
|
||||||
return archive_read_support_format_tar;
|
|
||||||
case ArchiveType::WARC:
|
|
||||||
return archive_read_support_format_warc;
|
|
||||||
case ArchiveType::XAR:
|
|
||||||
return archive_read_support_format_xar;
|
|
||||||
}
|
}
|
||||||
return archive_read_support_format_all;
|
return archive_read_support_format_all;
|
||||||
}
|
}
|
||||||
@@ -204,6 +207,16 @@ namespace OpenVulkano
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t ArchiveReader::ExtractRemaining(const std::filesystem::path &targetDir)
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
while (ExtractNext(targetDir))
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
bool ArchiveReader::HasNext() const
|
bool ArchiveReader::HasNext() const
|
||||||
{
|
{
|
||||||
return m_archiveEntry != nullptr;
|
return m_archiveEntry != nullptr;
|
||||||
@@ -246,19 +259,21 @@ namespace OpenVulkano
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<FileDescription> ArchiveReader::ExtractNext(std::string_view targetDir)
|
std::optional<FileDescription> ArchiveReader::ExtractNext(const std::filesystem::path& targetDir)
|
||||||
{
|
{
|
||||||
if (SkipTill(std::filesystem::file_type::regular))
|
if (SkipTill(std::filesystem::file_type::regular))
|
||||||
{
|
{
|
||||||
FileDescription fileDescription = GetNextDescription();
|
FileDescription fileDescription = GetNextDescription();
|
||||||
std::string outputFileName;
|
std::filesystem::path outputFilePath;
|
||||||
outputFileName.reserve(targetDir.size() + 1 + fileDescription.path.size());
|
if (m_filePathRewrite)
|
||||||
outputFileName.append(targetDir).append("/").append(fileDescription.path);
|
outputFilePath = targetDir / m_filePathRewrite(fileDescription.path);
|
||||||
|
else
|
||||||
|
outputFilePath = targetDir / fileDescription.path;
|
||||||
|
|
||||||
std::unique_ptr<archive, decltype(archive_write_free)*> a(archive_write_disk_new(), archive_write_free);
|
std::unique_ptr<archive, decltype(archive_write_free)*> a(archive_write_disk_new(), archive_write_free);
|
||||||
std::unique_ptr<archive_entry, decltype(archive_entry_free)*> entry(archive_entry_clone(m_archiveEntry), archive_entry_free);
|
std::unique_ptr<archive_entry, decltype(archive_entry_free)*> entry(archive_entry_clone(m_archiveEntry), archive_entry_free);
|
||||||
|
|
||||||
archive_entry_set_pathname(entry.get(), outputFileName.c_str());
|
archive_entry_set_pathname(entry.get(), outputFilePath.c_str());
|
||||||
|
|
||||||
ChkErr(archive_write_disk_set_options(a.get(), ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS));
|
ChkErr(archive_write_disk_set_options(a.get(), ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS));
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <ostream>
|
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
namespace OpenVulkano
|
namespace OpenVulkano
|
||||||
@@ -22,6 +21,7 @@ namespace OpenVulkano
|
|||||||
std::optional<ArchiveType::Type> m_archiveType;
|
std::optional<ArchiveType::Type> m_archiveType;
|
||||||
std::queue<std::string> m_archivesToRead;
|
std::queue<std::string> m_archivesToRead;
|
||||||
std::function<const char*()> m_passwordCallback;
|
std::function<const char*()> m_passwordCallback;
|
||||||
|
std::function<std::string(std::string)> m_filePathRewrite;
|
||||||
bool m_open = false;
|
bool m_open = false;
|
||||||
bool m_eof = false;
|
bool m_eof = false;
|
||||||
|
|
||||||
@@ -38,6 +38,8 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
void SetPasswordCallback(const decltype(m_passwordCallback)& callback);
|
void SetPasswordCallback(const decltype(m_passwordCallback)& callback);
|
||||||
|
|
||||||
|
bool Open(const std::filesystem::path& archiveFile);
|
||||||
|
|
||||||
bool Open(const char* archiveFile);
|
bool Open(const char* archiveFile);
|
||||||
|
|
||||||
bool Open(const std::string& archiveFile);
|
bool Open(const std::string& archiveFile);
|
||||||
@@ -50,7 +52,9 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
[[nodiscard]] bool IsOpen() const { return m_open; }
|
[[nodiscard]] bool IsOpen() const { return m_open; }
|
||||||
|
|
||||||
size_t ExtractRemaining(std::string_view targetDir);
|
[[deprecated]] size_t ExtractRemaining(std::string_view targetDir);
|
||||||
|
|
||||||
|
size_t ExtractRemaining(const std::filesystem::path& targetDir);
|
||||||
|
|
||||||
// Element wise operations
|
// Element wise operations
|
||||||
[[nodiscard]] bool HasNext() const;
|
[[nodiscard]] bool HasNext() const;
|
||||||
@@ -63,7 +67,9 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
std::optional<FileDescription> GetNextDirectory();
|
std::optional<FileDescription> GetNextDirectory();
|
||||||
|
|
||||||
std::optional<FileDescription> ExtractNext(std::string_view targetDir);
|
[[deprecated]] std::optional<FileDescription> ExtractNext(std::string_view targetDir) { return ExtractNext(std::filesystem::path(targetDir)); }
|
||||||
|
|
||||||
|
std::optional<FileDescription> ExtractNext(const std::filesystem::path& targetDir);
|
||||||
|
|
||||||
std::optional<std::pair<FileDescription, Array<char>>> GetNextFile();
|
std::optional<std::pair<FileDescription, Array<char>>> GetNextFile();
|
||||||
|
|
||||||
@@ -77,6 +83,10 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
const decltype(m_passwordCallback)& GetPasswordCallback() const { return m_passwordCallback; }
|
const decltype(m_passwordCallback)& GetPasswordCallback() const { return m_passwordCallback; }
|
||||||
|
|
||||||
|
void SetPathRewriteFunction(const decltype(m_filePathRewrite)& rewriteFunc) { m_filePathRewrite = rewriteFunc; }
|
||||||
|
|
||||||
|
const decltype(m_filePathRewrite)& GetPathRewriteFunction() const { return m_filePathRewrite; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ReadNextHeader();
|
void ReadNextHeader();
|
||||||
std::function<int(archive*)> GetCurrentFormatReadFunc() const;
|
std::function<int(archive*)> GetCurrentFormatReadFunc() const;
|
||||||
|
|||||||
Reference in New Issue
Block a user