Convert SVGZ to SVG in C#

When working with vector graphics, file size and compatibility are often key concerns. SVGZ files are GZIP-compressed versions of SVG files, which help reduce file size. However, not all tools or platforms may support them. In this article, we will demonstrate how to convert an SVGZ file to a standard SVG format using Aspose.SVG for .NET. Additionally, you will learn how to automate the process by scanning directories and batch-converting multiple SVGZ files. This conversion allows developers to work with uncompressed, editable SVG content.

About SVGZ File Format

As we have discussed, SVGZ files are GZIP-compressed versions of standard SVG files. They’re not a fundamentally different format; they simply use the widely used GZIP algorithm to reduce the size of the SVG file. Since SVG files are written in XML, they can be quite verbose, especially if they include complex paths, gradients, or inline styles. This makes them very compressible. By applying GZIP compression, an SVG file can be reduced in size by 50–70%, depending on its complexity. This compression is especially useful for reducing load times and improving performance in web applications where file size and bandwidth are critical. Files with the .svgz extension indicate that the file is compressed, while the uncompressed version retains the .svg extension.

Save SVGZ Document as SVG

Aspose.SVG for .NET API provides:

The following code snippet demonstrates a simple, single-file conversion process in which an SVGZ document is loaded from a disk and saved as an uncompressed SVG file. Using the SVGDocument class and SVGSaveFormat.SVG, you can load and save SVGZ as SVG with just a few lines of code:

1using Aspose.Svg.Saving;
2using System.IO;
3...
4
5    // Load an SVG document
6	SVGDocument document = new SVGDocument(Path.Combine(DataDir, "shapes.svgz"));
7	
8	// Save the document as SVGZ
9	document.Save(Path.Combine(OutputDir, "shapes.svg"), SVGSaveFormat.SVG);

Convert Multiple SVGZ to SVG

However, in real-world scenarios, especially when dealing with large batches of assets, such as exported graphics or archived web resources, you may need to convert multiple SVGZ files at once. In such cases, automating the process through directory scanning and batch conversion becomes more efficient.

The following example illustrates how to loop through a folder to convert each .svgz file to .svg. It also demonstrates how to handle any exceptions individually, ensuring that the conversion process continues even if some files encounter errors:

 1using Aspose.Svg.Saving;
 2using System.IO;
 3using System;
 4...
 5
 6	// Loop through all .svgz files in the input directory
 7	foreach (string svgzFile in Directory.GetFiles(DataDir, "*.svgz"))
 8	{
 9		try
10		{
11			// Extract the file name without extension to use for the output file
12			string fileNameWithoutExt = Path.GetFileNameWithoutExtension(svgzFile);
13
14			// Construct the output path with .svg extension
15			string outputPath = Path.Combine(OutputDir, fileNameWithoutExt + ".svg");
16
17			// Load the compressed SVGZ file
18			using (SVGDocument document = new SVGDocument(svgzFile))
19			{
20				// Save it as a standard uncompressed SVG file
21				document.Save(outputPath, SVGSaveFormat.SVG);
22			}
23
24			// Log successful conversion
25			Console.WriteLine($"Converted: {fileNameWithoutExt}.svgz → {fileNameWithoutExt}.svg");
26		}
27		catch (Exception ex)
28		{
29			// Log any errors that occurred during conversion
30			Console.WriteLine($"Failed to convert {svgzFile}: {ex.Message}");
31		}
32	}

SVGZ - Limitations and Compatibility

While SVGZ files offer numerous benefits, it’s essential to keep the following points in mind:

Thus, developers often convert SVGZ files back to standard SVG when they need to edit the graphics, work with third-party libraries that do not support compressed input, or embed the SVG directly into HTML or JavaScript as inline code.

Gzip Compression Technology

GZIP (GNU zip) is a popular compression technology and an associated compressed data format. It is a format you may have used more often than you realize. Introduced by the GNU Project, it has become a standard defined in RFC 1952. You will find it everywhere, from compressed files ending with .gz to web servers that use it to reduce the size of HTML, CSS, and JavaScript before sending them to browsers. GZIP is integrated into tools like gzip and gunzip, and it is also available through libraries such as zlib.

At its core, GZIP uses an algorithm called DEFLATE. DEFLATE combines two techniques: LZ77, which identifies repeating patterns and replaces them with references, and Huffman coding, which compresses frequently used data even further. Together, these techniques effectively reduce file size without losing any data.

One of the key advantages of GZIP is that it operates in a stream-based manner. This means it can compress data on the fly as it is being sent or received, making it ideal for web applications. Additionally, because GZIP is lossless, you don’t have to worry about any part of your data being lost when it is decompressed. Best of all, GZIP support is nearly universal - browsers, operating systems, and programming languages all have the tools to handle GZIP efficiently.

See also

Aspose.SVG offers a free online SVG Converter for converting SVG files to a variety of popular formats. You can easily convert SVG to PDF, XPS, JPG, PNG, BMP, TIFF, GIF, WebP, and SVGZ. Just select a file, choose the format to convert, and you’re done. It’s fast and completely free!

Text “SVG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.