gbieging
gbieging

Reputation: 318

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. 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).

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?

Upvotes: 2

Views: 1675

Answers (2)

John Alexiou
John Alexiou

Reputation: 29244

I think you have to write to a temp file first. Take a look at

DotNetPerls

Upvotes: 0

Katana314
Katana314

Reputation: 8610

I think this might be a little misunderstanding of fundamentals. From what I know, GZip is a compression system, but not an archiving system. When working with UNIX systems, they tend to be treated as two separate things (whereas ZIP or RAR compression does both). 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"? That's generally the naming format - they took an archive file (filename.tar) and applied GZip compression to it (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). if you want to follow along with UNIX traditions, just name your file "file.xml.gz". If you want to archive it, use a Tar archiving library. 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.

Upvotes: 1

Related Questions