使用zlib解压.zip文件的简单方法

48

有没有一个简单的例子可以演示如何解压缩.zip文件并将文件提取到目录中?我目前正在使用zlib,虽然我知道zlib不能直接处理zip文件,但在zlib的“contrib”库中似乎有几个其他的东西。我注意到并阅读了关于“minizip”的一些文档,看了一些代码后,我没有看到一个简单的例子来演示如何解压缩.zip文件并将文件提取到目录中。

我想找到一个平台无关的方法来做到这一点,但如果不可能的话,我需要找到一种适用于Windows和Mac的方法。


4
对于那些想知道如何使用zlib创建zip文件的读者,请参考我在这里的回答:https://dev59.com/dmgu5IYBdhLWcg3wYGC3 - niemiro
2个回答

60

zlib 处理 deflate 压缩/解压算法,但 ZIP 文件中还有更多内容。

你可以尝试使用 libzip。它是免费的、可移植的,且易于使用。

更新:这里附上一个快速而肮脏的 libzip 示例,省略了所有错误控制:

#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;
}

3
请问您能否提供一个使用libzip解压文件的简单示例? - judeclarke
经过几天在Windows上努力让libzip编译的斗争之后,结果发现您帖子中的代码无法编译。zip_stat是一个函数,而不像您所提到的那样是一种类型。 - judeclarke
1
@judeclarke - 实际上两者都是对的!在C语言中没有歧义,但在C++中有。只需将zip_stat st;更改为struct zip_stat st;即可。 - rodrigo
1
当我尝试在Ubuntu上使用g++编译它时,它会显示“undefined reference to functions”。 - aceBox
@aceBox:你在编译命令中加入了“-lzip”选项吗? - rodrigo

35

Minizip提供了示例程序来演示其用法 - 这些文件名为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;
}

我使用MinGW/MSYS在Windows上构建并测试它,步骤如下:

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

我之前研究过那个例子,那是一个非常复杂的例子。你能提供一个更简单的例子吗? - judeclarke
@judeclarke,我离开我的开发工作站一天了,但我会在能够时发布一些更小/更简单的内容! - x-x
6
您只需花时间阅读头文件和示例。首先查看zip.h和unzip.h,了解提供的函数及其功能。然后查看minizip.c和miniunz.c以了解它们的使用方法。 - Mark Adler
使用unzReadCurrentFile函数,无法在zip文件中同时打开两个“打开”文件吗? - paulm
unzFile已经是一个指针。必须按以下方式使用:unzFile zipfile = unzOpen(argv[1]); - Cem Polat
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接