A common requirement for applications which use Internet protocols is the need to encode binary files, as well as compress data to reduce the bandwidth and time required to send or receive the data. Encoding a binary file converts the contents of the file into printable characters which can be safely transferred over the Internet using protocols that only support a subset of 7-bit ASCII characters. This is commonly a restriction for e-mail, since many mail servers still are not capable of correctly processing messages which contain control characters, 8-bit data or multi-byte character sequences found in International text. To address this problem, the sender encodes and sends the data as part of a message; the recipient then extracts and decodes the data, with the end result being the same as the original, without any potential corruption by the mail servers which store and/or forward the message. The File Encoding class supports several encoding and decoding methods, including standard base64 encoding, quoted-printable encoding and uuencoding. For applications which access USENET newsgroup, the class also supports the newer yEnc encoding method which has become a popular method for attaching binary files to a message.
In addition to encoding and decoding files, the File Encoding class also can be used to compress files, reducing their overall size. Two compression algorithms are supported, the standard deflate algorithm which is commonly used in Zip files, and an algorithm based on the Burrows-Wheeler Transform (BWT) which can offer improved compression over the deflate algorithm for some types of files. The developer has control over the type of compression performed, as well as details such as the level of compression which determines how much memory and CPU time is allocated to compress the data. Developers can even create their own custom compression formats by creating an application-specific header block, typically represented by a structure or user-defined type that can be used to provide information to the program.
Note that if you are interested in using this class for purposes of attaching files to an email message, it is not necessary that you use these methods. The Mail Message class has the ability to automatically encode and decode file attachments without requiring that you use the methods in this control. However, the File Encoding class is useful if you need the ability to encode and/or compress for other applications.
There are several different encoding types available, with the default being the standard MIME encoding called Base64. The following encoding methods are supported by the class:
Base64
Base64 encoding works by representing three bytes of data as four
printable characters. Each of the three bytes is converted into
four six-bit numbers, and each six-bit number is converted to one
of 64 printable characters (which is where the encoding method gets
its name). Base64 is the default encoding method used by the
control and is the standard encoding used for MIME formatted email
messages as well as many other applications.
Quoted-Printable
Quoted-printable encoding is primarily used in email messages, and
is best used when the data being encoded is text which consists
primarily of printable characters. Only characters with the
high-bit set or a certain subset of printable characters are
actually encoded by representing them as their hexadecimal value.
All other printable characters are passed through unmodified.
Uucode
One of the original encoding methods used for email, it gets its
name from two UNIX command-line utilities called uuencode and
uudecode, which were used to encode and decode files. Like Base64,
uuencoding converts three bytes of data into four six-bit numbers,
and then a value of 32 is added to ensure that it is printable.
Uuencoding also adds some additional characters which are used to
ensure the integrity of the encoded data. This encoding method is
still used when posting files to USENET newsgroups, but has largely
been replaced by Base64 when attaching files to email messages.
yEnc
yEnc is a relatively new encoding method that was created
specifically for binary newsgroups on USENET. Because USENET
doesn't have the same limitations as email systems in terms of what
kind of characters can be safely used, yEnc only encodes null bytes
and certain control characters; the remaining 8-bit data is passed
through as is which can significantly reduce the overall size of
the encoded data. yEnc also uses checksums to ensure the integrity
of the data and is designed so that a large file can be split
across multiple messages and then recreated.
Encoding a binary file converts the contents of the file into printable characters which can be safely transferred over the Internet using protocols that only support a subset of 7-bit ASCII characters. This is commonly a restriction for e-mail, since many mail servers still are not capable of correctly processing messages which contain control characters, 8-bit data or multi-byte character sequences found in International text. To address this problem, the sender encodes and sends the data as part of a message; the recipient then extracts and decodes the data, with the end result being the same as the original, without any potential corruption by the mail servers which store and/or forward the message.
EncodeFile
This method encodes a file using the specified encoding method,
storing the encoded data in a new file. An option also allows you
to automatically compress the data prior to encoding it in order to
reduce the overall size of the encoded file.
DecodeFile
This method decodes a previously encoded file using the specified
encoding method, restoring the original contents. If the encoded
data was compressed, this method can also be used to automatically
expand the data after it has been decoded.
In addition to encoding and decoding data, the control can be used to compress data in order to reduce its size. The compression methods may be used separately, or may be used as part of the process of encoding a file.
CompressFile
This method reduces the size of a file using the standard Deflate
algorithm. This is the same algorithm that is commonly used in Zip
archives. Note however, that this does not create a Zip file, it
simply uses the same compression method.
ExpandFile
This method restores the original contents of a file that was
previously compressed using the CompressFile method. Note that this
method is not designed to extract files from a Zip archive or
expand data compressed using a different algorithm.
Note that there are advanced options for compressing files, such as the ability to specify the compression type and level. Please refer to the Technical Reference for more information.