【问题标题】:C zip library that can create password protected zip files on windows?可以在 Windows 上创建受密码保护的 zip 文件的 C zip 库?
【发布时间】:2011-06-13 12:35:19
【问题描述】:

有人知道可以在 Windows 上创建受密码保护的 zip 文件的 C 库吗?使用内置 zip 实用程序对 zip 文件进行密码保护的选项似乎已从 Windows 7 中删除,但我认为这不是问题。

zziplib 或 7-Zip SDK 可以做到这一点吗?

【问题讨论】:

    标签: c++ c windows windows-7 zip


    【解决方案1】:

    7-Zip SDK (LZMA SDK) 支持受密码保护的存档。

    相关 SO 帖子:
    https://stackoverflow.com/questions/221049/how-secure-is-7-zip

    LZMA SDK:
    http://www.7-zip.org/sdk.html

    【讨论】:

    • LZMA SDK 看起来可以工作了……可惜没有二进制发行版。
    • LZMA SDK 最终使用起来很痛苦,而且我找不到密码保护档案的功能。然而,这个小库是小菜一碟! codeproject.com/KB/files/zip_utils.aspx
    • 但是,它创建的受密码保护的 zip 文件在 Windows 7 上不起作用...当您尝试打开它们时,Windows 7 声称它们无效。
    • @freedrull 好吧.. 我打算自己尝试一下,但我不知道 Windows 支持受密码保护的 zip 文件。嗯..我现在测试了。当我打开并解压缩由 7Zip 制作的受密码保护的 zip 文件时,Windows 资源管理器报告错误 0x80004005 (Win7)。我认为我们应该使用 7Zip 来解压受密码保护的 zip 文件。
    【解决方案2】:

    如果您可以使用 .NET,请查看 DotNetZip:http://dotnetzip.codeplex.com/

    创建受密码保护的 ZIP 的 C++ .NET 示例: http://cheeso.members.winisp.net/DotNetZipHelp/Code%20Examples/Cpp.htm

    【讨论】:

      【解决方案3】:

      MINIZIP + zlib 支持 AES 256 加密,非常好用!

      基于 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-25
        • 1970-01-01
        • 1970-01-01
        • 2010-11-09
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多