zlib1g-dev를 설치한다.
$ sudo apt -y install zlib1g-dev
컴파일 시 libz.so
를 링킹하여 컴파일한다.
libzip-dev를 설치한다.
$ sudo apt -y install liblz4-dev
컴파일 시 liblz4.so
를 링킹하여 컴파일한다.
압축
을 위한 데이터를 준비하자.
#include <string.h>
int main(void) {
const char *data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
unsigned long data_len = strlen(data);
return 0;
}
compress
를 이용하여 압축 후 데이터의 크기를 비교해보자.
#include <string.h>
#include <zlib.h>
#include <stdio.h>
int main(void) {
const char *data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
unsigned long data_len = strlen(data);
unsigned long compressed_len1 = data_len + (data_len * 0.1);
unsigned char compress_data1[compressed_len1] = { '\0' };
if (0 != compress(compress_data1, &compressed_len1, (Byte *)data, data_len)) return 0;
printf("compress1 : %lu -> %lu\n", data_len, compressed_len1);
return 0;
}
compress2는 압축률을 지정할 수 있는 인자가 존재한다.
#include <string.h>
#include <zlib.h>
#include <stdio.h>
int main(void) {
const char *data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
unsigned long data_len = strlen(data);
unsigned long compressed_len2 = data_len + (data_len * 0.1);
unsigned char compress_data2[compressed_len2] = { '\0' };
if (0 != compress2(compress_data2, &compressed_len2, (Byte *)data, data_len, 1)) return 0;
printf("compress2 : %lu -> %lu\n", data_len, compressed_len2);
return 0;
}
압축 후 파일로 생성해보자.
#include <string.h>
#include <zlib.h>
#include <stdio.h>
int main(void) {
const char *data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
gzFile fp;
int ec = 0;
if ((fp = gzopen("data.gz", "wb")) == Z_NULL) return -1;
if (gzwrite(fp, data, strlen(data)) < 0) printf("gzwrite error %s\n", gzerror(fp, &ec));
if (gzclose(fp) != Z_OK) printf("gzclose error %s\n", gzerror(fp, &ec));
return 0;
}
압축
을 위한 데이터를 준비하자.
#include <string.h>
int main(void) {
const char *data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
unsigned long data_len = strlen(data);
return 0;
}
LZ4_compress_default
를 이용하여 압축 후 데이터의 크기를 비교해보자.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <lz4.h>
int main(void) {
const char *data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
unsigned long data_len = strlen(data);
int maxcompressed_len = LZ4_compressBound(data_len);
char *compressed_data = (char *)malloc(maxcompressed_len);
int compressed_data_len = LZ4_compress_default(data, compressed_data, data_len, maxcompressed_len);
printf("%d -> %d\n", data_len, compressed_data_len);
return 0;
}
데이터를 압축한 후 해제
하여 데이터와 크기를 비교해보자.
#include <string.h>
#include <zlib.h>
#include <stdio.h>
int main(void) {
const char *data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
unsigned long data_len = strlen(data);
unsigned long compressed_len = data_len + (data_len * 0.1);
unsigned char compress_data[compressed_len] = { '\0' };
if (0 != compress(compress_data, &compressed_len, (Byte *)data, data_len)) return 0;
unsigned long uncompressed_len = compressed_len * 2;
unsigned char uncompressed_data[uncompressed_len] = { '\0' };
if (0 != uncompress(uncompressed_data, &uncompressed_len, compress_data, compressed_len)) return 0;
printf("uncompress : %lu-> %lu\n", compressed_len, uncompressed_len);
if (memcmp(data, uncompressed_data, uncompressed_len) == 0) printf("Validation Success.\n");
return 0;
}
압축파일을 로드한 뒤 해제해보자.
#include <string.h>
#include <zlib.h>
#include <stdio.h>
int main(void) {
gzFile fp;
int ec = 0;
char data[256] = { '\0' };
if ((fp = gzopen("data.gz", "rb")) == Z_NULL) return 0;
while ( !gzeof(fp) )
{
int rlen = gzread(fp, data, sizeof(data));
if (rlen < 0) printf("gzread error %s\n", gzerror(fp, &ec));
data[rlen] = '\0';
printf("%s\n", data);
}
if (gzclose(fp) != Z_OK) printf("gzclose error %s\n", gzerror(fp, &ec));
return 0;
}
데이터를 압축한 후 해제
하여 데이터와 크기를 비교해보자.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <lz4.h>
int main(void) {
const char *data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
unsigned long data_len = strlen(data);
int maxcompressed_len = LZ4_compressBound(data_len);
char *compressed_data = (char *)malloc(maxcompressed_len);
int compressed_data_len = LZ4_compress_default(data, compressed_data, data_len, maxcompressed_len);
char* decompressed_data = (char*)malloc(data_len);
int decompressed_data_len = LZ4_decompress_safe(compressed_data, decompressed_data, compressed_data_len, data_len);
if (memcmp(data, decompressed_data, decompressed_data_len) == 0) printf("Validation Success.\n");
return 0;
}