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 static constexpr std::string_view GUID = "3F3FDBC4F5D44B1F8A8E3AF5611B561B";
28
29 [[nodiscard]] constexpr std::string_view getGUID() const override {
30 return ZIP::GUID;
31 }
32
33 [[nodiscard]] constexpr bool hasEntryChecksums() const override {
34 return true;
35 }
36
37 [[nodiscard]] std::vector<std::string> verifyEntryChecksums() const override;
38
39 [[nodiscard]] constexpr bool isCaseSensitive() const override {
40 return true;
41 }
42
43 [[nodiscard]] std::optional<std::vector<std::byte>> readEntry(const std::string& path_) const override;
44
45 [[nodiscard]] bool isReadOnly() const noexcept override;
46
47 bool bake(const std::string& outputDir_ /*= ""*/, BakeOptions options /*= {}*/, const EntryCallback& callback /*= nullptr*/) override;
48
49 [[nodiscard]] Attribute getSupportedEntryAttributes() const override;
50
51 [[nodiscard]] EntryCompressionType getEntryCompressionType(const std::string& path_) const;
52
53 void setEntryCompressionType(const std::string& path_, EntryCompressionType type);
54
55 [[nodiscard]] int16_t getEntryCompressionStrength(const std::string& path_) const;
56
57 void setEntryCompressionStrength(const std::string& path_, int16_t strength);
58
59protected:
60 explicit ZIP(const std::string& fullFilePath_);
61
62 void addEntryInternal(Entry& entry, const std::string& path, std::vector<std::byte>& buffer, EntryOptions options) override;
63
64 bool bakeTempZip(const std::string& writeZipPath, BakeOptions options, const EntryCallback& callback) const; // NOLINT(*-use-nodiscard)
65
66 bool openZIP(std::string_view path);
67
68 void closeZIP();
69
70 const std::string tempZIPPath;
71
72 void* streamHandle = nullptr;
73 bool streamOpen = false;
74
75 void* zipHandle = nullptr;
76 bool zipOpen = false;
77
78private:
79 VPKPP_REGISTER_PACKFILE_OPEN(BEE_EXTENSION, &ZIP::open);
80 VPKPP_REGISTER_PACKFILE_OPEN(BMZ_EXTENSION, &ZIP::open);
81 VPKPP_REGISTER_PACKFILE_OPEN(FPK_EXTENSION, &ZIP::open);
82 VPKPP_REGISTER_PACKFILE_OPEN(PK3_EXTENSION, &ZIP::open);
83 VPKPP_REGISTER_PACKFILE_OPEN(PK4_EXTENSION, &ZIP::open);
84 VPKPP_REGISTER_PACKFILE_OPEN(PKZ_EXTENSION, &ZIP::open);
85 VPKPP_REGISTER_PACKFILE_OPEN(ZIP_EXTENSION, &ZIP::open);
86};
87
88} // 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:33
ZIP(const std::string &fullFilePath_)
Definition ZIP.cpp:21
const std::string tempZIPPath
Definition ZIP.h:70
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:72
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:73
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:76
static constexpr std::string_view GUID
Definition ZIP.h:27
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:75
~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:39
constexpr std::string_view getGUID() const override
Get the GUID corresponding to the pack file type.
Definition ZIP.h:29
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