rasa has asked for the wisdom of the Perl Monks concerning the following question:

I am seeing this anomaly in both ActivePerl 5.10.1 build 1006 as well as ActivePerl 5.6.1 build 638 on Windows. I am trying to create a TAR file by storing the folder path. However, on repeated attempts, the folder path information is never being written to TAR file. Here is the code:
use strict; use Archive::Tar ; my $tar = Archive::Tar->new; my ($tarFile) = "test.tar"; $tar->add_data( "test/test.txt", "Hello World 1"); $tar->add_data( "qa/test1.txt", "Hello World 2"); $tar->write( "$tarFile.tgz", 8); $tar->clear; undef $tar;
I expect "test.txt" to be deflated to "test" sub-folder. Likewise "test1.txt" to be deflated to "qa" sub-folder. However, the "path" information is empty, when I open the TGZ file. I tried passing an options array using $tar->add_data( "test/test.txt", "Hello World 1", {prefix => 'test'}); to no avail, either. I spent several hours on trying to see what may be amiss. I am unable to make progress. Please help.

Replies are listed 'Best First'.
Re: Cannot add "folder" information to Archive::Tar->add_data in Win32 (ActivePerl)
by graff (Chancellor) on Nov 05, 2009 at 07:23 UTC
    First, the correct name for the tar file would either be "test.tgz" or "test.tar.gz", not "test.tar.tgz".

    Apart from that, I don't understand what your problem is. I ran the script as posted, and it did exactly what I expected: created a gzipped tar file (with an odd name), containing two directories, with one file in each directory, and 13 bytes in each file. When I ran the shell command "tar xzf test.tar.tgz", it created the expected pair of directories in my current working directory, with the expected test*.txt file in each one.

    What exactly are you looking for that you are not finding? (What is the "path information" you mentioned, and where do you think that should be?)

    (UPDATE: Could it be that you are not using a "real" version of the "tar" utility to unpack the tar file created by your test script? What are you doing with the tar file that makes you think the directory structure is not there?)

      graff: What you got is precisely what I expected for me too. But in my case when I run the script, it does everything EXCEPT it did NOT create the "test" and "qa" directories as it did for you. In my case it was just ignoring whatever folder name I was passing as part of the name. In which version of ActivePerl did you run this? It always used to work for me, but when I switched to a new machine and installed new Archive::Tar, the "folder" information isn't getting added to the archive file. Which means, when I add files to TAR as I show in the script, it adds all of them to the root, completely ignoring the folder info I am passing to "add_data" method. Please let me know which version you ran the script successfully under?
        I'm using a vanilla-unix version of perl 5.8.8, not ActiveState. (I happened to be on a macosx machine, but the result would be the same on any unix or linux box.)
Re: Cannot add "folder" information to Archive::Tar->add_data in Win32 (ActivePerl)
by rasa (Initiate) on Nov 05, 2009 at 13:02 UTC
    graff: I use WinRAR and WinZip tools to unpack the TGZ file I produce using the script. It always used to show me the directories from the TAR file. In fact I can still use these 2 tools to open & see directories on older TGZ files created by my other Perl scripts. Unfortunately these tools aren't showing the dir info in the TAR file created using the script I showed here. When I was debugging this issue and was trying to force the longlink flag to true in Archive.pm it tried to produce the directory in the TAR file, which my Zip and RAR programs were able to identify. The TAR file itself had a slight increase in size due to storing dir info. But I reinstated the original Tar.pm file to its original state so as to not tinker with it. So thisis how I am pretty positive that the TAR produced by my script isn't storing dir info in the file. If it's my Winzip or WinRAR that's the problem, they can't show due info on similar TAR files produced by my scripts using an older Archive::Tar. Right? Please help.
      If you modified Tar.pm, causing it to do the right thing with directory structure, and then you reverted to the original Tar.pm, which made it stop doing the right thing, you should report this to the module author, including the test script and the modification that you made to the code. (And you should keep using the modified version of the module.)

      Be sure to include the output of "perl -V" when reporting to the module author.

        graff:

        Thanks a lot. I think I have figure out what is going on here. As you said, the "path" info is there, it is just not being identified as such through WinZip and WinRAR programs. What is still a mystery is that the old Archive::Tar generated TAR.GZ files through my Perl script are still being able to be identified through WinZip and WinRAR, but not the new ones.

        Also, as I said, when I purposefully try to set the longlink flag, even the current WinZip and WinRAR programs identify the path info, but why I can't have that as a solution is because when I forcefully turn this longlink info, it chops of 1 character in the filename. Duh! But, I think the Archive::Tar itself is NOT buggy. That's because, subsequent to what you stated, I wrote another Archive::Tar perl script to read the contents of the archive and explode it. Here it did the right thing. It deflated it to "qa" and "test" directories as I expected it. However, WinZip and WinRAR do NOT do that: Here is the code and thanks for your help.
        use strict; use Archive::Tar; my $tar = Archive::Tar->new; my @numFiles = $tar->read('test.tar.gz') ; $_->extract for(@numFiles); $tar->clear; undef $tar;