Allow uncompressed files in zip archives

This commit is contained in:
2023-11-19 23:49:11 +01:00
parent 761d8d3d33
commit 681fc00a68
9 changed files with 98 additions and 3 deletions

View File

@@ -7,6 +7,7 @@
#include "ArchiveWriter.hpp"
#include "LibArchiveHelper.hpp"
#include "ArchiveStreamBufferWriter.hpp"
#include "SimpleFileTypeShouldCompressChecker.hpp"
#include <archive.h>
#include <archive_entry.h>
#include <iostream>
@@ -16,6 +17,7 @@ namespace OpenVulkano
ArchiveWriter::ArchiveWriter(const char* fileName, ArchiveConfiguration archiveConfiguration, const std::shared_ptr<spdlog::logger>& logger)
: ArchiveBase(archive_write_new(), archive_entry_new(), logger)
, m_archiveConfig(archiveConfiguration)
, m_shouldCompress(&SimpleFileTypeShouldCompressChecker)
{ //TODO error handling
ChkErr(archive_write_set_format(m_archive, archiveConfiguration.GetLibArchiveArchiveType()));
if (archiveConfiguration.type == ArchiveType::TAR)
@@ -87,6 +89,11 @@ namespace OpenVulkano
void ArchiveWriter::WriteHeader(const FileDescription& fileDescription)
{
if (m_asBuffer) { m_asBuffer->Close(); m_asBuffer = nullptr; }
if (m_archiveConfig.type.AllowsMixedCompressionLevels())
{
if (m_shouldCompress(fileDescription)) SetCompressed();
else SetUncompressed();
}
archive_entry_clear(m_archiveEntry);
archive_entry_set_pathname_utf8(m_archiveEntry, fileDescription.path.c_str());
if (fileDescription.size != FileDescription::UNKNOWN_SIZE)
@@ -95,7 +102,7 @@ namespace OpenVulkano
}
else
{
if (!m_archiveConfig.AllowsUnknownFileSize())
if (!m_archiveConfig.type.AllowsUnknownFileSize())
throw std::invalid_argument("Only ZIP files allow files of unknown size");
archive_entry_unset_size(m_archiveEntry);
}
@@ -106,4 +113,26 @@ namespace OpenVulkano
ChkErr(archive_write_header(m_archive, m_archiveEntry));
m_bytesWritten += fileDescription.size;
}
void ArchiveWriter::SetUncompressed()
{
if (!m_lastCompressed) return;
m_lastCompressed = false;
switch (m_archiveConfig.type)
{
case ArchiveType::ZIP: ChkErr(archive_write_zip_set_compression_store(m_archive));
default: ;
}
}
void ArchiveWriter::SetCompressed()
{
if (m_lastCompressed) return;
m_lastCompressed = true;
switch (m_archiveConfig.type)
{
case ArchiveType::ZIP: ChkErr(archive_write_zip_set_compression_deflate(m_archive));
default: ;
}
}
}