【问题标题】:C zip library for opening password protected files用于打开受密码保护的文件的 C zip 库
【发布时间】:2013-04-28 19:05:49
【问题描述】:

我正在尝试使用 zlib 和 minizip。当我在一个包含在存档中的解决方案中构建 6 个项目时,我下载了一切正常,项目将创建 exe 文件(minizip 和 miniunz)。这是问题所在,我不知道如何在我的应用程序中使用 miniunz 和 minizip 源代码,谷歌也没有帮助。有使用这些库经验的人能否提供分步教程如何将这些库包含在我的应用程序中?

或者,如果您有其他库可以处理受密码保护的文件,并且可以提供一些教程如何将其包含在项目中也会有所帮助,我试图找到一些东西,但没有教程如何将它们安装到项目中

谢谢

【问题讨论】:

  • 为什么不libzip?它有据可查。
  • 我以为它只适用于基于 unix 的系统

标签: c zip zlib


【解决方案1】:

基于 minizip unzip.c 的代码

包括stdio.h zip.h unzip.h

首先,创建一个 zip,其中包含一个带有密码的文件。 zip 文件必须与可执行文件位于同一目录中。 在生成的程序目录中从提示符处运行程序。 此示例仅提取第一个文件!

/*-----------start-------------- */
/*Tries to open the zip in the current directory.*/
unzFile zfile =  unzOpen("teste.zip");
if(zfile==NULL) 
{
    printf("Error!");
    return;
}

printf("OK Zip teste.zip opened...\n");


int err = unzGoToFirstFile(zfile);/*go to first file in zip*/
if (err != UNZ_OK)
{
    printf("error %d with zipfile in unzGoToFirstFile\n", err); 
    unzClose(zfile);/*close zip*/
}

/*At this point zfile points to the first file contained in the zip*/

char filename_inzip[256] = {0};/* The file name will be returned here */
unz_file_info file_info = {0};/*strcuture with info of the first file*/

err = unzGetCurrentFileInfo(zfile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
if (err != UNZ_OK)
{
    printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
}
else
{
    int len = 8192;/*size of chunk*/
    char buffer[8192]={0};/*buffer used to save uncompressed data*/
    printf("name of first file is :%s\n",filename_inzip);
    printf("uncompressed_size = %d\n",file_info.uncompressed_size);

    /*Use your password here, the same one used to create your zip */
    err = unzOpenCurrentFilePassword(zfile, "yourpassword");
    if (err != UNZ_OK)
        printf("error %d with zipfile in unzOpenCurrentFilePassword\n", err);
    else
        printf("password ok\n");

    FILE *fp = fopen(filename_inzip, "wb");/*file for data binary type*/
    if (fp != NULL) 
    {
        do
        {/*read the current file returned by unzGoToFirstFile to buffer in chunks of the 8192*/
            err = unzReadCurrentFile(zfile, &buffer, len );
            if (err < 0)
            {
                printf("error %d with zipfile in unzReadCurrentFile\n", err);
                break;
            }
            if (err == 0)
                break;
            /*Save the chunk read to the file*/
            if (fwrite(&buffer, err, 1, fp) != 1)/*if error break*/
            {
                printf("error %d in writing extracted file\n", errno);
                err = UNZ_ERRNO;
                break;
            }/*else continue*/
        }
        while (err > 0);
        /*close file*/
        fclose(fp);
    }
}

unzClose(zfile);

【讨论】:

    猜你喜欢
    • 2015-11-26
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多