title: Can you help with creating a zip archive with the LZMA (7zip) SDK?
body: <p>I am trying to use the <a href="http://www.7-zip.org/sdk.html" rel="nofollow noreferrer">LZMA SDK</a> to create a zip archive (either .zip or .7z format). I've downloaded and built the SDK and I just want to use the dll exports to compress or decompress a few files. When I use the LzamCompress method, it returns 0 (SZ_OK) as if it worked correctly. However, after I write the buffer to file and try to open it, I get an error that the file cannot be opened as an archive.</p>
  
  <p>Here is the code I am currently using. Any suggestions would be appreciated.</p>
  
  <pre><code>#include "lzmalib.h"
  
  typedef unsigned char byte;
  
  using namespace std;
  
  int main()
  {
      int length = 0;
      char *inBuffer;
  
      byte *outBuffer = 0;
      size_t outSize;
      size_t outPropsSize = 5;
      byte * outProps = new byte[outPropsSize];
  
      fstream in;
      fstream out;
  
      in.open("c:\\temp\\test.exe", ios::in | ios::binary);
  
      in.seekg(0, ios::end);
      length = in.tellg();
      in.seekg(0, ios::beg);
  
      inBuffer = new char[length];
  
      outSize = (size_t) length / 20 * 21 + ( 1 &lt;&lt; 16 ); //allocate 105% of file size for destination buffer
  
      if(outSize != 0)
      {
          outBuffer = (byte*)malloc((size_t)outSize);
          if(outBuffer == 0)
          {
              cout &lt;&lt; "can't allocate output buffer" &lt;&lt; endl;
              exit(1);
          }
      }
  
      in.read(inBuffer, length);
      in.close();
  
      int ret = LzmaCompress(
          outBuffer, /* output buffer */
          &amp;outSize, /* output buffer size */
          reinterpret_cast&lt;byte*&gt;(inBuffer),/* input buffer */
          length, /* input buffer size */
          outProps, /* archive properties out buffer */
          &amp;outPropsSize,/* archive properties out buffer size */
          5, /* compression level, 5 is default */
          1&lt;&lt;24,/* dictionary size, 16MB is default */
          -1, -1, -1, -1, -1/* -1 means use default options for remaining arguments */
      );
  
      if(ret != SZ_OK)
      {
          cout &lt;&lt; "There was an error creating the archive." &lt;&lt; endl;
          exit(1);
      }
  
      out.open("test.zip", ios::out | ios::binary);
  
      out.write(reinterpret_cast&lt;char*&gt;(outBuffer), (int)(outSize));
      out.close();
  
      delete inBuffer;
      delete outBuffer;
  }
  </code></pre>