2

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.

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#.
Both my C# and php code is working but output of both is different. It should be the same.

C# 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);
            }
        }

What I tried in PHP to so same thing, I have used gzcompress function for that then after compressing file content i am using ZipArchive 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.

PHP 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->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->addFromString($cfilename, $compressedstring);

// $zip->addFile($cfilename);

$zip->close();

// End of Create Zip File
Al.G.
  • 4,327
  • 6
  • 31
  • 56
Juned Ansari
  • 5,035
  • 7
  • 56
  • 89

1 Answers1

3

From gzcompress PHP manual: "Note: This is not the same as gzip compression, which includes some header data. See gzencode() for gzip compression. " .Source

You need instead, gzencode: " This function returns a compressed version of the input data compatible with the output of the gzip program.

The encoding mode. Can be FORCE_GZIP (the default) or FORCE_DEFLATE.

Prior to PHP 5.4.0, using FORCE_DEFLATE results in a standard zlib deflated string (inclusive zlib headers) after a gzip file header but without the trailing crc32 checksum.

In PHP 5.4.0 and later, FORCE_DEFLATE generates RFC 1950 compliant output, consisting of a zlib header, the deflated data, and an Adler checksum. " Source

Marco
  • 2,757
  • 1
  • 19
  • 24