直接从流压缩

时间:2012-02-23 14:19:02

标签: c# asp.net .net

我有一个简单的应用程序,它从数据库中检索数据集并将其转换为xml文件。然后读入此xml文件并压缩为.gz文件。

这看起来效率很低 - 是否可以跳过写入临时.xml文件并将其读回压缩文件的步骤?我可以自动将文件发送到流,将其直接转换为转换后的xml格式吗?

这是我的代码:

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);
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

在另一个答案中,您提到您还没有看到任何如何直接从WriteXML写入流的示例。以下是使用代码作为基础直接流式传输到文件的方法。

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);             
    }
}

有趣的是,这是一个作为扩展方法完成的版本,您只需将其用作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);             
        }
    }
}

答案 1 :(得分:0)

为什么不使用以下重载并直接写入ZIp流?

DataSet.WriteXml Method (Stream, XmlWriteMode)

public void WriteXml(
    Stream stream,
    XmlWriteMode mode
)

答案 2 :(得分:0)

要详细说明给定的答案,压缩函数可能看起来像这样。

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);
            }
        }
    }
}