#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 < 2 )
{
printf( "usage:\n%s {file to unzip}\n", argv[ 0 ] );
return -1;
} // Open the zip file
unzFile *zipfile = unzOpen( argv[ 1 ] );
if ( zipfile == NULL )
{
printf( "%s: not found\n" );
return -1;
} // 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 -1;
} // Buffer to hold data read from the zip file.
char read_buffer[ READ_SIZE ]; // Loop to extract all files
uLong i;
for ( i = 0; 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, 0, NULL, 0 ) != UNZ_OK )
{
printf( "could not read file info\n" );
unzClose( zipfile );
return -1;
} // Check if this entry is a directory or file.
const size_t filename_length = strlen( filename );
if ( filename[ filename_length-1 ] == 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 -1;
} // 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 -1;
} int error = UNZ_OK;
do
{
error = unzReadCurrentFile( zipfile, read_buffer, READ_SIZE );
if ( error < 0 )
{
printf( "error %d\n", error );
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return -1;
} // Write data to file.
if ( error > 0 )
{
fwrite( read_buffer, error, 1, out ); // You should check return of fwrite...
}
} while ( error > 0 ); fclose( out );
} unzCloseCurrentFile( zipfile ); // Go the the next entry listed in the zip file.
if ( ( i+1 ) < global_info.number_entry )
{
if ( unzGoToNextFile( zipfile ) != UNZ_OK )
{
printf( "cound not read next file\n" );
unzClose( zipfile );
return -1;
}
}
} unzClose( zipfile ); return 0;
}

contrib/minizip/$ gcc -I../.. -o unzip uzip.c unzip.c ioapi.c ../../libz.a
contrib/minizip/$ ./unzip.exe /j/zlib-125.zip

最新文章

  1. Linux系统资源查看
  2. 简单介绍一下R中的几种统计分布及常用模型
  3. 安装.Net Framework3.5
  4. Linux(Ubuntu 14.0)
  5. Silverlight TreeView 动态绑定Xml 文件
  6. JS 点击事件学习总结
  7. 用c++库函数轻松解决回文问题
  8. shop++ 安装
  9. 精妙SQL语句 基础
  10. 借贷宝注册送现金疯转 新闻PS图背后真相
  11. VB.NET 结构(Structure)和类(Class)的区别
  12. 链表的实现 -- 数据结构与算法的javascript描述 第六章
  13. RIO包 健壮的I/O函数代码
  14. 五、oracle基本建表语句
  15. c# 工具类(字符串和时间,文件)
  16. elk的一些零碎知识
  17. Python3学习笔记33-正则表达式
  18. Specified version of key is not available (44)
  19. LeetCode(42):接雨水
  20. asp.net几个重要对象

热门文章

  1. 动词 + to do、动词 + doing
  2. android 指定时间加一个小时算法
  3. maven 配置Project Facets时further configuration available不出来问题
  4. 关于db2的一点记录
  5. swift项目第六天:中间发布按钮的封装以及监听点击事件
  6. js读取json,纠结。。。
  7. [疯狂Java]JDBC:事务管理、中间点、批量更新
  8. ConcurrentHashMap 内部实现分析
  9. Windows环境搭建Web自己主动化測试框架Watir(基于Ruby)
  10. Mac OSX 下配置 LNMP开发环境