N
nickdu
I'm working on compressing some data. I was surprised to find that the data
generated when I use Stream.Read()/Write() to write to the DeflateStream is
different from the data when I use
StreamReader.ReadToEnd()/StreamWriter.Write(string) to write to the
DeflateStream.
The data I'm compressing is an XML file (ASCII or ANSI encoding). Here is
the logic I use to compress using Stream.Write():
if ((bool) compress == true)
{
input = File.OpenRead(ifile);
try
{
Stream ostream = File.Create(ofile);
try
{
output = new DeflateStream(ostream, CompressionMode.Compress);
try
{
byte[] buffer = new byte[4096];
int bytes;
while ((bytes = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytes);
}
}
finally
{
input.Close();
output.Close();
}
}
catch(Exception)
{
ostream.Close();
throw;
}
}
catch(Exception)
{
input.Close();
throw;
}
}
using StreamReader/StreamWriter I do:
if ((bool) comp == true)
{
Stream output = File.Create(ofile);
using(output)
{
Stream input = File.OpenRead(ifile);
using(input)
{
StreamReader reader = new StreamReader(input, Encoding.Default);
DeflateStream compress = new DeflateStream(output,
CompressionMode.Compress, true);
StreamWriter writer = new StreamWriter(compress, Encoding.Default);
string s = reader.ReadToEnd();
writer.Write(s);
writer.Flush();
}
}
}
The size of the output compressed files are quite different, 109,063 (using
Stream.Read()/Write()) v.s. 110,062 (using StreamReader/StreamWriter). I'm
not as concerned about this but would like to know why such a difference.
More importantly is that when I decompress I get two different results.
Decompressing the Stream.Read()/Write() version produces a file that matches
the original file. Decompressing the StreamReader/StreamWriter version
produces a file with one less byte than the original (though for some reason
fc.exe says the files are the same) and the one byte that's different is
caused by the decompressed file missing the last newline character.
--
Thanks,
Nick
(e-mail address removed)
remove "nospam" change community. to msn.com
generated when I use Stream.Read()/Write() to write to the DeflateStream is
different from the data when I use
StreamReader.ReadToEnd()/StreamWriter.Write(string) to write to the
DeflateStream.
The data I'm compressing is an XML file (ASCII or ANSI encoding). Here is
the logic I use to compress using Stream.Write():
if ((bool) compress == true)
{
input = File.OpenRead(ifile);
try
{
Stream ostream = File.Create(ofile);
try
{
output = new DeflateStream(ostream, CompressionMode.Compress);
try
{
byte[] buffer = new byte[4096];
int bytes;
while ((bytes = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytes);
}
}
finally
{
input.Close();
output.Close();
}
}
catch(Exception)
{
ostream.Close();
throw;
}
}
catch(Exception)
{
input.Close();
throw;
}
}
using StreamReader/StreamWriter I do:
if ((bool) comp == true)
{
Stream output = File.Create(ofile);
using(output)
{
Stream input = File.OpenRead(ifile);
using(input)
{
StreamReader reader = new StreamReader(input, Encoding.Default);
DeflateStream compress = new DeflateStream(output,
CompressionMode.Compress, true);
StreamWriter writer = new StreamWriter(compress, Encoding.Default);
string s = reader.ReadToEnd();
writer.Write(s);
writer.Flush();
}
}
}
The size of the output compressed files are quite different, 109,063 (using
Stream.Read()/Write()) v.s. 110,062 (using StreamReader/StreamWriter). I'm
not as concerned about this but would like to know why such a difference.
More importantly is that when I decompress I get two different results.
Decompressing the Stream.Read()/Write() version produces a file that matches
the original file. Decompressing the StreamReader/StreamWriter version
produces a file with one less byte than the original (though for some reason
fc.exe says the files are the same) and the one byte that's different is
caused by the decompressed file missing the last newline character.
--
Thanks,
Nick
(e-mail address removed)
remove "nospam" change community. to msn.com