Friday, March 07, 2008 8:52 PM
royashbrook
multiple objects in a using and compression sample
Found something nifty I didn't know about today. create multiple objects in a single using statement. I figured i would include some sample code i use for compression/decompression. I'm using the #ziplib libraries, but you could use any stream based process really. The bufferSize variable you can change to fit your needs.
//unzip data. assume we have a compressed file and are creating the output
file
using (FileStream
u = File.Create(PathToUncompressedFile),
c = File.OpenRead(PathToCompressedFile))
using (GZipInputStream gzip = new
GZipInputStream(c))
while
((bufferSize = gzip.Read(buffer, 0, buffer.Length)) != 0)
u.Write(buffer, 0, bufferSize);
//zip data. assume we have a uncompressed file and are creating the output
file
using (FileStream
u = File.OpenRead(PathToUncompressedFile),
c = File.Create(PathToCompressedFile))
using (GZipOutputStream gzip = new
GZipOutputStream(c))
while
((bufferSize = u.Read(buffer, 0, buffer.Length)) != 0)
gzip.Write(buffer, 0, bufferSize);
Filed under: C#, snippet