CppUtils/Utils/zlib/zlib_cpp/include/zcompressor.hpp

311 lines
6.3 KiB
C++
Raw Normal View History

2025-06-05 10:06:43 +08:00
#ifndef ZCOMPRESSOR_HPP
#define ZCOMPRESSOR_HPP
#include "zstream.hpp"
namespace zlib_cpp {
/**
* @brief GZip头部信息类
*/
class GZipHeader {
private:
bool _text = false;
uint32_t _time = 0;
int _extraFlags = 0;
int _os = 0;
std::vector<uint8_t> _extra;
std::string _name;
std::string _comment;
bool _hcrc = false;
public:
/**
* @brief
*/
GZipHeader() = default;
/**
* @brief
*
* @param isText
*/
void setText(bool isText);
/**
* @brief
*
* @return
*/
bool isText() const;
/**
* @brief
*
* @param time
*/
void setTime(uint32_t time);
/**
* @brief
*
* @return
*/
uint32_t getTime() const;
/**
* @brief
*
* @param flags
*/
void setExtraFlags(int flags);
/**
* @brief
*
* @return
*/
int getExtraFlags() const;
/**
* @brief
*
* @param os
*/
void setOS(int os);
/**
* @brief
*
* @return
*/
int getOS() const;
/**
* @brief
*
* @param extra
*/
void setExtra(const std::vector<uint8_t>& extra);
/**
* @brief
*
* @return
*/
const std::vector<uint8_t>& getExtra() const;
/**
* @brief
*
* @param name
*/
void setName(const std::string& name);
/**
* @brief
*
* @return
*/
const std::string& getName() const;
/**
* @brief
*
* @param comment
*/
void setComment(const std::string& comment);
/**
* @brief
*
* @return
*/
const std::string& getComment() const;
/**
* @brief HCRC标志
*
* @param hcrc HCRC标志
*/
void setHCRC(bool hcrc);
/**
* @brief HCRC标志
*
* @return HCRC标志
*/
bool getHCRC() const;
};
/**
* @brief
*/
class ZCompressor : public ZStream {
private:
CompressionLevel _level = CompressionLevel::DEFAULT_COMPRESSION;
CompressionStrategy _strategy = CompressionStrategy::DEFAULT_STRATEGY;
int _windowBits = 15; // 默认windowBits
int _memLevel = 8; // 默认memLevel
std::unique_ptr<GZipHeader> _gzipHeader;
bool _isGzip = false;
public:
/**
* @brief
*/
ZCompressor();
/**
* @brief
*
* @param level
* @param strategy
*/
ZCompressor(CompressionLevel level, CompressionStrategy strategy = CompressionStrategy::DEFAULT_STRATEGY);
/**
* @brief
*
* @param level
* @param windowBits 使zlib头/
* @param memLevel 使
* @param strategy
*/
ZCompressor(CompressionLevel level, int windowBits, int memLevel,
CompressionStrategy strategy = CompressionStrategy::DEFAULT_STRATEGY);
/**
* @brief
*/
virtual ~ZCompressor();
/**
* @brief 使
*
* @param data
* @return
*/
std::vector<uint8_t> compress(const std::vector<uint8_t>& data);
/**
* @brief
*
* @param flush
* @return
*/
StatusCode deflate(FlushMode flush = FlushMode::NO_FLUSH);
/**
* @brief
*
* @param dictionary
* @return
*/
StatusCode setDictionary(const std::vector<uint8_t>& dictionary);
/**
* @brief
*
* @return
*/
std::vector<uint8_t> getDictionary();
/**
* @brief
*
* @return
*/
virtual StatusCode reset() override;
/**
* @brief
*
* @param level
* @param strategy
* @return
*/
StatusCode setParams(CompressionLevel level, CompressionStrategy strategy);
/**
* @brief
*
* @param goodLength
* @param maxLazy
* @param niceLength
* @param maxChain
* @return
*/
StatusCode tune(int goodLength, int maxLazy, int niceLength, int maxChain);
/**
* @brief GZip头信息
*
* @param header GZip头信息
* @return
*/
StatusCode setHeader(const GZipHeader& header);
/**
* @brief
*
* @param sourceLen
* @return
*/
static size_t getBound(size_t sourceLen);
/**
* @brief GZip模式
*
* @param enable
*/
void enableGZip(bool enable = true);
/**
* @brief
*
* @param level
*/
void setLevel(CompressionLevel level);
/**
* @brief
*
* @return
*/
CompressionLevel getLevel() const;
/**
* @brief
*
* @param strategy
*/
void setStrategy(CompressionStrategy strategy);
/**
* @brief
*
* @return
*/
CompressionStrategy getStrategy() const;
protected:
/**
* @brief
*
* @return
*/
virtual StatusCode init() override;
/**
* @brief
*
* @return
*/
virtual StatusCode end() override;
};
} // namespace zlib_cpp
#endif // ZCOMPRESSOR_HPP