GZip Functions - Compress and Decompress

The GZip functions provide compression and decompression capabilities for strings and byte arrays using the GZip format. These functions are essential for reducing data size during transmission, storing compressed data, and working with GZipped content from external systems.

Key Benefits:

  • Reduce bandwidth usage for data transmission
  • Minimize storage requirements for large text data
  • Compress API request/response payloads
  • Process GZipped files and archives
  • Optimize pipeline performance with large datasets

GZip.compress()

Description:

Compresses a string or byte array into GZip format.

Syntax:

GZip.compress(content)

Parameters:

  • content - A string or binary data (byte array) to compress

Returns: A GZipped byte array in SnapLogic binary format

Examples:

GZip.compress($content)
// Where $content = "Hello, World!"
// Returns: GZipped byte array (binary format)

GZip.compress($largeText)
// Compresses large text data

GZip.compress($jsonData)
// Where $jsonData = JSON.stringify($object)
// Returns: Compressed JSON data

GZip.decompress()

Description:

Decompresses a GZipped string or byte array.

Syntax:

GZip.decompress(content)

Parameters:

  • content - A GZipped string or binary data to decompress

Returns: An unzipped byte array

Examples:

GZip.decompress($content)
// Where $content is GZipped data
// Returns: Original uncompressed byte array

GZip.decompress($gzippedResponse)
// Decompress API response

GZip.decompress($compressedFile)
// Decompress file content

Common Use Cases

Round-Trip Compression/Decompression:

// Compress and then decompress
$original = "Test data for compression"
$compressed = GZip.compress($original)
$decompressed = GZip.decompress($compressed)
// $decompressed should equal $original

Compressing Large API Payloads:

// Compress JSON before sending to API
{
  "data": GZip.compress(JSON.stringify($largeObject)),
  "encoding": "gzip",
  "originalSize": $largeObject.toString().length
}

Decompressing API Response:

// Decompress GZipped API response
{
  "responseData": GZip.decompress($response.body),
  "compressed": false
}

File Compression:

// Compress file content before storage
{
  "filename": $fileName + ".gz",
  "content": GZip.compress($fileContent),
  "compressionRatio": $originalSize / $compressedSize
}

Batch Data Compression:

// Compress multiple records for efficient storage
$records.map(record => ({
  "id": record.id,
  "data": GZip.compress(JSON.stringify(record)),
  "timestamp": Date.now()
}))

Logging Large Payloads:

// Compress logs to save storage space
{
  "logLevel": "INFO",
  "timestamp": Date.now(),
  "message": GZip.compress($largeLogMessage),
  "compressed": true
}

Bandwidth Optimization:

// Compress data before network transmission
{
  "payload": GZip.compress($data),
  "contentEncoding": "gzip",
  "originalSize": $data.length,
  "transmissionTime": Date.now()
}

Working with Binary Data

GZip functions work with SnapLogic's binary data format. Compressed data is returned as a byte array with the _snaptype_binary_base64 wrapper.

Binary Format Structure:

{
  "_snaptype_binary_base64": "Base64EncodedGZippedData"
}

Complete Workflow Example:

// Step 1: Compress string to GZip binary
$compressed = GZip.compress("Large text data")
// Result: {"_snaptype_binary_base64": "..."}

// Step 2: Pass binary data through pipeline

// Step 3: Decompress back to original
$decompressed = GZip.decompress($compressed)
// Result: Byte array of original data

// Step 4: Convert to string if needed
$text = $decompressed.toString()

Integration with Base64 Functions

GZip functions are often used in combination with Base64 encoding for text-safe transmission:

Compress and Base64 Encode:

// Compress data, then Base64 encode for text transmission
Base64.encode(GZip.compress($data))
// Returns: Base64-encoded string of compressed data

Base64 Decode and Decompress:

// Decode Base64, then decompress GZip
GZip.decompress(Base64.decode($encodedCompressedData))
// Returns: Original uncompressed data

Complete Round-Trip:

// Full encode/decode cycle
$original = "Test data"
$encoded = Base64.encode(GZip.compress($original))
$decoded = GZip.decompress(Base64.decode($encoded))
// $decoded equals $original

Compression Ratio Analysis

Calculating Compression Ratio:

// Calculate how much compression was achieved
{
  "originalSize": $original.length,
  "compressedSize": GZip.compress($original).toString().length,
  "compressionRatio": ($original.length / GZip.compress($original).toString().length).toFixed(2),
  "spaceSaved": (1 - (GZip.compress($original).toString().length / $original.length)) * 100 + "%"
}

Conditional Compression:

// Only compress if data exceeds threshold
$data.length > 1024
  ? {
      "data": GZip.compress($data),
      "compressed": true
    }
  : {
      "data": $data,
      "compressed": false
    }

Best Practices

  • Large Data: Use compression for data larger than 1KB; smaller data may not benefit from compression.
  • Text vs Binary: GZip works best with text data; binary data like images may not compress well.
  • Network Transmission: Compress data before sending over network to reduce bandwidth usage.
  • Storage: Compress infrequently accessed data to save storage space.
  • Performance Trade-off: Compression adds CPU overhead; balance compression benefits against processing time.
  • Decompression: Always decompress GZipped data before processing unless the target system expects compressed data.
  • Error Handling: Implement error handling for decompression failures due to corrupted data.
  • Content Type: Set appropriate content-encoding headers when transmitting compressed data.
  • Round-Trip Testing: Test that compress/decompress operations preserve data integrity.
  • Repetitive Data: GZip compression is most effective on repetitive or structured text data.

Performance Considerations

When to Use Compression:

Scenario Recommendation Reason
Large text files (>10KB) Compress Significant bandwidth/storage savings
Small payloads (<1KB) Don't compress Overhead exceeds benefits
JSON/XML data Compress High compression ratios (60-80%)
Already compressed (images, videos) Don't compress No additional compression possible
Repeated API calls Compress Reduces cumulative bandwidth
Real-time data Evaluate Balance latency vs bandwidth

Practical Examples

Compressing Log Data:

// Compress verbose log entries
{
  "timestamp": Date.now(),
  "level": "DEBUG",
  "message": GZip.compress(JSON.stringify($detailedLog)),
  "compressed": true,
  "originalLength": JSON.stringify($detailedLog).length
}

HTTP Response Compression:

// Compress API response payload
{
  "statusCode": 200,
  "headers": {
    "Content-Encoding": "gzip",
    "Content-Type": "application/json"
  },
  "body": GZip.compress(JSON.stringify($responseData))
}

File Upload with Compression:

// Compress file before upload
{
  "filename": $originalFilename + ".gz",
  "content": GZip.compress($fileContent),
  "originalSize": $fileContent.length,
  "uploadTimestamp": Date.now()
}

Database Storage Optimization:

// Compress large text fields before database insert
{
  "id": $recordId,
  "summary": $summary,
  "fullContent": GZip.compress($fullContent),
  "compressed": true,
  "created": Date.now()
}

Batch Processing with Compression:

// Process and compress batch data
$batch.map(item => ({
  "id": item.id,
  "data": GZip.compress(JSON.stringify(item.data)),
  "processedAt": Date.now(),
  "compressionRatio": (item.data.toString().length /
    GZip.compress(JSON.stringify(item.data)).toString().length).toFixed(2)
}))

Decompressing External Data:

// Decompress data received from external system
{
  "source": $source,
  "decompressedData": GZip.decompress($gzippedInput),
  "processedAt": Date.now(),
  "status": "success"
}

Archive Creation:

// Create compressed archive of multiple files
{
  "archiveName": "data-archive-" + Date.now() + ".gz",
  "files": $files.map(file => ({
    "name": file.name,
    "compressed": GZip.compress(file.content),
    "originalSize": file.content.length
  })),
  "totalFiles": $files.length
}

Error Handling

Handling Decompression Errors:

// Safely decompress with error handling
try {
  $decompressed = GZip.decompress($compressedData)
} catch (e) {
  {
    "error": "Decompression failed",
    "message": e.message,
    "fallback": $compressedData
  }
}

Validating Compressed Data:

// Check if data is actually compressed before decompressing
$isCompressed = $data._snaptype_binary_base64 != null
$result = $isCompressed
  ? GZip.decompress($data)
  : $data

Comparison with Other Encoding Functions

Function Purpose Output Size Use Case
GZip.compress() Compression Smaller (60-80% reduction) Reduce bandwidth/storage
Base64.encode() Text encoding Larger (~33% increase) Binary to text conversion
HTML.encode() HTML safety Similar or larger XSS prevention
iconv.encode() Character encoding Similar Character set conversion

Function Reference

Function Input Output Purpose
GZip.compress() String or byte array GZipped byte array Compress data
GZip.decompress() GZipped data Uncompressed byte array Decompress data