在内存中创建文件,然后使用 7-zip 命令行将它们推送到 zip 存档中?

发布于 2024-12-26 12:17:02 字数 114 浏览 0 评论 0原文

我正在尝试用 C++ 动态创建 zip 文件,我正在使用 7-zip 来尝试完成此任务,特别是我想使用 7-zip 命令行(我愿意尝试并修改其源代码来完成这)。

这可能吗?如果可以,我应该如何去做?

I'm trying to create zip files on the fly in C++, I'm using 7-zip to try and accomplish this, specifically I want to use the 7-zip commandline (I'm willing to try and modify its source to accomplish this).

Is this possible and if so how should I go about doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海夕 2025-01-02 12:17:02

使用 minizip 和这样的代码应该可以在 Windows 上完成这项工作。

#define USEWIN32IOAPI
#include <zip.h>
#include <unzip.h>
#include <iowin32.h>

int toZip(const wchar_t* zipfile,  const char* nameInZip, void* buf, size_t buf_size) 
{
  zlib_filefunc64_def ffunc;
  fill_win32_filefunc64W(&ffunc);
  zipFile zf = zipOpen2_64(zipfile, APPEND_STATUS_CREATE, NULL,&ffunc);
  if(0 == zf)  {
     return -1;
  }

  zip_fileinfo zi;
  zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
    zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
  zi.dosDate = 0;
  zi.internal_fa = 0;
  zi.external_fa = 0;
  zi.dosDate = 0; // no date
  int zip64 = 1; // always zip64
  unsigned long crcFile=0;

  int opt_compress_level(Z_DEFAULT_COMPRESSION);
  char* password = 0;
  int err = zipOpenNewFileInZip3_64(zf,nameInZip,&zi,
    NULL,0,NULL,0,NULL,
    (opt_compress_level != 0) ? Z_DEFLATED : 0,
    opt_compress_level,0,
    -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
    password,crcFile, zip64);

  if (ZIP_OK == err) {

    err |= zipWriteInFileInZip (zf,buf,buf_size);
    err |= zipCloseFileInZip(zf);
  }

  err |= zipClose(zf,NULL)

  return err;
}

Using minizip with code like this should do the job on windows.

#define USEWIN32IOAPI
#include <zip.h>
#include <unzip.h>
#include <iowin32.h>

int toZip(const wchar_t* zipfile,  const char* nameInZip, void* buf, size_t buf_size) 
{
  zlib_filefunc64_def ffunc;
  fill_win32_filefunc64W(&ffunc);
  zipFile zf = zipOpen2_64(zipfile, APPEND_STATUS_CREATE, NULL,&ffunc);
  if(0 == zf)  {
     return -1;
  }

  zip_fileinfo zi;
  zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
    zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
  zi.dosDate = 0;
  zi.internal_fa = 0;
  zi.external_fa = 0;
  zi.dosDate = 0; // no date
  int zip64 = 1; // always zip64
  unsigned long crcFile=0;

  int opt_compress_level(Z_DEFAULT_COMPRESSION);
  char* password = 0;
  int err = zipOpenNewFileInZip3_64(zf,nameInZip,&zi,
    NULL,0,NULL,0,NULL,
    (opt_compress_level != 0) ? Z_DEFLATED : 0,
    opt_compress_level,0,
    -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
    password,crcFile, zip64);

  if (ZIP_OK == err) {

    err |= zipWriteInFileInZip (zf,buf,buf_size);
    err |= zipCloseFileInZip(zf);
  }

  err |= zipClose(zf,NULL)

  return err;
}
在你怀里撒娇 2025-01-02 12:17:02

7-zip 可以从 STDIN 进行压缩,因此您可以使用某些命令行参数启动 7z 进程,然后将数据放入其 STDIN 中。例如以下示例:

echo foo | 7z a arc.7z -sifoo.txt
echo bar | 7z a arc.7z -sibar.txt

将创建 arc.7z 文件,其中包含两个文件:foo.txtbar.txt (包含文本 foo分别是bar)。

当然,在您的程序中,您不需要调用 echo,只需使用管道或操作系统提供的其他方式7z 重定向 STDIN 即可。如果您需要添加多个文件,则必须多次启动 7z

7-zip can compress from STDIN, so you start 7z process with certain command line arguments, then put your data into its STDIN. For example following:

echo foo | 7z a arc.7z -sifoo.txt
echo bar | 7z a arc.7z -sibar.txt

will create arc.7z file with two files inside: foo.txt and bar.txt (containing text foo and bar respectively).

Of course from your program you don't need to call echo, only 7z redirecting STDIN with pipes or some other means your OS provides. And you'll have to start 7z several times if you need to add more than one file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文