简体   繁体   English

写入文件之前压缩XML

[英]Compressing XML before writing file

I'm trying to save a large amount of data to a XML and the file ends up with a very large size. 我正在尝试将大量数据保存到XML中,并且文件最终的尺寸非常大。 I've searched compression but all examples I found first write the file, then read it to compress to another file, ending with both the large and the compressed files, and the closest I got to removing the intermediate step of writing then reading, ended up with a zip containing an extension-less file(which I can open in notepad as a XML). 我已经搜索了压缩,但是发现所有示例都首先写入文件,然后将其读取以压缩到另一个文件,最后是大文件和压缩文件,最后我最接近删除写入然后读取的中间步骤,结束了包含一个无扩展名文件的zip文件(我可以在记事本中将其作为XML打开)。

this is what I have now: 这就是我现在所拥有的:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (FileStream outFile = File.Create(@"File.zip"))
{
    using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
    {
        using (XmlWriter writer = XmlWriter.Create(Compress, settings))
        {
            //write the XML
        }
    }
}

How do I make the file inside the zip have the XML extension? 如何使zip内的文件具有XML扩展名?

I think this might be a little misunderstanding of fundamentals. 我认为这可能是对基本面的误解。 From what I know, GZip is a compression system, but not an archiving system. 据我所知,GZip是压缩系统,而不是归档系统。 When working with UNIX systems, they tend to be treated as two separate things (whereas ZIP or RAR compression does both). 在UNIX系统上工作时,它们往往被视为两个独立的事物(而ZIP或RAR压缩会同时执行)。 Archiving puts a number of files in one file, and compression makes that file smaller. 归档将多个文件放在一个文件中,压缩使该文件更小。

Have you ever seen Unix packages that are downloaded as "filename.tar.gz"? 您是否看过以“ filename.tar.gz”下载的Unix软件包? That's generally the naming format - they took an archive file (filename.tar) and applied GZip compression to it (filename.tar.gz) 这通常是命名格式-他们获取了一个存档文件(filename.tar)并对其应用了GZip压缩(filename.tar.gz)

Actually, you're technically kind of causing a bit of confusion by naming your file ".zip" (which is a completely different, more commonly-used format). 实际上,从技术上讲,通过将文件命名为“ .zip”(这是一种完全不同的,更常用的格式),会造成一些混乱。 if you want to follow along with UNIX traditions, just name your file "file.xml.gz". 如果要遵循UNIX传统,只需将文件命名为“ file.xml.gz”。 If you want to archive it, use a Tar archiving library. 如果要存档,请使用Tar存档库。 Other libraries such as 7-zip's may have simpler compression systems that will do both for you, for instance if you want this file to be a .zip, easily read by people on Windows computers. 其他库(例如7-zip)可能具有更简单的压缩系统,例如,如果您希望此文件为.zip,则这两种压缩都可以为您完成,Windows计算机上的人们可以轻松阅读。

I think you have to write to a temp file first. 我认为您必须先写入临时文件。 Take a look at 看一眼

DotNetPerls DotNetPerls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM