Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

XFDL File Decoding

RSS
Modified on Fri, Oct 28, 2011, 10:08 AM by Administrator Categorized as General Information
The following C# code will transform an XFDL file (see sample content below) to XML.

Sample Input
application/vnd.xfdl;content-encoding="base64-gzip"
H4sIAAAAAAAAC+y9aZOiyrYw/N1fwdMn4vbeYXfJqLBP776BgooDoOL4xhMdCKgoAjI4xfPj3wS0
RkWtkqoePHHv7hKSHFauOVeu9e1/13MDWmqOq1vmv5+QO/gTpJmKperm+N9PXEv4SpIE9RX59L/f
...

Reusable Code

using System;
using System.IO;
using System.IO.Compression;

. . .

public class XfdlFile
{
    public static void DecodeFile(string xfdlFile)
    {
        var xfdlContent = File.ReadAllText(xfdlFile);
        string xmlFile = Path.ChangeExtension(xfdlFile, ".xml");
        DecodeContent(xfdlContent, xmlFile);
    }
    public static void DecodeContent(string xfdlContent, string xmlFile)
    {
        /*--- Strip out first line ---*/
        var pos = xfdlContent.IndexOf('\n') + 1;
        var base64String = xfdlContent.Substring(pos);

        /*--- Decode Base64 to Binary ---*/
        var binaryBytes = Convert.FromBase64String(base64String);
        var binaryStream = new MemoryStream(binaryBytes);

        /*--- Ungzip ---*/
        using (FileStream xmlStream = File.Create(xmlFile))
        {
            using (GZipStream gZip = new GZipStream(binaryStream, CompressionMode.Decompress))
            {
                CopyStreamTo(gZip, xmlStream);
            }
        }
    }
    private static long CopyStreamTo(Stream source, Stream destination)
    {
        byte[] buffer = new byte[2048];
        int bytesRead;
        long totalBytes = 0;
        while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
        {
            destination.Write(buffer, 0, bytesRead);
            totalBytes += bytesRead;
        }
        return totalBytes;
    }

    /// <summary>
    /// Encodes the specified XML file as an XFDL file
    /// </summary>
    public static void EncodeFile(string xmlFile)
    {
        var xmlContent = File.ReadAllBytes(xmlFile);
        string xfdlFile = Path.ChangeExtension(xmlFile, ".xfdl");
        EncodeContent(xmlContent, xfdlFile);
    }

    /// <summary>
    /// Encodes the specified XML into the specified XFDL file
    /// </summary>
    public static void EncodeContent(string xml, string xfdlFile)
    {
        var xfdlContent = EncodeContent(xml);
        File.WriteAllText(xfdlFile, xfdlContent);
    }

    /// <summary>
    /// Encodes the specified XML as XDFL content
    /// </summary>
    public static string EncodeContent(string xml)
    {
        byte[] xmlContent = new byte[xml.Length];

        for (var i = 0; i <= xmlContent.GetUpperBound(0); i++)
            xmlContent[i] = (byte)(xml[i]);

        return EncodeContent(xmlContent);
    }

    /// <summary>
    /// Encodes the specified XML content into the specified XFDL file
    /// </summary>
    public static void EncodeContent(byte[] xmlContent, string xfdlFile)
    {
        var xfdlContent = EncodeContent(xmlContent);
        File.WriteAllText(xfdlFile, xfdlContent);
    }

    /// <summary>
    /// Encodes the specified XML Content as XFDL content
    /// </summary>
    /// <param name="xmlContent"></param>
    /// <returns></returns>
    public static string EncodeContent(byte[] xmlContent)
    {
        /*--- Inits ---*/
        var binaryBytes = new byte[xmlContent.Length];

        /*--- Gzip ---*/
        using (var xmlStream = new MemoryStream(xmlContent))
        {
            using (var binaryStream = new MemoryStream(binaryBytes))
            {
                using (GZipStream gZip = new GZipStream(binaryStream, CompressionMode.Compress))
                {
                    CopyStreamTo(xmlStream, gZip);
                }
            }
        }

        /*--- Encode Binary to Base64 ---*/
        var base64String = Convert.ToBase64String(binaryBytes);

        /*--- Add Header Line ---*/
        var xfdlContent = "application/vnd.xfdl;content-encoding=\"base64-gzip\"\n" + base64String;

        /*--- Return Result ---*/
        return xfdlContent;
    }

}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.