在Windows上有哪些能够创建带密码保护的zip文件的C语言库?

6

有没有一个可以在Windows上创建密码保护的zip文件的C语言库?似乎Windows 7中使用内置的zip工具设置密码保护已被删除,但我认为这不是问题。

zziplib或7-Zip SDK可以实现吗?

3个回答

3

LZMA SDK看起来可以工作......只可惜没有二进制发行版。 - freedrull
LZMA SDK使用起来很难受,我找不到一个为压缩文件设置密码的函数。然而这个小库却非常容易使用!http://www.codeproject.com/KB/files/zip_utils.aspx - freedrull
然而,它创建的受密码保护的 zip 文件在 Windows 7 上无法使用......当您尝试打开它们时,Windows 7 声称它们无效。 - freedrull
@freedrull 嗯... 我计划自己尝试这个东西,但我不知道Windows支持有密码保护的zip文件。嗯... 我现在测试了一下。当我尝试打开和解压一个由7Zip创建的有密码保护的zip文件时,Windows资源管理器报告错误代码0x80004005(Win7)。我认为我们应该使用7Zip来解压有密码保护的zip文件。 - 9dan

1

0

MINIZIP + zlib支持AES 256加密,非常易于使用!

代码基于minizip unzip.c。 包括stdio.h zip.h unzip.h

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

/*-----------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);

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