When You run this query: select compress('this is just a sample string') (for instance using MSSQL Server Management Studio) You will see a text representation in hexadecimal format of the binary compressed text.
I did a test with a lorem ipsum long text casted as varchar and nvarchar as well (You may notice that inside that text there aren't double-byte characters, but it doesn't matter for this example):
This compressed long text is stored inside a varbinary(max) column inside MSSQL:
--------VARCHAR---------- --------NVARCHAR--------
Original Hex Binary Original Hex Binary
13046 4024 2011 13046 4748 2373
So, the best choice here is to transfer from the back-end the binary data, not the hexadecimal representation.
Tell the browser that the response is gzipped:
SERVER SIDE: Here is an example endpoint in PHP :
<?php /* getcompressed.php */
header("Content-Encoding: gzip");
header("Vary: Accept-Encoding");
header("Content-type: text/html; charset=UTF-16"); /* nvarchar */
$serverName = 'testserver';
$connectionOptions = array('Database'=>'testdatabase');
$conn = sqlsrv_connect($serverName, $connectionOptions);
$sql = 'SELECT compressed FROM tb_compression WHERE id = 1';
$qry = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($qry);
$compressed = $row[0];
sqlsrv_close($conn);
echo $compressed;
?>
CLIENT SIDE: here is a working snippet in JavaScript:
var request = new XMLHttpRequest();
request.onload = function(e) {
var text = e.currentTarget.responseText;
console.log(text); /* Here You go */
};
request.responseType = 'text';
request.open('GET', 'getcompressed.php', true);
request.send(null);
This works for me in Chrome and FF as well. Try it out.
Using a gunzip javaScript Library:
SERVER SIDE: Here is an example endpoint in PHP :
<?php /* getcompressed.php */
header('Content-Type: application/octet-stream');
$serverName = 'testserver';
$connectionOptions = array('Database'=>'testdatabase');
$conn = sqlsrv_connect($serverName, $connectionOptions);
$sql = 'SELECT compressed FROM tb_compression WHERE id = 1';
$qry = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($qry);
$compressed = $row[0];
sqlsrv_close($conn);
echo $compressed;
?>
CLIENT SIDE: here is a working snippet in JavaScript:
CREDIT: The library used here for the decompression is gunzip.min.js from Imaya Yuta.
var request = new XMLHttpRequest();
request.onload = function(e) {
var response = new Uint8Array(e.currentTarget.response);
console.log(response.byteLength);
/* Strip out the response termination */
var compressed = response.subarray(0, response.byteLength - 4);
var gunzip = new Zlib.Gunzip(compressed);
var decompressed = gunzip.decompress();
var encoding = 'utf-8'; /* For varchar text (ansi) */
//var encoding = 'utf-16'; /* For nvarchar text (double-byte) */
var text = new TextDecoder(encoding).decode(decompressed);
console.log(text); /* Here You go */
};
request.responseType = 'arraybuffer';
request.open('GET', 'getcompressed.php', true);
request.send(null);
If You look at the response.byteLength, for that example text of 13046 Bytes, You will check that there are effectively transmitted over the network just only 2015 Bytes.