How SunSitemap Supports Gzip Compression for XML Sitemaps

SunSitemap automatically generates .gz compressed versions of sitemap files when the createZip property is set to true, utilizing PHP's native zlib functions to write compressed output without external dependencies.

The msbatal/php-sitemap-generator repository provides a lightweight PHP solution for generating search engine sitemaps through the SunSitemap class. When managing large websites, SunSitemap gzip compression reduces file sizes significantly, improving transfer speeds and server bandwidth usage. The implementation relies entirely on PHP's built-in compression handlers rather than external libraries or command-line tools.

Enabling the createZip Flag

Property Configuration

In SunSitemap.php (lines 63-68), the class exposes a public boolean property createZip that defaults to false. Toggling this property to true instructs the generator to produce a .gz counterpart for every sitemap file it creates.

Constructor Initialization

You can enable compression immediately upon instantiation. The constructor (lines 134-136) accepts a $createZip argument that assigns the value directly to the property:

$sitemap = new SunSitemap('https://example.com', 'sitemaps', 50000, true);

Alternatively, instantiate the class without the flag and set the property manually before generating sitemaps.

Generating Compressed Sitemap Files

Multi-File Index Handling

When sitemaps exceed the URL limit and require an index file, createSitemap() prepares filenames with the .xml.gz extension (lines 29-33). The method verifies that ob_gzhandler is available and that zlib.output_compression is enabled in the PHP configuration. For each XML chunk, it opens a gzip stream using gzopen(), writes content via gzwrite(), and closes with gzclose() (lines 43-51).

Single Sitemap Compression

For single-file sitemaps, the process follows a similar pattern but executes after writing the plain XML file. Lines 56-64 in SunSitemap.php check the same gzip prerequisites. When createZip is active, the code creates a parallel .gz file using the standard zlib workflow alongside the uncompressed version.

Robots.txt Integration for Gzipped Sitemaps

The updateRobots() method ensures search engine crawlers can discover compressed files. When createZip is true and no sitemap index is used, lines 99-101 append a Sitemap: directive pointing to the .gz file rather than the raw XML. This automatic reference update prevents crawler errors and ensures proper indexing of the compressed assets.

Error Handling and Requirements

If the required PHP gzip extensions are unavailable, SunSitemap throws an Exception during the compression phase. The checks for ob_gzhandler and zlib.output_compression serve as guards against attempting compression on servers lacking the necessary zlib support, providing clear feedback for debugging configuration issues.

Complete Implementation Example

The following example demonstrates enabling compression and generating both XML and gzip files:

<?php
require_once 'SunSitemap.php';

// Enable gzip compression via constructor
$sitemap = new SunSitemap('https://example.com', 'sitemaps', 50000, true);

// Add URLs to the sitemap
$sitemap->addUrl('index.php', date('c'), 'daily', '1.0');
$sitemap->addUrl('about.php', date('c'), 'weekly', '0.8');
$sitemap->addUrl('products.php', date('c'), 'weekly', '0.9');

// Generate XML and .gz files
$sitemap->createSitemap();

// Update robots.txt with compressed sitemap reference
$sitemap->updateRobots();
?>

Summary

  • SunSitemap creates .gz files automatically when the createZip boolean property is enabled in the class configuration.
  • Compression relies on PHP's native zlib functions (gzopen, gzwrite, gzclose) rather than external dependencies.
  • The generator handles both single sitemaps and multi-file indexes, adjusting filenames and write operations accordingly.
  • Robots.txt entries automatically reference the compressed versions when applicable, ensuring proper crawler discovery.
  • Missing compression extensions trigger Exceptions with clear error messages to aid server configuration debugging.

Frequently Asked Questions

What PHP extensions are required for SunSitemap gzip compression?

SunSitemap requires the zlib extension to be enabled in your PHP installation. The code specifically checks for ob_gzhandler availability and zlib.output_compression configuration before attempting to create .gz files. If these prerequisites are missing, the class throws an Exception during the sitemap generation process.

Does SunSitemap compress both sitemap indexes and individual sitemaps?

Yes. When createZip is enabled, the generator produces compressed versions for both scenarios. For multi-file indexes, it writes .xml.gz chunks directly using gzopen() and gzwrite() (lines 43-51). For single sitemaps, it creates a parallel .gz file after writing the standard XML (lines 56-64).

How does SunSitemap handle robots.txt when using gzip compression?

The updateRobots() method automatically detects when compression is enabled and no index file exists. In this case, it appends a Sitemap: directive pointing to the .gz file instead of the uncompressed XML (lines 99-101). This ensures search engine crawlers retrieve the smaller compressed files rather than the raw XML.

Can I enable gzip compression after instantiating the SunSitemap class?

Absolutely. While you can pass the $createZip parameter to the constructor (lines 134-136), the createZip property remains publicly accessible. You can toggle it to true at any point before calling createSitemap(), allowing dynamic configuration based on runtime conditions or user preferences.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →