T
Tony Johansson
Hi!
I received this text marked with * below as an answer on a mail for several
days ago when I used method CompressFile_1
for compressing file that is located at the bottom.
*This 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.*
My question is the text is saying at the end "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." but I don't understand this.
If I look in the file using a binary viewer I can see that each character is
occupying one byte which I think this means that I use unicode encoding
UTF-8 which is the default because I didn't specified any encoding. So when
I run my program I check the size of the file by using
FileInfo.Length and the length was 1582 and when I check using these two
statements
string data = sourceFile.ReadToEnd();
int sizeData = data.Length;
I can also see that sizedata is 1582 which is the same value as the size of
the file.
So as a summary saying "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."
is wrong beacuse I have just checked this by comparing the size of the file
by the size of data that is read from the file.
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();
}
//Tony
I received this text marked with * below as an answer on a mail for several
days ago when I used method CompressFile_1
for compressing file that is located at the bottom.
*This 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.*
My question is the text is saying at the end "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." but I don't understand this.
If I look in the file using a binary viewer I can see that each character is
occupying one byte which I think this means that I use unicode encoding
UTF-8 which is the default because I didn't specified any encoding. So when
I run my program I check the size of the file by using
FileInfo.Length and the length was 1582 and when I check using these two
statements
string data = sourceFile.ReadToEnd();
int sizeData = data.Length;
I can also see that sizedata is 1582 which is the same value as the size of
the file.
So as a summary saying "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."
is wrong beacuse I have just checked this by comparing the size of the file
by the size of data that is read from the file.
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();
}
//Tony