Skip to content

Instantly share code, notes, and snippets.

@drewchapin
Last active December 16, 2022 17:56
Show Gist options
  • Save drewchapin/57b3ae7d5c4ba257aa4e02db88388f9e to your computer and use it in GitHub Desktop.
Save drewchapin/57b3ae7d5c4ba257aa4e02db88388f9e to your computer and use it in GitHub Desktop.
Wrappers for WriteXml() and ReadXml() methods of DataSet and DataTable objects that first compresses the XML using Gzip.
/**
* \author Drew Chapin <drew@drewchapin.com>
* \copyright Public Domain
* \date 2017/12/08
* \details Wrappers for WriteXml() and ReadXml() methods of DataSet and DataTable objects that first compresses the XML using Gzip.
*
* https://gist.github.com/drewchapin/57b3ae7d5c4ba257aa4e02db88388f9e
*
*/
using System;
using System.Data;
using System.IO;
using System.IO.Compression;
namespace Porcupine
{
public static class DataSetCompressionExtensions
{
/// <summary>
/// Wrapper for DataSet.WriteXml() method that compresses the file first using the GZip library.
/// </summary>
/// <param name="fileName">The file name (including the path) to which to write.</param>
/// <param name="mode">One of the System.Data.XmlWriteMode values.</param>
public static void WriteCompressedXml( this DataSet dataSet, string fileName, XmlWriteMode mode )
{
Uri uri = new Uri(fileName);
using( FileStream file = new FileStream(uri.LocalPath,FileMode.Create,FileAccess.Write) )
using( GZipStream gzip = new GZipStream(file,CompressionMode.Compress) )
{
dataSet.WriteXml(gzip,mode);
}
}
/// <summary>
/// Wrapper for DataSet.WriteXml() method that compresses the file first using the GZip library.
/// </summary>
/// <param name="fileName">The file name (including the path) to which to write.</param>
public static void WriteCompressedXml( this DataSet dataSet, string fileName )
{
Uri uri = new Uri(fileName);
using( FileStream file = new FileStream(uri.LocalPath,FileMode.Create,FileAccess.Write) )
using( GZipStream gzip = new GZipStream(file,CompressionMode.Compress) )
{
dataSet.WriteXml(gzip);
}
}
/// <summary>
/// Wrapper for DataTable.WriteXml() method that compresses the file first using the GZip library.
/// </summary>
/// <param name="fileName">The file name (including the path) to which to write.</param>
/// <param name="mode">One of the System.Data.XmlWriteMode values.</param>
public static void WriteCompressedXml( this DataTable dataTable, string fileName, XmlWriteMode mode )
{
Uri uri = new Uri(fileName);
using( FileStream file = new FileStream(uri.LocalPath,FileMode.Create,FileAccess.Write) )
using( GZipStream gzip = new GZipStream(file,CompressionMode.Compress) )
{
dataTable.WriteXml(gzip,mode);
}
}
/// <summary>
/// Wrapper for DataTable.WriteXml() method that compresses the file first using the GZip library.
/// </summary>
/// <param name="fileName">The file name (including the path) to which to write.</param>
public static void WriteCompressedXml( this DataTable dataTable, string fileName )
{
Uri uri = new Uri(fileName);
using( FileStream file = new FileStream(uri.LocalPath,FileMode.Create,FileAccess.Write) )
using( GZipStream gzip = new GZipStream(file,CompressionMode.Compress) )
{
dataTable.WriteXml(gzip);
}
}
/// <summary>
/// Wrapper for DataSet.ReadXml() method that compresses the file first using the GZip library.
/// </summary>
/// <param name="fileName">The file name (including the path) from which to read.</param>
/// <returns>The XmlReadMode used to read the data.</returns>
public static XmlReadMode ReadCompressedXml( this DataSet dataSet, string fileName, XmlReadMode mode )
{
Uri uri = new Uri(fileName);
using( FileStream file = new FileStream(uri.LocalPath,FileMode.Open,FileAccess.Write) )
using( GZipStream gzip = new GZipStream(file,CompressionMode.Decompress) )
{
return dataSet.ReadXml(gzip,mode);
}
}
/// <summary>
/// Wrapper for DataSet.ReadXml() method that compresses the file first using the GZip library.
/// </summary>
/// <param name="fileName">The file name (including the path) from which to read.</param>
public static XmlReadMode ReadCompressedXml( this DataSet dataSet, string fileName )
{
Uri uri = new Uri(fileName);
using( FileStream file = new FileStream(uri.LocalPath,FileMode.Open,FileAccess.Write) )
using( GZipStream gzip = new GZipStream(file,CompressionMode.Decompress) )
{
return dataSet.ReadXml(gzip);
}
}
/// <summary>
/// Wrapper for DataTable.ReadXml() method that compresses the file first using the GZip library.
/// </summary>
/// <param name="fileName">The file name (including the path) from which to read.</param>
/// <returns>The XmlReadMode used to read the data.</returns>
public static XmlReadMode ReadCompressedXml( this DataTable dataTable, string fileName )
{
Uri uri = new Uri(fileName);
using( FileStream file = new FileStream(uri.LocalPath,FileMode.Open,FileAccess.Write) )
using( GZipStream gzip = new GZipStream(file,CompressionMode.Decompress) )
{
return dataTable.ReadXml(gzip);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment