Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating files in memory then pushing them into a zip archive using 7-zip commandline?

Tags:

c++

stream

zip

7zip

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?

like image 933
meds Avatar asked Dec 14 '25 16:12

meds


1 Answers

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;
}
like image 196
Totonga Avatar answered Dec 17 '25 21:12

Totonga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!