2008-10-21 122 views
8

我有一个数据集,包括XML数据,我可以轻松地输出这一个文件:.NET - 将DataSet(XML数据)流式传输到ZIP文件?

DataSet ds = new DataSet(); 
DataTable dt = new DataTable(); 
ds.Tables.Add(dt); 
ds.Load(reader, LoadOption.PreserveChanges, ds.Tables[0]); 
ds.WriteXml("C:\\test.xml"); 

但是我想要做的是XML压缩成ZIP或其他类型的压缩文件,然后就保存将该文件分割为磁盘,同时将ZIP文件分割为1MB块。我真的不想保存未压缩的文件,然后将其压缩,然后将其分开。

我正在寻找具体为:我能流的XML并保存到磁盘

  • 一些C#示例代码中的zip文件(S)

    1. 合适的压缩库这可以告诉我如何做到这一点。
  • 回答

    10

    我设法使用.NET 2.0的gzip压缩来压缩DataSet的XML流。

    这里的博客文章中我几年前做这件事:

    Saving DataSets Locally With Compression

    ...这是我加入到我的数据集的部分类的代码写的压缩文件(博客文章有读取代码也是):

    public void WriteFile(string fileName) 
    { 
        using (FileStream fs = new FileStream(fileName, FileMode.Create)) 
        { 
         Stream s; 
         if (Path.GetExtension(fileName) == ".cmx") 
         { 
          s = new GZipStream(fs, CompressionMode.Compress); 
         } 
         else if (Path.GetExtension(fileName) == ".cmz") 
         { 
          s = new DeflateStream(fs, CompressionMode.Compress); 
         } 
         else 
         { 
          s = fs; 
         } 
         WriteXml(s); 
         s.Close(); 
        } 
    } 
    

    请注意,此代码使用基于文件扩展名的不同压缩方案。这纯粹是为了我可以用我的DataSet测试一个方案与另一个方案。

    +0

    +1恰到好处 – LarsTech 2011-12-09 02:27:30

    1

    该框架包括几个压缩数据流的类。其中之一是GZipStream。如果你搜索它,你会发现很多点击。这里是他们的one。我想像分块输出会涉及一些额外的工作。

    2

    这适用于流或文件,具有良好的许可证和源:http://www.codeplex.com/DotNetZip

    下面的代码做的正是楼主问:写一个DataSet成被分成1MB块一个zip:

    // get connection to the database 
    var c1= new System.Data.SqlClient.SqlConnection(connstring1); 
    var da = new System.Data.SqlClient.SqlDataAdapter() 
    { 
        SelectCommand= new System.Data.SqlClient.SqlCommand(strSelect, c1) 
    }; 
    
    DataSet ds1 = new DataSet(); 
    
    // fill the dataset with the SELECT 
    da.Fill(ds1, "Invoices"); 
    
    // write the XML for that DataSet into a zip file (split into 1mb chunks) 
    using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) 
    { 
        zip.MaxOutputSegmentSize = 1024*1024; 
        zip.AddEntry(zipEntryName, (name,stream) => ds1.WriteXml(stream)); 
        zip.Save(zipFileName); 
    } 
    
    +0

    截至2009年9月,DotNetZip可以读取或写入 “跨越” ZIP文件。如果您想创建拆分或跨接zip,请在保存之前指定每个段的大小限制。然后您会得到N个文件,每个文件都有您指定的大小。 – Cheeso 2009-09-10 20:39:50

    1

    您应该使用Xceed Zip。代码看起来像这样(未测试):

    ZipArchive archive = new ZipArchive(new DiskFile(@"c:\path\file.zip")); 
    
    archive.SplitSize = 1024*1024; 
    archive.BeginUpdate(); 
    
    try 
    { 
        AbstractFile destFile = archive.GetFile("data.xml"); 
    
        using(Stream stream = destFile.OpenWrite(true)) 
        { 
        ds.WriteXml(stream); 
        } 
    } 
    finally 
    { 
        archive.EndUpdate(); 
    } 
    
    2

    3.5框架中包含一个不是很知名的包装API。程序集引用在GAC中,称为WindowsBase。 System.IO.Packaging命名空间包含用于创建OPC文件(例如OOXML)的内容,这些文件是包含xml的zip文件以及任何其他所需的文件。你会得到一些你不需要的东西,但是ZipPackage类使用流式接口迭代地添加内容。

    +0

    +1 System.IO.Packaging rocks! – kenny 2010-03-10 03:20:04

    0

    DotNetZip通过流进行zip压缩,但不会执行多部分zip文件。 :(

    编辑:截至2009年9月,DotNetZip可以做多部分的zip文件

    相关问题