.NET - 将数据集(XML 数据)流式传输到 ZIP 文件?

发布于 2024-07-07 20:46:37 字数 480 浏览 10 评论 0原文

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

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 的块。 我真的不想保存未压缩的文件,然后压缩它,然后分割它。

我正在寻找的具体内容是:

  1. 一个合适的压缩库,我可以将 XML 流式传输到该库,并将 zip 文件保存到磁盘上
  2. 一些示例 C# 代码,可以向我展示如何执行此操作。

I have a DataSet consisting of XML data, I can easily output this to a file:

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

However what I want to do is compress the XML into a ZIP or other type of compressed file and then just save this file to disk while splitting the ZIP file into 1MB chunks. I do not really want to save the uncompressed file, and then zip it, then split it.

What I'm looking for specifically is:

  1. a suitable compression library that I can stream the XML to and have the zip file(s) saved to disk
  2. some sample C# code that can show me how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我很坚强 2024-07-14 20:46:37

我已经成功地使用 .NET 2.0 的 gzip 压缩来压缩 DataSet 的 XML 流。

这是我几年前写的关于它的博客文章:

在本地保存数据集压缩

...这是我添加到 DataSet 的部分类中以写入压缩文件的代码(博客文章也有阅读代码):

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

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

I've managed to compress a DataSet's XML stream using .NET 2.0's gzip compression.

Here's the blog post I made a few years ago about it:

Saving DataSets Locally With Compression

... and here's the code I added to my DataSet's partial class to write the compressed file (the blog post has the reading code too):

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

Note that this code uses different compression schemes based on the file's extension. That was purely so I could test one scheme against the other with my DataSet.

生寂 2024-07-14 20:46:37

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

There is a not-so-well-known packaging API included with the 3.5 framework. The Assembly reference is in the GAC, called WindowsBase. The System.IO.Packaging namespace contains stuff for creating OPC files (e.g. OOXML), which are zip files containing xml and whatever else is desired. You get some extra stuff you don't need, but the ZipPackage class uses a streaming interface for adding content iteratively.

怀中猫帐中妖 2024-07-14 20:46:37

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

下面的代码完全按照原始发布者的要求进行操作:将 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);
}

This works with streams or files, has a good license and source: http://www.codeplex.com/DotNetZip

Here's the code to do exactly what the original poster asked: write a DataSet into a zip that is split into 1mb chunks:

// 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);
}
时光清浅 2024-07-14 20:46:37

您应该使用 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();
}

You should use Xceed Zip. The code would look like this (not tested):

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();
}
﹎☆浅夏丿初晴 2024-07-14 20:46:37

该框架包括一些用于压缩流的类。 其中之一是 GZipStream。 如果你搜索它,你会发现很多命中。 这是一个其中。 我想对输出进行分块会涉及一些额外的工作。

the framework includes a few classes for compressing streams. One of them is GZipStream. If you search for it you'll find plenty of hits. Here's one of them. I imagine chunking the output would involve some some additional work.

っ左 2024-07-14 20:46:37

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

编辑:截至 2009 年 9 月,DotNetZip 可以处理多部分 zip 文件。

DotNetZip does zip compression, via streams, but does not do multi-part zip files.
:(

EDIT: as of September 2009, DotNetZip can do multi-part zip files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文