From: Tony Johansson on
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


From: Alberto Poblacion on
"Tony Johansson" <johansson.andersson(a)telia.com> wrote in message
news:%23dYyDkSuKHA.3360(a)TK2MSFTNGP06.phx.gbl...
> 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.

I don't like either of your examples. The first one will only work with
text files, and you may even lose some information after going through the
StreamReader and then the StreamWriter in case the file has some characters
that don't fit into the default encoding that you are using. And besides,
you read the whole file into memory before writing the compressed file. In
fact, since the string is stored as Unicode, you are using about twice as
many bytes of memory as the size of the file.

The second example will be very slow, as you are reading and writing the
bytes one-by-one.

I would prefer writing a loop similar to your second method, reading from
the input Stream and writing to the GzipStream. But I would use a buffer of
a reasonable size (say, 4 kilobytes) for each iteration of the loop, instead
of a single byte (using the Read(buffer,...) and Write(buffer,...) methods
of the Stream class).


From: Peter Duniho on
Tony Johansson wrote:
> [...]
> 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 ?

I would agree with Alberto's comments, and also point out that being
"more OO" is not necessarily the highest-priority goal. It's well and
good to try for clean OO design, but ultimately programs do need to
produce correct results in a finite, reasonable amount of time.

Being incorrect (as in your first example) is obviously a deal-breaker,
but there is also the potential issue of needlessly limiting the ability
of the code to deal with large amounts of data, efficiently or at all.
The latter could be considered an aspect of correctness as well.

So, when in doubt, make sure the code is correct first, then worry about
design. Of course, the art is in being able to do both at the same
time. :)

Pete