Thursday, March 26, 2009

Compressed Streams - GZipStream

GZipStream class allow you to write compressed streams which can consume less storage space. Using GZipStream, you can read and write only individual byte and byte arrays and if your data type is other than bytes, you should use StreamWriter and StreamReader to save strings in a compressed stream.



FileStream fs =
new FileStream(@"C:\fb\tst.txt",
FileMode.Create,
FileAccess.ReadWrite);
FileStream fsCmprs =
new FileStream(@"C:\fb\txtCmprs.txt",
FileMode.Create,
FileAccess.ReadWrite);
GZipStream gzCmprs =
new GZipStream(fsCmprs,
CompressionMode.Compress);

StreamWriter sw = new StreamWriter(fs);
StreamWriter swCmprs = new StreamWriter(gzCmprs);

for (int i = 0; i < 10000; i++)
{
sw.WriteLine(String.Format("{0}_ this is a test.", i));
swCmprs.WriteLine(String.Format("{0}_ this is a test.", i));
}

sw.Flush();
sw.Close();

swCmprs.Flush();
swCmprs.Close();

gzCmprs.Close();
fsCmprs.Close();
fs.Close();


Share/Bookmark

No comments: