Direct compression from a stream

I have a simple application that retrieves a dataset from a database and converts it to an XML file. This xml file is then read and compressed into a .gz file.

This seems rather inefficient - is it possible to skip the write step to a temporary XML file and read it back into compression? Can I automatically send a file to a stream that converts it directly to the converted XML format?

Here is my code:

public partial class _Default : System.Web.UI.Page { DataSet dataset = new DataSet(); string uri = "C:\\tester.xml"; string compressedfileuri = "C:\\tester.gz"; protected void Page_Load(object sender, EventArgs e) { //get the dataset RetrieveData(); //serialize data to a xml file dataset.WriteXml(uri); //compress the data Compress(); } public void RetrieveData() { string connString = ConfigurationManager.ConnectionStrings["Platform"].ConnectionString; using (SqlConnection conn = new SqlConnection(connString)) { SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand("SELECT * FROM expenses.CST_COSTHEADER", conn); adapter.Fill(dataset); } } public void Compress() { using (FileStream fs = new FileStream(uri, FileMode.Open)) { using (FileStream outFile = File.Create(compressedfileuri)) { using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress)) { fs.CopyTo(Compress); } } } } } 
+6
source share
3 answers

Why not just use the following overload and write directly to the ZIp stream?

Method DataSet.WriteXml (stream, XmlWriteMode)

 public void WriteXml( Stream stream, XmlWriteMode mode ) 
0
source

In another answer, you mention that you have not seen examples of how to write directly from WriteXML to a stream. Here's how to transfer a stream directly to a file using your code as a base.

 public void RetrieveData(Stream outputStream) { string connString = ConfigurationManager.ConnectionStrings["Platform"].ConnectionString; using (SqlConnection conn = new SqlConnection(connString)) using (GZipStream compressStream = new GZipStream(outputStream, CompressionMode.Compress)) { SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand("SELECT * FROM expenses.CST_COSTHEADER", conn); adapter.Fill(dataset); dataSet.WriteXml(compressStream); } } 

For fun, there is a version that runs as an extension method, you can simply use it as dataset.WriteCompressedXml(stream)

 public static class ExtensionMethods { public static void WriteCompressedXml(this DataSet dataset, Stream stream) { using (GZipStream compressStream = new GZipStream(stream, CompressionMode.Compress)) { dataSet.WriteXml(compressStream); } } public static void WriteCompressedXml(this DataSet dataset, Stream stream, XmlWriteMode mode) { using (GZipStream compressStream = new GZipStream(stream, CompressionMode.Compress)) { dataSet.WriteXml(compressStream, mode); } } } 
+2
source

To fulfill this answer, the compression function might look something like this.

 public void Compress(DataSet ds) { using (FileStream fs = new FileStream(uri, FileMode.Open)) { using (FileStream outFile = File.Create(compressedfileuri)) { using (GZipStream compress = new GZipStream(outFile, CompressionMode.Compress)) { ds.WriteXml(compress); } } } } 
0
source

Source: https://habr.com/ru/post/909187/


All Articles