#include <cstdio>
#include <iosfwd>
#include <iostream>
#include <vector>
#include <zconf.h>
#include <zlib.h>
#include <iomanip>
#include <cassert>
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <iomanip>
#include <mutex>
#include <sstream>
#include <string>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <limits.h>
#include <thread>
void add_buffer_to_vector(std::vector<char> &vector, const char *buffer, uLongf length) {
for (int character_index = 0; character_index < length; character_index++) {
char current_character = buffer[character_index];
vector.push_back(current_character);
}
}
int compress_vector(std::vector<char> source, std::vector<char> &destination) {
unsigned long source_length = source.size();
uLongf destination_length = compressBound(source_length);
char *destination_data = (char *) malloc(destination_length);
if (destination_data == nullptr) {
return Z_MEM_ERROR;
}
Bytef *source_data = (Bytef *) source.data();
int return_value = compress2((Bytef *) destination_data, &destination_length, source_data, source_length,
Z_BEST_COMPRESSION);
add_buffer_to_vector(destination, destination_data, destination_length);
if (destination.size() > 30 )
{
free(destination_data);
}
return return_value;
}
int decompress_vector(std::vector<char> source, std::vector<char> &destination) {
unsigned long source_length = source.size();
uLongf destination_length = compressBound(source_length);
char *destination_data = (char *) malloc(destination_length);
if (destination_data == nullptr) {
return Z_MEM_ERROR;
}
Bytef *source_data = (Bytef *) source.data();
int return_value = uncompress((Bytef *) destination_data, &destination_length, source_data, source.size());
add_buffer_to_vector(destination, destination_data, destination_length);
free(destination_data);
return return_value;
}
void add_string_to_vector(std::vector<char> &uncompressed_data,
const char *my_string) {
int character_index = 0;
while (true) {
char current_character = my_string[character_index];
uncompressed_data.push_back(current_character);
if (current_character == '\00') {
break;
}
character_index++;
}
}
// https://stackoverflow.com/a/27173017/3764804
void print_bytes(std::ostream &stream, const unsigned char *data, size_t data_length, bool format = true) {
stream << std::setfill('0');
for (size_t data_index = 0; data_index < data_length; ++data_index) {
stream << std::hex << std::setw(2) << (int) data[data_index];
if (format) {
stream << (((data_index + 1) % 16 == 0) ? "\n" : " ");
}
}
stream << std::endl;
}
typedef struct {
uint16_t offset;
char content[];
} LogPersisterBuffer;
void test_compression() {
LogPersisterBuffer *buffer;
std::vector<char> uncompressed(0);
auto *my_string = (char *) "Hello, world!";
//输入到buffer里,uncompressed就是压缩前的buffer
std::vector<char> compressed(0);
int i=0;
while(i++<2) {
memcpy(buffer->content, my_string, 20 );
buffer->offset += 20;
add_string_to_vector(uncompressed, buffer->content);
int compression_result = compress_vector(uncompressed, compressed);
assert(compression_result == F_OK);
}
std::ostream &standard_output = std::cout;
print_bytes(standard_output, (const unsigned char *) compressed.data(), compressed.size(), false);
std::vector<char> decompressed(0);
int decompression_result = decompress_vector(compressed, decompressed);
assert(decompression_result == F_OK);
printf("Uncompressed: %s\n", uncompressed.data());
printf("Compressed: ");
printf("Decompressed: %s\n", decompressed.data());
}
int main(int argc, char *argv[]) {
test_compression();
return EXIT_SUCCESS;
}
stream buffer
最新推荐文章于 2024-07-17 15:57:43 发布
本文探讨了C++中使用zlib库进行数据压缩和解压的基本操作,包括`compress_vector`和`decompress_vector`函数。通过`test_compression`函数展示了如何压缩字符串并验证其解压后的效果。关键技术和标签涉及数据压缩算法、内存管理与C++编程。
摘要由CSDN通过智能技术生成