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 SVGDocument class that supports reading and loading GZIP-compressed SVGZ files.
- the Aspose.Svg.Saving namespace is presented by API entities for a description of specific save options at the conversion/saving process. For example, the SVGSaveFormat enumeration specifies the format in which the document is saved, allowing you to select either an SVG or SVGZ format.
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:
- Not all applications can read SVGZ files.
- Certain build pipelines or image processors may overlook or improperly handle these files.
- Since SVGZ is a binary format, it cannot be opened directly in a text editor for debugging or editing purposes.
- Incorrect server configurations may lead to double compression or inappropriate content delivery.
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
- Convert SVG to SVGZ in C# – You will learn how to programmatically convert an SVG file to SVGZ using Aspose.SVG for .NET.
- What is an SVG File? – Pros, Cons, XML Code – You will learn more about SVG files. SVG files can be resized without losing resolution, making them ideal for online graphics and responsive web design.
- Save an SVG Document – You will learn how to save an SVG document to a file, ZIP archive, memory stream, or Url using Aspose.SVG for .NET library.
- Gzip (software)
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!