title: Create zip from inline binary attachment to multi-part message
body: <p>As part of the eBay API bulk upload methods we receive a multi-part response from eBay (supposedly) containing the raw data of a zip file containing an XML file. We're having issues converting this from its raw binary form into the zip file. <a href="http://developer.ebay.com/DevZone/file-transfer/CallRef/Samples/downloadFile_basic_out_xml.txt" rel="nofollow noreferrer">This is an example</a> of the ebay response with the zip/xml document at the bottom of the multi-part message.</p>
  
  <p>This is some quick (and dirty) PHP we've been using to test the response:</p>
  
  <pre><code>$fpath = "http://developer.ebay.com/DevZone/file-transfer/CallRef/Samples/downloadFile_basic_out_xml.txt";
  $responseXml = file_get_contents($fpath);
  $endofxmlstring = "&lt;/downloadFileResponse&gt;";
  $pos = strpos($responseXml, $endofxmlstring) + 1; //plus one to catch the final return
  $zipbuffer = substr($responseXml, $pos + strlen($endofxmlstring));
  unset($responseXml);
  
  $startofzipstring = "Content-ID:";
  $pos = strpos($zipbuffer, $startofzipstring);
  $zipbuffer = substr($zipbuffer, $pos);
  
  $startofzipstring = "PK";
  $pos = strpos($zipbuffer, $startofzipstring);
  $zipbuffer = substr($zipbuffer, $pos);
  
  $handler = fopen("response.zip", 'wb') or die("Failed. Cannot Open file to Write!");
  fwrite($handler,$zipbuffer);
  fclose($handler);
  </code></pre>
  
  <p>The zip file is created, but it is corrupt. The content passed to the zip file in <code>$zipbuffer</code> appears to be the correct code (in as much as it is identical to the code at the bottom of the response content) so i'm not sure whats going on.</p>
  
  <p>The ebay docs <a href="http://developer.ebay.com/DevZone/file-transfer/CallRef/downloadFile.html" rel="nofollow noreferrer">here</a> describe what gets returned here:</p>
  
  <blockquote>
    <p>The output sample shows the raw format of the download file response
    to illustrate how the data file is attached in the multi-part message.
    The root part (or body) contains the call response with the standard
    output fields, such as ack, timestamp, and version. The final part
    contains the compressed file attachment in base64binary format. The
    file attachment stream is referenced by content ID (i.e., cid) in the
    Data field of the body. When the ack value is "Success," the binary
    data of the file attachment must be saved as a zip file. The
    SoldReport XML file must, in turn, be extracted from the zip file.</p>
  </blockquote>
  
  <p>It mentions the returned content is "base64binary" but what actually is this? It's certainly not a base64 string that i've worked with before. </p>