title: Add multiple files from buffers to ZIP archive using libzip
body: <p>I'm trying to use libzip in a program that needs to archive several data chunks in different files. At the moment I have a code similar to the following snippet, edited from <a href="https://github.com/aseprite/libzip/blob/master/examples/in-memory.c" rel="nofollow noreferrer"><code>in-memory.c</code></a> example in libzip examples.</p>
  <p>The zip file is correctly saved with the files inside, but each file contains garbage.
  Any help is appreciated.</p>
  <pre class="lang-c++ prettyprint-override"><code>bool push_files(zip_t* za) {
    for (int i = 0; i &lt; 10; i++) {
      // Generate data
      std::stringstream ss;
      ss &lt;&lt; &quot;Test file #&quot; &lt;&lt; i;
      std::string a = ss.str();
  
      zip_source_t* source = zip_source_buffer(za, a.c_str(), a.size(), 0);
  
      if (source == NULL) {
        std::cerr &lt;&lt; &quot;error creating source: &quot; &lt;&lt; zip_strerror(za) &lt;&lt; std::endl;
        return false;
      }
  
      // Add buffer with filename
      std::stringstream fname;
      fname &lt;&lt; &quot;TEST-&quot; &lt;&lt; i;
      a = fname.str();
  
      if (zip_file_add(za, a.c_str(), source, ZIP_FL_ENC_UTF_8) &lt; 0) {
        std::cerr &lt;&lt; &quot;error adding source: &quot; &lt;&lt; zip_strerror(za) &lt;&lt; std::endl;
        return false;
      }
    }
    return true;
  }
  
  int main() {
    zip_source_t* src;
    zip_error_t error;
    zip_t* za;
  
    zip_error_init(&amp;error);
  
    if ((src = zip_source_buffer_create(NULL, 0, 1, &amp;error)) == NULL) {
      std::cerr &lt;&lt; &quot;can't create source: &quot; &lt;&lt; zip_error_strerror(&amp;error) &lt;&lt; std::endl;
      zip_error_fini(&amp;error);
      return 1;
    }
  
    if ((za = zip_open_from_source(src, ZIP_TRUNCATE, &amp;error)) == NULL) {
      std::cerr &lt;&lt; &quot;can't open zip from source: &quot; &lt;&lt; zip_error_strerror(&amp;error) &lt;&lt; std::endl;
      zip_source_free(src);
      zip_error_fini(&amp;error);
      return 1;
    }
  
    zip_error_fini(&amp;error);
  
    zip_source_keep(src);
  
    if (!push_files(za))
      return -1;
  
    if (zip_close(za) &lt; 0) {
      std::cerr &lt;&lt; &quot;can't close zip archive&quot; &lt;&lt; zip_strerror(za) &lt;&lt; std::endl;
      return 1;
    }
  
    // ... omissis, save archive to file as in in-memory.c
  }
  </code></pre>