SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
ZIP.h
Go to the documentation of this file.
1// ReSharper disable CppRedundantQualifier
2
3#pragma once
4
5#include "../PackFile.h"
6
7namespace vpkpp {
8
9constexpr std::string_view BEE_EXTENSION = ".bee_pack";
10constexpr std::string_view BMZ_EXTENSION = ".bmz";
11constexpr std::string_view FPK_EXTENSION = ".fpk";
12constexpr std::string_view PK3_EXTENSION = ".pk3";
13constexpr std::string_view PK4_EXTENSION = ".pk4";
14constexpr std::string_view PKZ_EXTENSION = ".pkz";
15constexpr std::string_view ZIP_EXTENSION = ".zip";
16
17class ZIP : public PackFile {
18public:
19 ~ZIP() override;
20
22 static std::unique_ptr<PackFile> create(const std::string& path);
23
25 [[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr);
26
27 [[nodiscard]] constexpr bool hasEntryChecksums() const override {
28 return true;
29 }
30
31 [[nodiscard]] std::vector<std::string> verifyEntryChecksums() const override;
32
33 [[nodiscard]] constexpr bool isCaseSensitive() const override {
34 return true;
35 }
36
37 [[nodiscard]] std::optional<std::vector<std::byte>> readEntry(const std::string& path_) const override;
38
39 [[nodiscard]] bool isReadOnly() const noexcept override;
40
41 bool bake(const std::string& outputDir_ /*= ""*/, BakeOptions options /*= {}*/, const EntryCallback& callback /*= nullptr*/) override;
42
43 [[nodiscard]] Attribute getSupportedEntryAttributes() const override;
44
45 [[nodiscard]] EntryCompressionType getEntryCompressionType(const std::string& path_) const;
46
47 void setEntryCompressionType(const std::string& path_, EntryCompressionType type);
48
49 [[nodiscard]] int16_t getEntryCompressionStrength(const std::string& path_) const;
50
51 void setEntryCompressionStrength(const std::string& path_, int16_t strength);
52
53protected:
54 explicit ZIP(const std::string& fullFilePath_);
55
56 void addEntryInternal(Entry& entry, const std::string& path, std::vector<std::byte>& buffer, EntryOptions options) override;
57
58 bool bakeTempZip(const std::string& writeZipPath, BakeOptions options, const EntryCallback& callback) const; // NOLINT(*-use-nodiscard)
59
60 bool openZIP(std::string_view path);
61
62 void closeZIP();
63
64 const std::string tempZIPPath;
65
66 void* streamHandle = nullptr;
67 bool streamOpen = false;
68
69 void* zipHandle = nullptr;
70 bool zipOpen = false;
71
72private:
73 VPKPP_REGISTER_PACKFILE_OPEN(BEE_EXTENSION, &ZIP::open);
74 VPKPP_REGISTER_PACKFILE_OPEN(BMZ_EXTENSION, &ZIP::open);
75 VPKPP_REGISTER_PACKFILE_OPEN(FPK_EXTENSION, &ZIP::open);
76 VPKPP_REGISTER_PACKFILE_OPEN(PK3_EXTENSION, &ZIP::open);
77 VPKPP_REGISTER_PACKFILE_OPEN(PK4_EXTENSION, &ZIP::open);
78 VPKPP_REGISTER_PACKFILE_OPEN(PKZ_EXTENSION, &ZIP::open);
79 VPKPP_REGISTER_PACKFILE_OPEN(ZIP_EXTENSION, &ZIP::open);
80};
81
82} // namespace vpkpp
This class represents the metadata that a file has inside a PackFile.
Definition Entry.h:14
EntryCallbackBase< void > EntryCallback
Definition PackFile.h:38
PackFile(const PackFile &other)=delete
void addEntryInternal(Entry &entry, const std::string &path, std::vector< std::byte > &buffer, EntryOptions options) override
Definition ZIP.cpp:124
Attribute getSupportedEntryAttributes() const override
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
Definition ZIP.cpp:152
constexpr bool hasEntryChecksums() const override
Returns true if the format has a checksum for each entry.
Definition ZIP.h:27
ZIP(const std::string &fullFilePath_)
Definition ZIP.cpp:21
const std::string tempZIPPath
Definition ZIP.h:64
void setEntryCompressionType(const std::string &path_, EntryCompressionType type)
Definition ZIP.cpp:165
void setEntryCompressionStrength(const std::string &path_, int16_t strength)
Definition ZIP.cpp:180
void closeZIP()
Definition ZIP.cpp:272
void * streamHandle
Definition ZIP.h:66
static std::unique_ptr< PackFile > open(const std::string &path, const EntryCallback &callback=nullptr)
Open a ZIP file.
Definition ZIP.cpp:46
bool isReadOnly() const noexcept override
Definition ZIP.cpp:118
std::vector< std::string > verifyEntryChecksums() const override
Verify the checksums of each file, if a file fails the check its path will be added to the vector If ...
Definition ZIP.cpp:87
int16_t getEntryCompressionStrength(const std::string &path_) const
Definition ZIP.cpp:172
bool streamOpen
Definition ZIP.h:67
bool openZIP(std::string_view path)
Definition ZIP.cpp:254
bool bake(const std::string &outputDir_, BakeOptions options, const EntryCallback &callback) override
If output folder is an empty string, it will overwrite the original.
Definition ZIP.cpp:131
bool zipOpen
Definition ZIP.h:70
std::optional< std::vector< std::byte > > readEntry(const std::string &path_) const override
Try to read the entry's data to a bytebuffer.
Definition ZIP.cpp:91
void * zipHandle
Definition ZIP.h:69
~ZIP() override
Definition ZIP.cpp:25
bool bakeTempZip(const std::string &writeZipPath, BakeOptions options, const EntryCallback &callback) const
Definition ZIP.cpp:187
constexpr bool isCaseSensitive() const override
Does the format support case-sensitive file names?
Definition ZIP.h:33
static std::unique_ptr< PackFile > create(const std::string &path)
Create a ZIP file.
Definition ZIP.cpp:29
EntryCompressionType getEntryCompressionType(const std::string &path_) const
Definition ZIP.cpp:157
constexpr std::string_view FPK_EXTENSION
Definition ZIP.h:11
constexpr std::string_view ZIP_EXTENSION
Definition ZIP.h:15
Attribute
Definition Attribute.h:7
constexpr std::string_view PKZ_EXTENSION
Definition ZIP.h:14
constexpr std::string_view BEE_EXTENSION
Definition ZIP.h:9
constexpr std::string_view PK3_EXTENSION
Definition ZIP.h:12
EntryCompressionType
Definition Options.h:7
constexpr std::string_view BMZ_EXTENSION
Definition ZIP.h:10
constexpr std::string_view PK4_EXTENSION
Definition ZIP.h:13