当前位置:首页>编程日记>正文

miniz库简介及使用

miniz:Google开源库,它是单一的C源文件,紧缩/膨胀压缩库,使用zlib兼容API,ZIP归档读写,PNG写方式。关于miniz的更详细介绍可以参考:https://code.google.com/archive/p/miniz/

miniz.c is a lossless, high performance data compression library in a single source file that implements the zlib(RFC 1950) and Deflate(RFC 1951) compressed data format specification standards.

miniz.c also contains simple to use functions for writing .PNG format image files and reading/writing/appending .ZIP format archives.

从https://github.com/richgel999/miniz下载源代码,也可以从google下载:https://code.google.com/archive/p/miniz/downloads。

测试miniz的使用,新建miniz_Test控制台工程,测试代码(来源于源代码中的example*.c文件)如下:

#include "funset.hpp"
#include "../../src/miniz/miniz.c"typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint;#define my_max(a,b) (((a) > (b)) ? (a) : (b))
#define my_min(a,b) (((a) < (b)) ? (a) : (b))#define BUF_SIZE (1024 * 1024)// Demonstrates miniz.c's compress() and uncompress() functions (same as zlib's),
// also a crude decompressor fuzzy test.
int test_miniz_1()
{// The string to compress.static const char *s_pStr = "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson.";uint step = 0;int cmp_status;uLong src_len = (uLong)strlen(s_pStr);uLong cmp_len = compressBound(src_len);uLong uncomp_len = src_len;uint8 *pCmp, *pUncomp;uint total_succeeded = 0;printf("miniz.c version: %s
", MZ_VERSION);do {// Allocate buffers to hold compressed and uncompressed data.pCmp = (mz_uint8 *)malloc((size_t)cmp_len);pUncomp = (mz_uint8 *)malloc((size_t)src_len);if ((!pCmp) || (!pUncomp)) {printf("Out of memory!
");return EXIT_FAILURE;}// Compress the string.cmp_status = compress(pCmp, &cmp_len, (const unsigned char *)s_pStr, src_len);if (cmp_status != Z_OK) {printf("compress() failed!
");free(pCmp);free(pUncomp);return EXIT_FAILURE;}printf("Compressed from %u to %u bytes
", (mz_uint32)src_len, (mz_uint32)cmp_len);if (step) {// Purposely corrupt the compressed data if fuzzy testing (this is a very crude fuzzy test).uint n = 1 + (rand() % 3);while (n--) {uint i = rand() % cmp_len;pCmp[i] ^= (rand() & 0xFF);}}// Decompress.cmp_status = uncompress(pUncomp, &uncomp_len, pCmp, cmp_len);total_succeeded += (cmp_status == Z_OK);if (step) {printf("Simple fuzzy test: step %u total_succeeded: %u
", step, total_succeeded);} else {if (cmp_status != Z_OK) {printf("uncompress failed!
");free(pCmp);free(pUncomp);return EXIT_FAILURE;}printf("Decompressed from %u to %u bytes
", (mz_uint32)cmp_len, (mz_uint32)uncomp_len);// Ensure uncompress() returned the expected data.if ((uncomp_len != src_len) || (memcmp(pUncomp, s_pStr, (size_t)src_len))) {printf("Decompression failed!
");free(pCmp);free(pUncomp);return EXIT_FAILURE;}}free(pCmp);free(pUncomp);step++;// Keep on fuzzy testing if there's a non-empty command line.} while (step <= 2);printf("Success.
");return EXIT_SUCCESS;
}// Demonstration of miniz.c's ZIP archive API's. Adds a bunch of filesto test.zip,
// dumps file stat info on each file in the archive, then extracts a single file into memory.
int test_miniz_2()
{// The string to compress.static const char *s_pTest_str ="MISSION CONTROL I wouldn't worry too much about the computer. First of all, there is still a chance that he is right, despite your tests, and" "if it should happen again, we suggest eliminating this possibility by allowing the unit to remain in place and seeing whether or not it" "actually fails. If the computer should turn out to be wrong, the situation is still not alarming. The type of obsessional error he may be" "guilty of is not unknown among the latest generation of HAL 9000 computers. It has almost always revolved around a single detail, such as" "the one you have described, and it has never interfered with the integrity or reliability of the computer's performance in other areas." "No one is certain of the cause of this kind of malfunctioning. It may be over-programming, but it could also be any number of reasons. In any" "event, it is somewhat analogous to human neurotic behavior. Does this answer your query?  Zero-five-three-Zero, MC, transmission concluded.";static const char *s_pComment = "This is a comment";int i, sort_iter;mz_bool status;size_t uncomp_size;mz_zip_archive zip_archive;void *p;const int N = 5; //50;char data[2048];char archive_filename[64];static const char *s_Test_archive_filename = "E:/GitCode/Messy_Test/testdata/miniz2.zip";assert((strlen(s_pTest_str) + 64) < sizeof(data));printf("miniz.c version: %s
", MZ_VERSION);// Delete the test archive, so it doesn't keep growing as we run this testremove(s_Test_archive_filename);// Append a bunch of text files to the test archivefor (i = (N - 1); i >= 0; --i) {sprintf(archive_filename, "%u.txt", i);sprintf(data, "%u %s %u", (N - 1) - i, s_pTest_str, i);// Add a new file to the archive. Note this is an IN-PLACE operation, so if it fails your archive is probably hosed (its central directory may not be complete) but it should be recoverable using zip -F or -FF. So use caution with this guy.// A more robust way to add a file to an archive would be to read it into memory, perform the operation, then write a new archive out to a temp file and then delete/rename the files.// Or, write a new archive to disk to a temp file, then delete/rename the files. For this test this API is fine.status = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, archive_filename, data, strlen(data) + 1, s_pComment, (uint16)strlen(s_pComment), MZ_BEST_COMPRESSION);if (!status) {printf("mz_zip_add_mem_to_archive_file_in_place failed!
");return EXIT_FAILURE;}}// Add a directory entry for testingstatus = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, "directory/", NULL, 0, "no comment", (uint16)strlen("no comment"), MZ_BEST_COMPRESSION);if (!status) {printf("mz_zip_add_mem_to_archive_file_in_place failed!
");return EXIT_FAILURE;}// Now try to open the archive.memset(&zip_archive, 0, sizeof(zip_archive));status = mz_zip_reader_init_file(&zip_archive, s_Test_archive_filename, 0);if (!status) {printf("mz_zip_reader_init_file() failed!
");return EXIT_FAILURE;}// Get and print information about each file in the archive.for (i = 0; i < (int)mz_zip_reader_get_num_files(&zip_archive); i++) {mz_zip_archive_file_stat file_stat;if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) {printf("mz_zip_reader_file_stat() failed!
");mz_zip_reader_end(&zip_archive);return EXIT_FAILURE;}printf("Filename: "%s", Comment: "%s", Uncompressed size: %u, Compressed size: %u, Is Dir: %u
", file_stat.m_filename, file_stat.m_comment, (uint)file_stat.m_uncomp_size, (uint)file_stat.m_comp_size, mz_zip_reader_is_file_a_directory(&zip_archive, i));if (!strcmp(file_stat.m_filename, "directory/")) {if (!mz_zip_reader_is_file_a_directory(&zip_archive, i)) {printf("mz_zip_reader_is_file_a_directory() didn't return the expected results!
");mz_zip_reader_end(&zip_archive);return EXIT_FAILURE;}}}// Close the archive, freeing any resources it was usingmz_zip_reader_end(&zip_archive);// Now verify the compressed datafor (sort_iter = 0; sort_iter < 2; sort_iter++) {memset(&zip_archive, 0, sizeof(zip_archive));status = mz_zip_reader_init_file(&zip_archive, s_Test_archive_filename, sort_iter ? MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY : 0);if (!status) {printf("mz_zip_reader_init_file() failed!
");return EXIT_FAILURE;}for (i = 0; i < N; i++) {sprintf(archive_filename, "%u.txt", i);sprintf(data, "%u %s %u", (N - 1) - i, s_pTest_str, i);// Try to extract all the files to the heap.p = mz_zip_reader_extract_file_to_heap(&zip_archive, archive_filename, &uncomp_size, 0);if (!p) {printf("mz_zip_reader_extract_file_to_heap() failed!
");mz_zip_reader_end(&zip_archive);return EXIT_FAILURE;}// Make sure the extraction really succeeded.if ((uncomp_size != (strlen(data) + 1)) || (memcmp(p, data, strlen(data)))) {printf("mz_zip_reader_extract_file_to_heap() failed to extract the proper data
");mz_free(p);mz_zip_reader_end(&zip_archive);return EXIT_FAILURE;}printf("Successfully extracted file "%s", size %u
", archive_filename, (uint)uncomp_size);printf("File data: "%s"
", (const char *)p);// We're done.mz_free(p);}// Close the archive, freeing any resources it was usingmz_zip_reader_end(&zip_archive);}printf("Success.
");return EXIT_SUCCESS;
}// Demonstrates how to use miniz.c's deflate() and inflate() functions for simple file compression.
// Command line tool for file compression/decompression.
int test_miniz_3()
{static uint8 s_inbuf[BUF_SIZE];static uint8 s_outbuf[BUF_SIZE];const char *pMode;FILE *pInfile, *pOutfile;uint infile_size;int level = Z_BEST_COMPRESSION;z_stream stream;int p = 1;const char *pSrc_filename;const char *pDst_filename;long file_loc;printf("miniz.c version: %s
", MZ_VERSION);int argc = 5;char* argv[5] {"", "-l5", "c", "E:/GitCode/Messy_Test/testdata/infile.zip", "E:/GitCode/Messy_Test/testdata/outfile_compress.zip"};//char* argv[5] {"", "-l5", "d", "E:/GitCode/Messy_Test/testdata/outfile_compress.zip", "E:/GitCode/Messy_Test/testdata/outfile_decompress.zip"};if (argc < 4) {printf("Usage: example3 [options] [mode:c or d] infile outfile
");printf("
Modes:
");printf("c - Compresses file infile to a zlib stream in file outfile
");printf("d - Decompress zlib stream in file infile to file outfile
");printf("
Options:
");printf("-l[0-10] - Compression level, higher values are slower.
");return EXIT_FAILURE;}while ((p < argc) && (argv[p][0] == '-')) {switch (argv[p][1]) {case 'l': {level = atoi(&argv[1][2]);if ((level < 0) || (level > 10)) {printf("Invalid level!
");return EXIT_FAILURE;}break;}default: {printf("Invalid option: %s
", argv[p]);return EXIT_FAILURE;}}p++;}if ((argc - p) < 3) {printf("Must specify mode, input filename, and output filename after options!
");return EXIT_FAILURE;} else if ((argc - p) > 3) {printf("Too many filenames!
");return EXIT_FAILURE;}pMode = argv[p++];if (!strchr("cCdD", pMode[0])) {printf("Invalid mode!
");return EXIT_FAILURE;}pSrc_filename = argv[p++];pDst_filename = argv[p++];printf("Mode: %c, Level: %u
Input File: "%s"
Output File: "%s"
", pMode[0], level, pSrc_filename, pDst_filename);// Open input file.pInfile = fopen(pSrc_filename, "rb");if (!pInfile) {printf("Failed opening input file!
");return EXIT_FAILURE;}// Determine input file's size.fseek(pInfile, 0, SEEK_END);file_loc = ftell(pInfile);fseek(pInfile, 0, SEEK_SET);if ((file_loc < 0) || (file_loc > INT_MAX)) {// This is not a limitation of miniz or tinfl, but this example.printf("File is too large to be processed by this example.
");return EXIT_FAILURE;}infile_size = (uint)file_loc;// Open output file.pOutfile = fopen(pDst_filename, "wb");if (!pOutfile) {printf("Failed opening output file!
");return EXIT_FAILURE;}printf("Input file size: %u
", infile_size);// Init the z_streammemset(&stream, 0, sizeof(stream));stream.next_in = s_inbuf;stream.avail_in = 0;stream.next_out = s_outbuf;stream.avail_out = BUF_SIZE;if ((pMode[0] == 'c') || (pMode[0] == 'C')) {// Compression.uint infile_remaining = infile_size;if (deflateInit(&stream, level) != Z_OK) {printf("deflateInit() failed!
");return EXIT_FAILURE;}for (;;) {int status;if (!stream.avail_in) {// Input buffer is empty, so read more bytes from input file.uint n = my_min(BUF_SIZE, infile_remaining);if (fread(s_inbuf, 1, n, pInfile) != n) {printf("Failed reading from input file!
");return EXIT_FAILURE;}stream.next_in = s_inbuf;stream.avail_in = n;infile_remaining -= n;//printf("Input bytes remaining: %u
", infile_remaining);}status = deflate(&stream, infile_remaining ? Z_NO_FLUSH : Z_FINISH);if ((status == Z_STREAM_END) || (!stream.avail_out)) {// Output buffer is full, or compression is done, so write buffer to output file.uint n = BUF_SIZE - stream.avail_out;if (fwrite(s_outbuf, 1, n, pOutfile) != n) {printf("Failed writing to output file!
");return EXIT_FAILURE;}stream.next_out = s_outbuf;stream.avail_out = BUF_SIZE;}if (status == Z_STREAM_END)break;else if (status != Z_OK) {printf("deflate() failed with status %i!
", status);return EXIT_FAILURE;}}if (deflateEnd(&stream) != Z_OK) {printf("deflateEnd() failed!
");return EXIT_FAILURE;}} else if ((pMode[0] == 'd') || (pMode[0] == 'D')) {// Decompression.uint infile_remaining = infile_size;if (inflateInit(&stream)) {printf("inflateInit() failed!
");return EXIT_FAILURE;}for (;;) {int status;if (!stream.avail_in) {// Input buffer is empty, so read more bytes from input file.uint n = my_min(BUF_SIZE, infile_remaining);if (fread(s_inbuf, 1, n, pInfile) != n) {printf("Failed reading from input file!
");return EXIT_FAILURE;}stream.next_in = s_inbuf;stream.avail_in = n;infile_remaining -= n;}status = inflate(&stream, Z_SYNC_FLUSH);if ((status == Z_STREAM_END) || (!stream.avail_out)) {// Output buffer is full, or decompression is done, so write buffer to output file.uint n = BUF_SIZE - stream.avail_out;if (fwrite(s_outbuf, 1, n, pOutfile) != n) {printf("Failed writing to output file!
");return EXIT_FAILURE;}stream.next_out = s_outbuf;stream.avail_out = BUF_SIZE;}if (status == Z_STREAM_END)break;else if (status != Z_OK) {printf("inflate() failed with status %i!
", status);return EXIT_FAILURE;}}if (inflateEnd(&stream) != Z_OK) {printf("inflateEnd() failed!
");return EXIT_FAILURE;}} else {printf("Invalid mode!
");return EXIT_FAILURE;}fclose(pInfile);if (EOF == fclose(pOutfile)) {printf("Failed writing to output file!
");return EXIT_FAILURE;}printf("Total input bytes: %u
", (mz_uint32)stream.total_in);printf("Total output bytes: %u
", (mz_uint32)stream.total_out);printf("Success.
");return EXIT_SUCCESS;
}// Uses tinfl.c to decompress a zlib stream in memory to an output file
int test_miniz_4()
{// need include "tinfl.c", conflict with "miniz.c"// reference: example4.creturn 0;
}// Demonstrates how to use miniz.c's low-level tdefl_compress() and tinfl_inflate() API's for simple file to file compression/decompression.
// The low-level API's are the fastest, make no use of dynamic memory allocation, and are the most flexible functions exposed by miniz.c.
int test_miniz_5()
{// reference: example5.c// conflict with other examples(1/2/3)return 0;
}// Demonstrates how to miniz's PNG writer func
int test_miniz_6()
{// reference: example6.c// conflict with other examples(1/2/3)return 0;
}

GitHub: https://github.com/fengbingchun/Messy_Test


http://www.coolblog.cn/news/9974ddefd3712a0b.html

相关文章:

  • asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询
  • s7day2学习记录
  • 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  • 矿Spring入门Demo
  • 拼音怎么写_老师:不会写的字用圈代替,看到孩子试卷,网友:人才
  • Linux 实时流量监测(iptraf中文图解)
  • Win10 + Python + GPU版MXNet + VS2015 + RTools + R配置
  • 美颜
  • shell访问php文件夹,Shell获取某目录下所有文件夹的名称
  • 如何优雅的实现 Spring Boot 接口参数加密解密?
  • LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络
  • Mac原生Terminal快速登录ssh
  • java受保护的数据与_Javascript类定义语法,私有成员、受保护成员、静态成员等介绍...
  • mysql commit 机制_1024MySQL事物提交机制
  • 支撑微博千亿调用的轻量级RPC框架:Motan
  • jquery 使用小技巧
  • 2019-9
  • 法拉利虚拟学院2010 服务器,法拉利虚拟学院2010
  • vscode pylint 错误_将实际未错误的py库添加到pylint白名单
  • 科学计算工具NumPy(3):ndarray的元素处理
  • 工程师在工作电脑存 64G 不雅文件,被公司开除后索赔 41 万,结果…
  • linux批量创建用户和密码
  • newinsets用法java_Java XYPlot.setInsets方法代碼示例
  • js常用阻止冒泡事件
  • 气泡图在开源监控工具中的应用效果
  • 各类型土地利用图例_划重点!国土空间总体规划——土地利用
  • php 启动服务器监听
  • dubbo简单示例
  • 【设计模式】 模式PK:策略模式VS状态模式
  • [iptables]Redhat 7.2下使用iptables实现NAT
  • Ubuntu13.10:[3]如何开启SSH SERVER服务
  • CSS小技巧——CSS滚动条美化
  • JS实现-页面数据无限加载
  • 阿里巴巴分布式服务框架 Dubbo
  • 最新DOS大全
  • Django View(视图系统)
  • 阿里大鱼.net core 发送短信
  • 程序员入错行怎么办?
  • 两张超级大表join优化
  • 第九天函数
  • Linux软件安装-----apache安装
  • HDU 5988 最小费用流
  • Sorenson Capital:值得投资的 5 种 AI 技术
  • 《看透springmvc源码分析与实践》读书笔记一
  • 正式开课!如何学习相机模型与标定?(单目+双目+鱼眼+深度相机)
  • Arm芯片的新革命在缓缓上演
  • nagios自写插件—check_file
  • python3 错误 Max retries exceeded with url 解决方法
  • 行为模式之Template Method模式
  • 通过Spark进行ALS离线和Stream实时推荐