http://www.perlmonks.org?node_id=590854


in reply to Problem in sending "tar" files as attachment using MIME::Lite module

I tested this snippet locally without modification, other than substituting my email address and adding use strict/warnings/MIME::Lite.

I succesfully tested sending a mail with a tar file created by both my native tar binary and Archive::Tar. Both mails were received, and untarring the file on the other end worked fine.

The only failure I encountered was when I did not add any files to the newfile.tar. A file created with this:

perl -MArchive::Tar -e '$t = Archive::Tar->new(); $t->write("newfile.t +ar")'
Yielded the following when reading the file with tar -tf:
tar: Cannot identify format. Searching... tar: End of archive volume 1 reached tar: Sorry, unable to determine archive format.
These tests were performed on OpenBSD 3.5, perl 5.8.2, Archive::Tar 1.10, MIME::Lite 3.01. Mails were read with mutt and gmail.

Replies are listed 'Best First'.
Re^2: Problem in sending "tar" files as attachment using MIME::Lite module
by santhi (Monk) on Dec 20, 2006 at 07:21 UTC
    hi,

    I think problem is with creating the Tar file. I am using the code as below:
    my $tar = Archive::Tar->new(); my @filelist = ("txt.del", "txt1.del"); $tar->add_files(@filelist); $tar->write("newpart.tar", 2); my $error = $tar->error(); print "error is $error \n";
    By using the $tar->write("newpart.tar", 2);, I am getting the error as "Invalid archive directory".
    But to use the $tar->write("newpart.tar");, the tar file size is very large.

    So which compression value can be used such that the tar file size will be less and will open without errors.

    Thank you,
    Santhi
      You could try opening the output file first, using PerlIO::gzip so that it gets compressed on output, then pass the file handle to Archive::Tar->write() -- something like this:
      use PerlIO::gzip; use Archive::tar; # ... open( my $tarfh, ">:gzip", "my_tarball.tar.gz" ); # ... $tar->write( $tarfh );
      (I haven't tried that myself, but that's what I would try.)

      UPDATE: (2010-10-18) It seems that PerlIO::gzip should be viewed as superseded by PerlIO::via:gzip. (see PerlIO::gzip or PerlIO::via::gzip).

      That error is not coming from your code snippet, correct? Is it coming from winzip (or some other extraction program) when you try to extract it? I wonder if your extraction program is barfing because it doesn't know how to handle the decompression. If you're going to compress the archive, you should name it either newpart.tar.gz or newpart.tgz. Then your extraction program may be better able to handle it or at least you've let others know the archive needs to be unzipped first. And if your mailing compressed, I wouldn't use the application/tar mime type, application/x-gtar would probably work better.

      -derby