T
Tony Johansson
Hi!
I know that I can use the using statement so an implicit close is done but
this is just copy from a book.
These two method below is doing the same thing which is compressing a file
using the GZipStream class.
Now as you can see in method CompressFile_1 I load all the data into a
string object which might cause problem if the file is large.
In the second method CompressFile_2 I loop through the the sourceFile
reading a single byte each time in the loop.
In each loop I write the byte to the GZipStream object.
Now to my question I find the first example better because it's less code
and more OO.
So is it possible to say anything which of these two methods is better then
the other ?
One more question what will happen if I use alternative 1 on a very large
file. ?
The GZipStream has a limitation on 4 GB.
private static void CompressFile_1(string inFile, string utFile)
{
StreamReader sourceFile = File.OpenText(inFile);
string data = sourceFile.ReadToEnd();
FileStream myFileStream = new FileStream(utFile, FileMode.Create,
FileAccess.Write);
GZipStream compStream = new GZipStream(myFileStream,
CompressionMode.Compress);
StreamWriter streamWriter = new StreamWriter(compStream);
streamWriter.Write(data);
streamWriter.Close();
sourceFile.Close();
}
private static void CompressFile_2(string inFile, string utFile)
{
FileStream sourceFile = new FileStream(inFile, FileMode.Open,
FileAccess.Read);
FileStream destFile = new FileStream(utFile, FileMode.Create,
FileAccess.Write);
GZipStream compStream = new GZipStream(destFile,
CompressionMode.Compress);
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
sourceFile.Close();
destFile.Close();
compStream.Close();
}
//Tony
I know that I can use the using statement so an implicit close is done but
this is just copy from a book.
These two method below is doing the same thing which is compressing a file
using the GZipStream class.
Now as you can see in method CompressFile_1 I load all the data into a
string object which might cause problem if the file is large.
In the second method CompressFile_2 I loop through the the sourceFile
reading a single byte each time in the loop.
In each loop I write the byte to the GZipStream object.
Now to my question I find the first example better because it's less code
and more OO.
So is it possible to say anything which of these two methods is better then
the other ?
One more question what will happen if I use alternative 1 on a very large
file. ?
The GZipStream has a limitation on 4 GB.
private static void CompressFile_1(string inFile, string utFile)
{
StreamReader sourceFile = File.OpenText(inFile);
string data = sourceFile.ReadToEnd();
FileStream myFileStream = new FileStream(utFile, FileMode.Create,
FileAccess.Write);
GZipStream compStream = new GZipStream(myFileStream,
CompressionMode.Compress);
StreamWriter streamWriter = new StreamWriter(compStream);
streamWriter.Write(data);
streamWriter.Close();
sourceFile.Close();
}
private static void CompressFile_2(string inFile, string utFile)
{
FileStream sourceFile = new FileStream(inFile, FileMode.Open,
FileAccess.Read);
FileStream destFile = new FileStream(utFile, FileMode.Create,
FileAccess.Write);
GZipStream compStream = new GZipStream(destFile,
CompressionMode.Compress);
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
sourceFile.Close();
destFile.Close();
compStream.Close();
}
//Tony