title: GZipStream in PHP
body: <p>I am converting c# application into php. In that application GZipStream is used but i dont know how to compress and decompress same thing in php.</p>
  
  <p>I am able to compress and zip using php but the problem is, zip file of php and zip file of c# both are different. I want exactly same output like c#.<br>
  Both my C# and php code is working but output of both is different. It should be the same.</p>
  
  <p><strong>C# Code:</strong></p>
  
  <pre><code>public static bool CompressDirectory(string sInDir, string sOutFile)
          {
              try
              {
                  string[] sFiles = Directory.GetFiles(sInDir, "*.*", SearchOption.AllDirectories);
                  int iDirLen = sInDir[sInDir.Length - 1] == Path.DirectorySeparatorChar ? sInDir.Length : sInDir.Length + 1;
  
                  using (FileStream outFile = new FileStream(sOutFile, FileMode.Create, FileAccess.Write, FileShare.None))
                  using (GZipStream str = new GZipStream(outFile, CompressionMode.Compress))
                      foreach (string sFilePath in sFiles)
                      {
                          string sRelativePath = sFilePath.Substring(iDirLen);
                          //if (progress != null)
                          //    progress(sRelativePath);
                          CompressFile(sInDir, sRelativePath, str);
                      }
              }
              catch (System.Exception ex)
              {
                  PharmaRackMargSynchronizerLog.WriteEntry("CompressDirectory: " + ex.Message, EventLogEntryType.Error);
                  return false;
              }
              return true;
          }
  
  static void CompressFile(string sDir, string sRelativePath, GZipStream zipStream)
          {
              try
              {
                  //Compress file name
                  char[] chars = sRelativePath.ToCharArray();
                  zipStream.Write(BitConverter.GetBytes(chars.Length), 0, sizeof(int));
                  foreach (char c in chars)
                      zipStream.Write(BitConverter.GetBytes(c), 0, sizeof(char));
  
                  //Compress file content
                  byte[] bytes = File.ReadAllBytes(Path.Combine(sDir, sRelativePath));
                  zipStream.Write(BitConverter.GetBytes(bytes.Length), 0, sizeof(int));
                  zipStream.Write(bytes, 0, bytes.Length);
              }
              catch (System.Exception ex)
              {
                  PharmaRackMargSynchronizerLog.WriteEntry("CompressFile: " + ex.Message, EventLogEntryType.Error);
              }
          }
  </code></pre>
  
  <p>What I tried in PHP to so same thing, I have used <strong>gzcompress</strong> function for that then after compressing file content i am using <strong>ZipArchive</strong> to zip file. I am able to create encoding and zip both here but output of php's zip and output of C#'s zip are different. i want it to be same.</p>
  
  <p><strong>PHP Code:</strong></p>
  
  <pre><code>// create zip file
  
  $cfilename = REPORTPATH . $_SERVER['HTTP_DISTRIBUTORAPIKEY'] . "\\" . $_SERVER['HTTP_DISTRIBUTORAPIKEY'] . "_Firms.csv";
  $zipname = date('m-d-Y_H_i_s') . '.zip';
  $zip = new ZipArchive();
  $zip-&gt;open($_SERVER['DOCUMENT_ROOT'] . "/api.pharmarack.com/distributors/" . $_SERVER['HTTP_DISTRIBUTORAPIKEY'] . "/" . $zipname, ZipArchive::CREATE);
  
  // gzcompress compressiong process
  
  $compressedstring = gzcompress(base64_encode(file_get_contents($cfilename)) , 9);
  
  //                    $uncompressed = gzuncompress($compressedstring);
  //                    echo $uncompressed;die;
  
  $zip-&gt;addFromString($cfilename, $compressedstring);
  
  // $zip-&gt;addFile($cfilename);
  
  $zip-&gt;close();
  
  // End of Create Zip File
  </code></pre>