#navi_header|技術| メールの添付ファイルの形式として、UNIX世界では昔から Unix to Unix encode というエンコードツールが使われて来ました。現在も多くのディストリビューションで "sharutils" というパッケージ名で提供されています。 >sharutils >The sharutils package contains the GNU shar utilities, a set of tools >for encoding and decoding packages of files (in binary or text format) >in a special plain text format called shell archives (shar). This >format can be sent through e-mail (which can be problematic for regular >binary files). The shar utility supports a wide range of capabilities >(compressing, uuencoding, splitting long files for multi-part >mailings, providing checksums), which make it very flexible at >creating shar files. After the files have been sent, the unshar tool >scans mail messages looking for shar files. Unshar automatically >strips off mail headers and introductory text and then unpacks the >shar files. > >Install sharutils if you send binary files through e-mail. >http://www.gnu.org/software/sharutils/ このパッケージに含まれる uuencode/uudecode というツールと使うとBASE64でバイナリファイルをエンコード・デコードできます。(ヘッダとフッタにはツール独自の情報が付加される点に注意) エンコード: $ cat hoge.bin | uuencode -m hoge.bin > encoded.txt デコード: $ cat encoded.txt | uudecode 注意点としては、まず両ツールとも標準入力からファイルを読み込むという点です。 次に、uuencodeではエンコード結果が標準出力へ流れる、という事。 また、uudecodeでは、出力ファイル名がuuencodeのエンコードしたファイルのヘッダに書いてあるため、出力ファイル名をコマンドラインで指示する必要が無いと言うこと。 試しに適当なファイルで上のサンプルみたくエンコード・デコードしてみて下さい。 もう一つ、実際にメール添付ファイルなどをPerlで処理させる際の注意点として。 uuencode/decodeでは、エンコードされたファイルの最初と最後に独自のヘッダが混じる、という点です。 $ less encoded.txt begin-base64 644 hoge.bin L8hsiOdsHACLM... ....GkdshK9ds7D9KJfds== === このように、uuencodeは冒頭にエンコード方法と元のファイルのパーミッション、ファイル名を記述し、ファイル末尾に = を三つつけます。 これはuudecode側でチェックしているため、もしメール添付のエンコード文字列をuudecodeで復元しようと思ったら、手動でこの二つのヘッダとフッタを付け足した後、uudecodeに流し込む必要があります。 uuencodeを利用する場合はその逆で、MIMEヘッダを用意した後、エンコードされた文字列から冒頭と末尾のヘッダを除き、MIMEヘッダをつけ直す必要があります。 #navi_footer|技術|