C# .NET-将(XML数据的)数据集流到ZIP文件?

C# .NET-将(XML数据的)数据集流到ZIP文件?,c#,compression,zip,C#,Compression,Zip,我有一个由XML数据组成的数据集,我可以轻松地将其输出到文件: 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文件拆

我有一个由XML数据组成的数据集,我可以轻松地将其输出到文件:

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流式传输到其中,并将zip文件保存到磁盘
  • 一些示例C#代码可以告诉我如何做到这一点

  • 我已经设法使用.NET2.0的gzip压缩来压缩数据集的XML流

    这是我几年前写的一篇关于它的博文:

    。。。下面是我添加到数据集的分部类中用于编写压缩文件的代码(博客文章中也有阅读代码):


    请注意,此代码根据文件扩展名使用不同的压缩方案。这纯粹是为了让我可以用我的数据集测试一个方案和另一个方案。

    该框架包括几个用于压缩流的类。其中之一是GZipStream。如果你搜索它,你会发现大量的点击率。这是它们的一部分。我想对输出进行分块将涉及一些额外的工作。

    这适用于流或文件,具有良好的许可证和来源:

    下面的代码完全符合原始海报的要求:将数据集写入一个拆分为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);
    }
    
    你应该使用。代码如下所示(未测试):


    3.5框架中包含一个不太知名的打包API。程序集引用位于GAC中,称为WindowsBase。System.IO.Packaging命名空间包含用于创建OPC文件(例如OOXML)的内容,这些文件是包含xml和其他所需内容的zip文件。您可以获得一些不需要的额外内容,但ZipPackage类使用流接口以迭代方式添加内容。

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


    编辑:从2009年9月起,DotNetZip可以执行多部分压缩文件。

    从2009年9月起,DotNetZip可以读取或写入“跨区”压缩文件。如果要创建拆分或跨区压缩,请在保存之前为每个段指定大小限制。然后,您会得到N个文件,每个文件的大小都是您指定的。
    // 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);
    }
    
    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();
    }