C++ 使用zlib解压.zip文件的简单方法
有没有一个简单的例子说明如何解压.zip文件并将其解压缩到目录中?我目前正在使用zlib,虽然我知道zlib并不直接处理zip文件,但在zlibs的“contrib”库中似乎还有其他一些东西。我注意到并阅读了有关“minizip”的内容,在阅读了一些文档并查看了一些代码之后,我没有看到如何解压缩.zip文件并将文件解压缩到目录的简单示例C++ 使用zlib解压.zip文件的简单方法,c++,zip,zlib,unzip,C++,Zip,Zlib,Unzip,有没有一个简单的例子说明如何解压.zip文件并将其解压缩到目录中?我目前正在使用zlib,虽然我知道zlib并不直接处理zip文件,但在zlibs的“contrib”库中似乎还有其他一些东西。我注意到并阅读了有关“minizip”的内容,在阅读了一些文档并查看了一些代码之后,我没有看到如何解压缩.zip文件并将文件解压缩到目录的简单示例 我想找到一种独立于平台的方法来实现这一点,但如果这不可能,那么我需要为windows和mac找到一种方法。zlib处理deflate压缩/解压算法,但ZIP文件
我想找到一种独立于平台的方法来实现这一点,但如果这不可能,那么我需要为windows和mac找到一种方法。
zlib
处理deflate压缩/解压算法,但ZIP文件中的压缩/解压算法不止这些
你可以试试。它是免费的,便于携带和使用
更新:这里我附上libzip的quick'n'dirty示例,其中包含所有错误控件:
#include <zip.h>
int main()
{
//Open the ZIP archive
int err = 0;
zip *z = zip_open("foo.zip", 0, &err);
//Search for the file of given name
const char *name = "file.txt";
struct zip_stat st;
zip_stat_init(&st);
zip_stat(z, name, 0, &st);
//Alloc memory for its uncompressed contents
char *contents = new char[st.size];
//Read the compressed file
zip_file *f = zip_fopen(z, name, 0);
zip_fread(f, contents, st.size);
zip_fclose(f);
//And close the archive
zip_close(z);
//Do something with the contents
//delete allocated memory
delete[] contents;
}
#包括
int main()
{
//打开压缩文件
int err=0;
zip*z=zip_open(“foo.zip”,0,&err);
//搜索给定名称的文件
const char*name=“file.txt”;
结构zip_stat st;
zip_stat_init(&st);
zip_stat(z、name、0和st);
//其未压缩内容的Alloc内存
字符*内容=新字符[st.size];
//读取压缩文件
zip_file*f=zip_fopen(z,name,0);
zip_fread(f、目录、标准尺寸);
zip_fclose(f);
//然后关闭档案
zip_close(z);
//对内容做点什么
//删除分配的内存
删除[]项内容;
}
确实有一个示例程序来演示它的用法-这些文件称为minizip.c和miniunz.c
更新:我有几分钟的时间,所以我为大家快速制作了这个简单的例子。这是非常臭的C,如果没有重大改进,我不会使用它。希望这足以让你现在开始
// uzip.c - Simple example of using the minizip API.
// Do not use this code as is! It is educational only, and probably
// riddled with errors and leaks!
#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;
}
我之前研究过这个例子,这是一个巨大的例子。你能提供一个更简单的例子吗?@judeclarke,我今天不在我的开发箱里,但我会在可能的时候发布一些更小/更简单的东西!您只需要花时间阅读头文件和示例。首先看一下zip.h和unzip.h,看看提供了哪些函数以及它们的作用。然后看看minizip.c和miniunz.c,看看它们是如何使用的。使用unzReadCurrentFile,在zip中不可能有两个“打开”的文件吗?@paulm:只需创建两个unzfile实例,您可以提供一个简单的示例,说明如何使用libzip解压文件吗?没问题。查看更新后的答案。在与libzip进行了几天的斗争以使其能够在windows上编译之后,您的帖子中的代码最终无法编译。zip_stat是一个函数,而不是像您这样的类型mentioned@judeclarke-事实上两者都是!在C中没有歧义,但是在C++中有。只需更改
zip\u stat st代码>进入struct zip\u stat st
和done。当我尝试使用g++o ubuntuff编译它时,它给出了“未定义的函数引用”。对于正在寻找相反方法的读者,要使用zlib创建zip文件,请参见我的答案:
contrib/minizip/$ gcc -I../.. -o unzip uzip.c unzip.c ioapi.c ../../libz.a
contrib/minizip/$ ./unzip.exe /j/zlib-125.zip