SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
vpkpp::ZIP Class Reference

#include <ZIP.h>

Inheritance diagram for vpkpp::ZIP:
Collaboration diagram for vpkpp::ZIP:

Public Member Functions

 ~ZIP () override
constexpr std::string_view getGUID () const override
 Get the GUID corresponding to the pack file type.
constexpr bool hasEntryChecksums () const override
 Returns true if the format has a checksum for each entry.
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 there is no checksum ability in the format, it will return an empty vector.
constexpr bool isCaseSensitive () const override
 Does the format support case-sensitive file names?
std::optional< std::vector< std::byte > > readEntry (const std::string &path_) const override
 Try to read the entry's data to a bytebuffer.
bool isReadOnly () const noexcept override
bool bake (const std::string &outputDir_, BakeOptions options, const EntryCallback &callback) override
 If output folder is an empty string, it will overwrite the original.
Attribute getSupportedEntryAttributes () const override
 Returns a list of supported entry attributes Mostly for GUI programs that show entries and their metadata in a table ;)
EntryCompressionType getEntryCompressionType (const std::string &path_) const
void setEntryCompressionType (const std::string &path_, EntryCompressionType type)
int16_t getEntryCompressionStrength (const std::string &path_) const
void setEntryCompressionStrength (const std::string &path_, int16_t strength)
Public Member Functions inherited from vpkpp::PackFile
 PackFile (const PackFile &other)=delete
PackFileoperator= (const PackFile &other)=delete
 PackFile (PackFile &&other) noexcept=default
PackFileoperator= (PackFile &&other) noexcept=default
virtual ~PackFile ()=default
template<typename T>
requires requires (const T&) {{T::GUID} -> std::convertible_to<std::string_view>;}
bool isInstanceOf () const
 Check if the pack file is an instance of the given pack file class.
virtual bool hasPackFileChecksum () const
 Returns true if the entire file has a checksum.
virtual bool verifyPackFileChecksum () const
 Verify the checksum of the entire file, returns true on success Will return true if there is no checksum ability in the format.
virtual bool hasPackFileSignature () const
 Returns true if the file is signed.
virtual bool verifyPackFileSignature () const
 Verify the file signature, returns true on success Will return true if there is no signature ability in the format.
bool hasEntry (const std::string &path, bool includeUnbaked=true) const
 Check if an entry exists given the file path.
std::optional< EntryfindEntry (const std::string &path_, bool includeUnbaked=true) const
 Try to find an entry given the file path.
std::optional< std::vector< std::byte > > operator[] (const std::string &path_) const
std::optional< std::string > readEntryText (const std::string &path) const
 Try to read the entry's data to a string.
void addEntry (const std::string &entryPath, const std::string &filepath, EntryOptions options={})
 Add a new entry from a file path - the first parameter is the path in the PackFile, the second is the path on disk.
void addEntry (const std::string &path, std::vector< std::byte > &&buffer, EntryOptions options={})
 Add a new entry from a buffer.
void addEntry (const std::string &path, std::span< const std::byte > buffer, EntryOptions options={})
 Add a new entry from a buffer.
void addDirectory (const std::string &entryBaseDir, const std::string &dir, EntryOptions options={})
 Adds new entries using the contents of a given directory.
void addDirectory (const std::string &entryBaseDir_, const std::string &dir, const EntryCreation &creation)
 Adds new entries using the contents of a given directory.
virtual bool renameEntry (const std::string &oldPath_, const std::string &newPath_)
 Rename an existing entry.
virtual bool renameDirectory (const std::string &oldDir_, const std::string &newDir_)
 Rename an existing directory.
virtual bool removeEntry (const std::string &path_)
 Remove an entry.
virtual std::size_t removeDirectory (const std::string &dirName_)
 Remove a directory.
bool extractEntry (const std::string &entryPath, const std::string &filepath) const
 Extract the given entry to disk at the given file path.
bool extractDirectory (const std::string &dir_, const std::string &outputDir) const
 Extract the given directory to disk under the given output directory.
bool extractAll (const std::string &outputDir, bool createUnderPackFileDir=true) const
 Extract the contents of the pack file to disk at the given directory.
bool extractAll (const std::string &outputDir, const EntryPredicate &predicate, bool stripSharedDirs=true) const
 Extract the contents of the pack file to disk at the given directory - only entries which match the predicate are extracted.
const EntryTriegetBakedEntries () const
 Get entries saved to disk.
const EntryTriegetUnbakedEntries () const
 Get entries that have been added but not yet baked.
std::size_t getEntryCount (bool includeUnbaked=true) const
 Get the number of entries in the pack file.
void runForAllEntries (const EntryCallback &operation, bool includeUnbaked=true) const
 Run a callback for each entry in the pack file.
void runForAllEntries (const std::string &parentDir, const EntryCallback &operation, bool recursive=true, bool includeUnbaked=true) const
 Run a callback for each entry in the pack file under the parent directory.
std::string_view getFilepath () const
 /home/user/pak01_dir.vpk
std::string getTruncatedFilepath () const
 /home/user/pak01_dir.vpk -> /home/user/pak01
std::string getFilename () const
 /home/user/pak01_dir.vpk -> pak01_dir.vpk
std::string getTruncatedFilename () const
 /home/user/pak01_dir.vpk -> pak01.vpk
std::string getFilestem () const
 /home/user/pak01_dir.vpk -> pak01_dir
virtual std::string getTruncatedFilestem () const
 /home/user/pak01_dir.vpk -> pak01
virtual operator std::string () const

Static Public Member Functions

static std::unique_ptr< PackFilecreate (const std::string &path)
 Create a ZIP file.
static std::unique_ptr< PackFileopen (const std::string &path, const EntryCallback &callback=nullptr)
 Open a ZIP file.
Static Public Member Functions inherited from vpkpp::PackFile
static std::unique_ptr< PackFileopen (const std::string &path, const EntryCallback &callback=nullptr, const OpenPropertyRequest &requestProperty=nullptr)
 Open a generic pack file. The parser is selected based on the file extension.
static std::vector< std::string > getOpenableExtensions ()
 Returns a sorted list of supported extensions for opening, e.g. {".bsp", ".vpk"}.
static std::string escapeEntryPathForWrite (const std::string &path)
 On Windows, some characters and file names are invalid - this escapes the given entry path.

Static Public Attributes

static constexpr std::string_view GUID = "3F3FDBC4F5D44B1F8A8E3AF5611B561B"

Protected Member Functions

 ZIP (const std::string &fullFilePath_)
void addEntryInternal (Entry &entry, const std::string &path, std::vector< std::byte > &buffer, EntryOptions options) override
bool bakeTempZip (const std::string &writeZipPath, BakeOptions options, const EntryCallback &callback) const
bool openZIP (std::string_view path)
void closeZIP ()
Protected Member Functions inherited from vpkpp::PackFile
 PackFile (std::string fullFilePath_)
void runForAllEntriesInternal (const std::function< void(const std::string &, Entry &)> &operation, bool includeUnbaked=true)
void runForAllEntriesInternal (const std::string &parentDir, const std::function< void(const std::string &, Entry &)> &operation, bool recursive=true, bool includeUnbaked=true)
std::vector< std::string > verifyEntryChecksumsUsingCRC32 () const
std::string getBakeOutputDir (const std::string &outputDir) const
void mergeUnbakedEntries ()
void setFullFilePath (const std::string &outputDir)
std::string cleanEntryPath (const std::string &path) const

Protected Attributes

const std::string tempZIPPath
void * streamHandle = nullptr
bool streamOpen = false
void * zipHandle = nullptr
bool zipOpen = false
Protected Attributes inherited from vpkpp::PackFile
std::string fullFilePath
EntryTrie entries
EntryTrie unbakedEntries

Additional Inherited Members

Public Types inherited from vpkpp::PackFile
enum class  OpenProperty { DECRYPTION_KEY }
using OpenPropertyRequest = std::function<std::vector<std::byte>(PackFile* packFile, OpenProperty property)>
template<typename R>
using EntryCallbackBase = std::function<R(const std::string& path, const Entry& entry)>
 Accepts the entry's path and metadata.
using EntryCallback = EntryCallbackBase<void>
using EntryPredicate = EntryCallbackBase<bool>
using EntryCreation = std::function<EntryOptions(const std::string& path)>
using EntryTrie = tsl::htrie_map<char, Entry>
Protected Types inherited from vpkpp::PackFile
using OpenFactoryFunctionBasic = std::function<std::unique_ptr<PackFile>(const std::string& path, const EntryCallback& callback)>
using OpenFactoryFunction = std::function<std::unique_ptr<PackFile>(const std::string& path, const EntryCallback& callback, const OpenPropertyRequest& requestProperty)>
Static Protected Member Functions inherited from vpkpp::PackFile
static Entry createNewEntry ()
static std::optional< std::vector< std::byte > > readUnbakedEntry (const Entry &entry)
static std::unordered_map< std::string, std::vector< OpenFactoryFunction > > & getOpenExtensionRegistry ()
static const OpenFactoryFunctionregisterOpenExtensionForTypeFactory (std::string_view extension, const OpenFactoryFunctionBasic &factory)
static const OpenFactoryFunctionregisterOpenExtensionForTypeFactory (std::string_view extension, const OpenFactoryFunction &factory)

Detailed Description

Definition at line 17 of file ZIP.h.

Constructor & Destructor Documentation

◆ ~ZIP()

ZIP::~ZIP ( )
override

Definition at line 25 of file ZIP.cpp.

◆ ZIP()

ZIP::ZIP ( const std::string & fullFilePath_)
explicitprotected

Definition at line 21 of file ZIP.cpp.

Member Function Documentation

◆ addEntryInternal()

void ZIP::addEntryInternal ( Entry & entry,
const std::string & path,
std::vector< std::byte > & buffer,
EntryOptions options )
overrideprotectedvirtual

Implements vpkpp::PackFile.

Definition at line 124 of file ZIP.cpp.

◆ bake()

bool ZIP::bake ( const std::string & outputDir_,
BakeOptions options,
const EntryCallback & callback )
overridevirtual

If output folder is an empty string, it will overwrite the original.

Implements vpkpp::PackFile.

Definition at line 131 of file ZIP.cpp.

◆ bakeTempZip()

bool ZIP::bakeTempZip ( const std::string & writeZipPath,
BakeOptions options,
const EntryCallback & callback ) const
protected

Definition at line 187 of file ZIP.cpp.

◆ closeZIP()

void ZIP::closeZIP ( )
protected

Definition at line 272 of file ZIP.cpp.

◆ create()

std::unique_ptr< PackFile > ZIP::create ( const std::string & path)
static

Create a ZIP file.

Definition at line 29 of file ZIP.cpp.

◆ getEntryCompressionStrength()

int16_t ZIP::getEntryCompressionStrength ( const std::string & path_) const
nodiscard

Definition at line 172 of file ZIP.cpp.

◆ getEntryCompressionType()

EntryCompressionType ZIP::getEntryCompressionType ( const std::string & path_) const
nodiscard

Definition at line 157 of file ZIP.cpp.

◆ getGUID()

std::string_view vpkpp::ZIP::getGUID ( ) const
inlinenodiscardconstexproverridevirtual

Get the GUID corresponding to the pack file type.

Implements vpkpp::PackFile.

Definition at line 29 of file ZIP.h.

◆ getSupportedEntryAttributes()

Attribute ZIP::getSupportedEntryAttributes ( ) const
nodiscardoverridevirtual

Returns a list of supported entry attributes Mostly for GUI programs that show entries and their metadata in a table ;)

Reimplemented from vpkpp::PackFile.

Definition at line 152 of file ZIP.cpp.

◆ hasEntryChecksums()

bool vpkpp::ZIP::hasEntryChecksums ( ) const
inlinenodiscardconstexproverridevirtual

Returns true if the format has a checksum for each entry.

Reimplemented from vpkpp::PackFile.

Definition at line 33 of file ZIP.h.

◆ isCaseSensitive()

bool vpkpp::ZIP::isCaseSensitive ( ) const
inlinenodiscardconstexproverridevirtual

Does the format support case-sensitive file names?

Reimplemented from vpkpp::PackFile.

Definition at line 39 of file ZIP.h.

◆ isReadOnly()

bool ZIP::isReadOnly ( ) const
nodiscardoverridevirtualnoexcept

Reimplemented from vpkpp::PackFile.

Definition at line 118 of file ZIP.cpp.

◆ open()

std::unique_ptr< PackFile > ZIP::open ( const std::string & path,
const EntryCallback & callback = nullptr )
staticnodiscard

Open a ZIP file.

Definition at line 46 of file ZIP.cpp.

◆ openZIP()

bool ZIP::openZIP ( std::string_view path)
protected

Definition at line 254 of file ZIP.cpp.

◆ readEntry()

std::optional< std::vector< std::byte > > ZIP::readEntry ( const std::string & path_) const
nodiscardoverridevirtual

Try to read the entry's data to a bytebuffer.

Implements vpkpp::PackFile.

Definition at line 91 of file ZIP.cpp.

◆ setEntryCompressionStrength()

void ZIP::setEntryCompressionStrength ( const std::string & path_,
int16_t strength )

Definition at line 180 of file ZIP.cpp.

◆ setEntryCompressionType()

void ZIP::setEntryCompressionType ( const std::string & path_,
EntryCompressionType type )

Definition at line 165 of file ZIP.cpp.

◆ verifyEntryChecksums()

std::vector< std::string > ZIP::verifyEntryChecksums ( ) const
nodiscardoverridevirtual

Verify the checksums of each file, if a file fails the check its path will be added to the vector If there is no checksum ability in the format, it will return an empty vector.

Reimplemented from vpkpp::PackFile.

Definition at line 87 of file ZIP.cpp.

Member Data Documentation

◆ GUID

std::string_view vpkpp::ZIP::GUID = "3F3FDBC4F5D44B1F8A8E3AF5611B561B"
staticconstexpr

Definition at line 27 of file ZIP.h.

◆ streamHandle

void* vpkpp::ZIP::streamHandle = nullptr
protected

Definition at line 72 of file ZIP.h.

◆ streamOpen

bool vpkpp::ZIP::streamOpen = false
protected

Definition at line 73 of file ZIP.h.

◆ tempZIPPath

const std::string vpkpp::ZIP::tempZIPPath
protected

Definition at line 70 of file ZIP.h.

◆ zipHandle

void* vpkpp::ZIP::zipHandle = nullptr
protected

Definition at line 75 of file ZIP.h.

◆ zipOpen

bool vpkpp::ZIP::zipOpen = false
protected

Definition at line 76 of file ZIP.h.


The documentation for this class was generated from the following files: