I know there are libraries out there for working with ZIP files. And, you can alternatively use the functionality built into Windows for working ZIP files.
But, I\'m
Yes, I've used it in the past. I sub-classed DataSet once to support persisting itself out to a file (via the ReadXML/WriteXML method). As an added bonus, I decided to allow it to be, optionally, compressed if desired (this, as you all should already know, is extremely effective with textual data like XML).
I used the GZipStream class (it was my understanding at the time that the related DeflateStream was merely GZip without header information, or some such — I'm sure someone could correct me on this). It works quite simply by piggy-backing on top of another stream and thus you then use the GZipStream in its place. In my case, it was piggy-backing on a FileStream.
Given a MemoryStream to be filled with the output of myDataSet.WriteXML()
, I did something like the following:
if (CompressData)
{
// Write to memory
mStream = new MemoryStream();
Save(mStream);
mStream.Seek(0, SeekOrigin.Begin);
// Filter that through a GZipStream and then to file
fStream = new FileStream(Path.Combine(CacheFilePath, FileName + ".gz"),
FileMode.OpenOrCreate);
zipStream = new GZipStream(fStream, CompressionMode.Compress, true);
Pump(mStream, zipStream);
}
else
{
// Write straight to file
fStream = new FileStream(Path.Combine(CacheFilePath, FileName),
FileMode.OpenOrCreate);
Save(fStream);
}
Where Save()
and Pump()
are simple methods like the following:
private void Pump(Stream input, Stream output)
{
int n;
byte[] bytes = new byte[4096]; // 4KiB at a time
while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
{
output.Write(bytes, 0, n);
}
}
public void Save(Stream stream)
{
AcceptChanges();
WriteXml(stream, XmlWriteMode.WriteSchema);
}