转载:https://www.topomel.com/archives/979.html

一、minizip

是一套用来压缩和解压文件的工具,其基于zlib开源代码库。

开源代码下载链接:http://www.winimage.com/zLibDll/minizip.html

二、压缩文件

使用minizip压缩文件,我们一般使用如下的几个函数:
zipOpen2_64:新建一个空的Zip文件
zipOpenNewFileInZip3_64:添加一个文件到之前创建好的Zip文件中
zipWriteInFileInZip:读取文件内容并写入到Zip文件
zipCloseFileInZip:关闭当前Zip文件
zipClose: 关闭Zip文件

这里有几个地方需要注意:
1) 在调用zipOpenNewFileInZip3_64在Zip文件中添加文件之前,我们需要计算文件的CRC效验码和文件的时间戳信息,否则,会出现生成的Zip文件解压时头部错误的问题。
2) 读取文件并写入Zip文件过程中,我们需要使用循环的方式,每次对文件读取一定的字节,并注意检查是否到达文件尾(EOF),同时需要检查zipWriteInFileInZip调用是否失败。

伪代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
新建Zip文件(zipOpen2_64)
获取源文件时间戳信息(filetime)
在Zip中创建新文件(zipOpenNewFileInZip3_64)
打开源文件(fopen)
do
{
    读取源文件一定字节(fread)
    写入到Zip文件(zipWriteInFileInZip)
} while (源文件未到达EOF)
关闭源文件(fclose)
关闭Zip当前文件(zipCloseFileInZip)
关闭Zip文件(zipClose)

解压Zip文件
在解压一个Zip文件时,我们需要使用的函数如下:
unzOpen64:打开Zip文件
unzGetGlobalInfo64:获取Zip文件的全局信息
unzGetCurrentFileInfo64:获取当前文件信息
unzOpenCurrentFile:打开Zip中的文件
unzReadCurrentFile:读取文件内容
unzCloseCurrentFile:关闭当前文件
unzGoToNextFile:准备读取Zip文件中的下一个文件
unzClose:关闭Zip文件

伪代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
打开Zip文件(unzOpen64)
获取全局信息(unzGetGlobalInfo64)
for (从全局信息中获取所有Zip文件并遍历)
    获取当前文件信息(unzGetCurrentFileInfo64)
    if (当前Zip文件是目录)
        创建目录
    else
        打开Zip文件(unzOpenCurrentFile)
        打开目标文件(fopen)
        while (Zip文件读取成功)
            读取Zip文件(unzReadCurrentFile)
            写入目标文件(fwrite)
        关闭目标文件(fclose)
    关闭当前Zip文件(unzCloseCurrentFile)
    准备处理下一个Zip文件(unzGoToNextFile)
关闭Zip文件(unzClose)

代码例子:

#include <stdio.h>
#include <string.h> #include "unzip.h" #define dir_delimter '/'
#define MAX_FILENAME 512
#define READ_SIZE 8192 int main( int argc, char **argv )
{
if ( argc < )
{
printf( "usage:\n%s {file to unzip}\n", argv[ ] );
return -;
} // Open the zip file
unzFile *zipfile = unzOpen( argv[ ] );
if ( zipfile == NULL )
{
printf( "%s: not found\n" );
return -;
} // Get info about the zip file
unz_global_info global_info;
if ( unzGetGlobalInfo( zipfile, &global_info ) != UNZ_OK )
{
printf( "could not read file global info\n" );
unzClose( zipfile );
return -;
} // Buffer to hold data read from the zip file.
char read_buffer[ READ_SIZE ]; // Loop to extract all files
uLong i;
for ( i = ; i < global_info.number_entry; ++i )
{
// Get info about current file.
unz_file_info file_info;
char filename[ MAX_FILENAME ];
if ( unzGetCurrentFileInfo(
zipfile,
&file_info,
filename,
MAX_FILENAME,
NULL, , NULL, ) != UNZ_OK )
{
printf( "could not read file info\n" );
unzClose( zipfile );
return -;
} // Check if this entry is a directory or file.
const size_t filename_length = strlen( filename );
if ( filename[ filename_length- ] == dir_delimter )
{
// Entry is a directory, so create it.
printf( "dir:%s\n", filename );
mkdir( filename );
}
else
{
// Entry is a file, so extract it.
printf( "file:%s\n", filename );
if ( unzOpenCurrentFile( zipfile ) != UNZ_OK )
{
printf( "could not open file\n" );
unzClose( zipfile );
return -;
} // Open a file to write out the data.
FILE *out = fopen( filename, "wb" );
if ( out == NULL )
{
printf( "could not open destination file\n" );
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return -;
} int error = UNZ_OK;
do
{
error = unzReadCurrentFile( zipfile, read_buffer, READ_SIZE );
if ( error < )
{
printf( "error %d\n", error );
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return -;
} // Write data to file.
if ( error > )
{
fwrite( read_buffer, error, , out ); // You should check return of fwrite...
}
} while ( error > ); fclose( out );
} unzCloseCurrentFile( zipfile ); // Go the the next entry listed in the zip file.
if ( ( i+ ) < global_info.number_entry )
{
if ( unzGoToNextFile( zipfile ) != UNZ_OK )
{
printf( "cound not read next file\n" );
unzClose( zipfile );
return -;
}
}
} unzClose( zipfile ); return ;
}

minizip -基于zlib开源代码库的更多相关文章

  1. iOS流行的开源代码库

    本文介绍一些流行的iOS的开源代码库 1.AFNetworking 更新频率高的轻量级的第三方网络库,基于NSURL和NSOperation,支持iOS和OSX.https://github.com/ ...

  2. Element没更新了?Element没更新,基于El的扩展库更新

    think-vuele 基于Vue和ElementUI框架进行整合二次开发的一个框架.提供一些elementUI没有的或当时没有的控件.优化了或简化了便于2B软件开发的一些控件 demo:http:/ ...

  3. 【奔走相告】- Github送福利:用户可免费创建私有代码库啦

    最新消息 PingWest品玩1月8日讯,据TheNextWeb消息,据美国科技媒体The Next Web报道,被微软收购的代码平台GitHub最近调整政策,用户免费创建无限空间私有代码库(priv ...

  4. 【转】使用minizip解压缩多个文件(基于zlib)

    原帖子:使用minizip解压缩多个文件(基于zlib) 写作目的:之前在网上看到很多人在寻找可以解压缩多个文件的程序,其中有尝试zlib的,使用zlib的源码可以生成后缀为点gz的压缩文件,但是一次 ...

  5. Android6.0运行时权限(基于RxPermission开源库)

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 在6.0以前的系统,都是权限一刀切的处理方式,只要用户安装,Manifest申请的权限都会被赋予,并且安装后权限也撤销不了. And ...

  6. 基于第三方开源库的OPC服务器开发指南(3)——OPC客户端

    本篇将讲解如何编写一个OPC客户端程序测试我们在前文<基于第三方开源库的OPC服务器开发指南(2)——LightOPC的编译及部署>一篇建立的服务器.本指南的目的是熟悉OPC服务器的开发流 ...

  7. 基于第三方开源库的OPC服务器开发指南(2)——LightOPC的编译及部署

    前文已经说过,OPC基于微软的DCOM技术,所以开发OPC服务器我们要做的事情就是开发一个基于DCOM的EXE文件.一个代理/存根文件,然后就是写一个OPC客户端测试一下我们的服务器了.对于第一项工作 ...

  8. CodeGuide 300+文档、100+代码库,一个指导程序员写代码的,Github 仓库开源啦!

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.路怎样走,让你们自己挑 B站 视频:https://www.bilibili.com/vi ...

  9. Git---初入开源代码管理库的学习过程003

    Git常用命令总结 上接<Git 初入开源代码管理库的学习过程>学了一周Git,基本有了个认识.每一位比我厉害的,都是大牛,网上找了几篇博客和教材(感谢你们),边学习边实践用了四天,写笔记 ...

随机推荐

  1. string的深入理解

    本文只是个人总结见解,勿喷 首先肯定的是string是引用类型 string s_a = "yhc"; string s_b = s_a; if(s_a.Equals(s_b)) ...

  2. Golang简单写文件操作的四种方法

    package main import ( "bufio" //缓存IO "fmt" "io" "io/ioutil" ...

  3. 【HDU4970】Killing Monsters

    题意 数轴上有n个点,有m座炮塔,每个炮塔有一个攻击范围和伤害,有k个怪物,给出他们的初始位置和血量,问最后有多少怪物能活着到达n点.n<=100000 分析 对于某个怪物,什么情况下它可以活着 ...

  4. Python程序调试-TabError: inconsistent use of tabs and spaces in indentation

    报错信息:TabError: inconsistent use of tabs and spaces in indentation 说明:代码缩进统一使用Tab键或空格键,不能混用. 解决办法: 1. ...

  5. ubuntu 16.04 ARM glog移植

    1. 下载源文件https://github.com/google/glog 2. 源文件有CMakeLists.txt, 直接使用toolchain.cmake 直接编译就可以了,详情参考我的随笔  ...

  6. 模板模式和Comparable类

    模板模式中,父类规定好了一些算法的流程,并且空出一些步骤(方法)留给子类填充 Java的数组类中静态方法sort()就是一个模板,它空出了一个compareTo的方法,留给子类填充,用来规定什么是大于 ...

  7. Apache fcgistarter命令

    一.简介 fcgistarter命令用于启动FastCGI程序. 二.语法 fcgistarter -c command -p port [ -i interface ] -N num 参考:http ...

  8. pandas dataframe 满足条件的样本提取

    pandas 的dataframe 对 数据查询可以通过3种方式 . 预备知识: 1. pandas 的索引和label都是从0开始的计数的 2. 时间切片都是左闭右开的. [5:6,:]  只会输出 ...

  9. SpringMVC——<mvc:annotation-driven/>

    会自动注 册RequestMappingHandlerMapping .RequestMappingHandlerAdapter 与 ExceptionHandlerExceptionResolver ...

  10. 如何处理与开发有争议的Bug?

     工作中,测试人员有时会遇到类似的问题:提交了一份软件缺陷报告,可由于某种原因,无论是开发人员还是开发经理就是不愿修改程序.应如何处理这类问题呢?我认为,当对报告出现分歧意见后,测试工程师应首先做如下 ...