Gzip Json Data

Compression Steps

  1. Create json array for payment request records e.g. [{"SiteCode": "EXA-SIT-001", "Amount": 5.21, "TransactionReference": "ID001"},{"SiteCode": "EXA-SIT-001", "Amount": 1050, "TransactionReference": "ID002"}]
  2. Convert json string to UTF8 byte array
  3. Compress byte array using Gzip format
  4. Convert compressed byte array to a Base64 string

Decompression Steps

  1. Convert Base64 string to a byte array
  2. Decompress bytes using Gzip format
  3. Convert bytes to UTF8 encoded string
  4. Result should be json string with request fields and results e.g. [{"SiteCode": "EXA-SIT-001", "Amount": 5.21, "TransactionReference": "ID001", "PaymentLink": "https://p.ozow.com/123456", "Errors": "", "SentUTC": null},{"SiteCode": "EXA-SIT-001", "Amount": 1050, "TransactionReference": "ID002", "PaymentLink": "https://p.ozow.com/789012", "Errors": "Invalid mobile number", "SentUTC": null},{"SiteCode": "EXA-SIT-001", "Amount": 175.20, "TransactionReference": "ID003", "RecipientAddress": "0831231234", "PaymentLink": "https://p.ozow.com/456987", "Errors": null, "SentUTC": "2020-07-20 10:01:52"}]

Snippets Compression/Decompression

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

public class Program
{
	static void Main(string[] args)
	{
		string originalString = "Hello, world!";
		Console.WriteLine($"Original: {originalString}");
		string zipped = Zip(originalString);
		Console.WriteLine($"Zipped: {zipped}");
		string unzipped = Unzip(zipped);
		Console.WriteLine($"Unzipped: {unzipped}");
	}

	public static string Unzip(string inputString)
	{
		var gZipBuffer = Convert.FromBase64String(inputString);
		using var memoryStream = new MemoryStream();
		var dataLength = BitConverter.ToInt32(gZipBuffer, 0);
		memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
		var buffer = new byte[dataLength];
		memoryStream.Position = 0;
		using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
		{
			int totalRead = 0, bytesRead;
			while ((bytesRead = gZipStream.Read(buffer, totalRead, buffer.Length - totalRead)) > 0)
			{
				totalRead += bytesRead;
			}
		}

		return Encoding.UTF8.GetString(buffer);
	}

	public static string Zip(string inputString)
	{
		var buffer = Encoding.UTF8.GetBytes(inputString);
		var memoryStream = new MemoryStream();
		using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
		{
			gZipStream.Write(buffer, 0, buffer.Length);
		}

		memoryStream.Position = 0;
		var compressedData = new byte[memoryStream.Length];
		memoryStream.Read(compressedData, 0, compressedData.Length);
		var gZipBuffer = new byte[compressedData.Length + 4];
		Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
		Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
		return Convert.ToBase64String(gZipBuffer);
	}
}

//Result:
//Original: Hello, world!
//Zipped: DQAAAB+LCAAAAAAAAAPzSM3JyddRKM8vyklRBADmxubrDQAAAA==
//Unzipped: Hello, world!
<?php

function unzip($inputString) {
    $gZipBuffer = base64_decode($inputString);
    $dataLength = unpack("V", substr($gZipBuffer, 0, 4))[1];
    $memoryStream = substr($gZipBuffer, 4);

    $buffer = gzdecode($memoryStream);

    if ($buffer === false) {
        return false; // Decompression failed
    }

    return $buffer;
}

function zip($inputString) {
    $buffer = $inputString;
    $compressedData = gzencode($buffer);

    if ($compressedData === false) {
        return false; // Compression failed
    }

    $dataLength = pack("V", strlen($buffer));
    $gZipBuffer = $dataLength . $compressedData;

    return base64_encode($gZipBuffer);
}

// Test the functions
$originalString = "Hello, world!";
echo "Original: $originalString\n";

$zipped = zip($originalString);
echo "Zipped: $zipped\n";

$unzipped = unzip($zipped);
echo "Unzipped: $unzipped\n";
?>

//Result:
//Original: Hello, world!
//Zipped: DQAAAB+LCAAAAAAAAAPzSM3JyddRKM8vyklRBADmxubrDQAAAA==
//Unzipped: Hello, world!