zlib简介
zlib是一个开源的数据压缩库,用于在应用程序中进行数据的压缩和解压缩操作。它提供了一组函数和数据结构,可以实现广泛的压缩算法,其中最常用的是Deflate算法。zlib库的设计简单、高效,并且具有广泛的应用领域。
压缩(compress)
#define SRCPATH "0:/system/image/"
#define COMPRPATH "0:/system/com/"
#define UNCOMPRPATH "0:/system/ucom/"
void compress_file(void)
{
printf("%s, %d, start !\r\n", __func__, __LINE__);
uint8_t *src_buf = NULL, *compr_buf = NULL;
int compr_file_len = 0,src_file_len = 0;
DIR dir;
uint8_t *filename = NULL;
FILINFO *fileinfo = NULL;
FIL fpsrc, fpcompr;
FRESULT res;
uint32_t bw;
int err = 0;
filename = WOKOO_MALLOC(32*sizeof(uint8_t));
if (filename == NULL){
printf("malloc filename error \r\n");
goto err_name;
}
fileinfo = (FILINFO*)WOKOO_MALLOC(sizeof(FILINFO));
if (NULL == fileinfo) {
goto err_finfo;
}
res = f_opendir(&dir, SRCPATH);
if (res == FR_OK)
{
while (1)
{
/* Reads a file in the directory*/
res = f_readdir(&dir, fileinfo);
if (res != FR_OK || fileinfo->fname[0] == 0)
{
/* Error/reached the end, exit */
break;
}
res = exfuns_file_type(fileinfo->fname);
/* Pick up four digits to see if it is a music file */
if ((res & 0xF0) == 0x00)
{
strcpy((uint8_t*)filename,SRCPATH);
/* Follow the file name*/
strcat((uint8_t*)filename,(const uint8_t*)fileinfo->fname);
printf("filename = %s\r\n",filename);
res = f_open(&fpsrc, (uint8_t *)filename, FA_OPEN_EXISTING | FA_READ);
if (res == 0)
{
src_file_len = f_size(&fpsrc);
printf("src_file_len = %d \r\n", src_file_len);
printf("src_file_len = 0x%x \r\n", src_file_len);
src_buf = (uint8_t *)WOKOO_MALLOC(src_file_len * sizeof(uint8_t));
if (src_buf == NULL) {
printf("malloc %s buf error !\r\n", filename);
continue;
}
res = f_read(&fpsrc, src_buf, src_file_len, &bw);
if (res != FR_OK) {
printf("read %s error !\r\n", filename);
continue;
}
f_close(&fpsrc);
}
compr_file_len = compressBound(src_file_len);
printf("compr_file_len = %d \r\n", compr_file_len);
compr_buf = (uint8_t *)WOKOO_MALLOC(compr_file_len * sizeof(uint8_t));
if (compr_buf == NULL) {
printf("malloc compr buf error \r\n");
continue;
}
err = compress(compr_buf, &compr_file_len, src_buf, src_file_len);
if(err != Z_OK)
{
printf("compress failed!\n");
continue ;
}
printf("0x%x 0x%x 0x%x 0x%x 0x%x \r\n", compr_buf[compr_file_len -4], compr_buf[compr_file_len - 3],
compr_buf[compr_file_len -2],compr_buf[compr_file_len - 1], compr_buf[compr_file_len]);
strcpy((uint8_t*)filename,COMPRPATH);
/* Follow the file name*/
strcat((uint8_t*)filename,(const uint8_t*)fileinfo->fname);
res = f_open(&fpcompr, filename, FA_CREATE_ALWAYS | FA_WRITE);
if (res == FR_OK) {
res = f_write(&fpcompr, compr_buf, compr_file_len, &bw);
if(res != FR_OK) {
printf("read image src error \r\n");
}
f_close(&fpcompr);
}
if (compr_buf != NULL) {
WOKOO_FREE(compr_buf);
compr_buf = NULL;
}
if (src_buf != NULL) {
WOKOO_FREE(src_buf);
src_buf = NULL;
}
}
}
f_closedir(&dir);
}
err_finfo:
if (fileinfo != NULL) {
WOKOO_FREE(fileinfo);
fileinfo = NULL;
}
err_name:
if (filename != NULL) {
WOKOO_FREE(filename);
filename = NULL;
}
return;
printf("%s, %d, end !\r\n", __func__, __LINE__);
}
解压(uncompress)
void uncompress_file(void)
{
printf("%s, %d, start !\r\n", __func__, __LINE__);
uint8_t *src_buf = NULL, *ucompr_buf = NULL;
int ucompr_file_len = 0,src_file_len = 0;
DIR dir;
uint8_t *filename;
FILINFO *fileinfo;
FIL fpsrc, fpcompr;
FRESULT res;
uint32_t bw;
int err = 0;
filename = WOKOO_MALLOC(32*sizeof(uint8_t));
if (filename == NULL){
printf("malloc filename error \r\n");
goto err_name;
}
fileinfo = (FILINFO*)WOKOO_MALLOC(sizeof(FILINFO));
if (NULL == fileinfo) {
goto err_finfo;
}
res = f_opendir(&dir, SRCPATH);
if (res == FR_OK)
{
while (1)
{
/* Reads a file in the directory*/
res = f_readdir(&dir, fileinfo);
if (res != FR_OK || fileinfo->fname[0] == 0)
{
/* Error/reached the end, exit */
break;
}
res = exfuns_file_type(fileinfo->fname);
/* Pick up four digits to see if it is a music file */
if ((res & 0xF0) == 0x00)
{
strcpy((uint8_t*)filename,COMPRPATH);
/* Follow the file name*/
strcat((uint8_t*)filename,(const uint8_t*)fileinfo->fname);
printf("filename = %s\r\n",filename);
res = f_open(&fpsrc, (uint8_t *)filename, FA_OPEN_EXISTING | FA_READ);
if (res == 0)
{
src_file_len = f_size(&fpsrc);
printf("src_file_len = %d \r\n", src_file_len);
src_buf = (uint8_t *)WOKOO_MALLOC(src_file_len * sizeof(uint8_t));
if (src_buf == NULL) {
printf("malloc %s buf error !\r\n", filename);
continue;
}
res = f_read(&fpsrc, src_buf, src_file_len, &bw);
if (res != FR_OK) {
printf("read %s error !\r\n", filename);
continue;
}
f_close(&fpsrc);
}
ucompr_file_len = src_buf[src_file_len -4] << 24 | src_buf[src_file_len - 3] << 16 |
src_buf[src_file_len -2] << 8 | src_buf[src_file_len - 1];
printf("ucompr_file_len : 0x%x \r\n", ucompr_file_len);
ucompr_buf = (uint8_t *)WOKOO_MALLOC(ucompr_file_len * sizeof(uint8_t));
if (ucompr_buf == NULL) {
printf("malloc uncompr buf error \r\n");
continue;
}
err = uncompress(ucompr_buf, &ucompr_file_len, src_buf, src_file_len);
if(err != Z_OK)
{
printf("uncompress failed!\n");
continue;
}
strcpy((uint8_t*)filename,UNCOMPRPATH);
/* Follow the file name*/
strcat((uint8_t*)filename,(const uint8_t*)fileinfo->fname);
res = f_open(&fpcompr, filename, FA_CREATE_ALWAYS | FA_WRITE);
if (res == FR_OK) {
res = f_write(&fpcompr, ucompr_buf, ucompr_file_len, &bw);
if(res != FR_OK) {
printf("read image src error \r\n");
}
f_close(&fpcompr);
}
if (ucompr_buf != NULL) {
WOKOO_FREE(ucompr_buf);
ucompr_buf = NULL;
}
if (src_buf != NULL) {
WOKOO_FREE(src_buf);
src_buf = NULL;
}
}
}
f_closedir(&dir);
}
err_finfo:
if (fileinfo != NULL) {
WOKOO_FREE(fileinfo);
fileinfo = NULL;
}
err_name:
if (filename != NULL) {
WOKOO_FREE(filename);
filename = NULL;
}
return;
printf("%s, %d, end !\r\n", __func__, __LINE__);
}
demo
void demo_zlib(void)
{
compress_file();
uncompress_file();
}