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

I am using Archive::Zip and Archive::Zip::Tree for the first time and have come across a problem, and can't find any mention of it in the docs.

I have put the contents of directory C:\Windows\Desktop\carl\zip\mysql into an archive.
The contents consist of 1 file 'setup.exe', and 1 subfolder 'sub-folder' containing 1 file 'setup.bmp'.
I put all of this in a root folder named 'backup' within the archive.

However, after I run my script and check the contents of the archive, there is also a folder called 'C:' in the archive, with an empty directory structure within it.
Below is a representation of the archive directory structure, my expected structure, and then the script I used to create it.
Note: I have used 2 different zip utilities to check the contents of the archive and verify that the empty dir structure is really there.

Contents of test.zip
C: ->Windows ->Desktop ->Carl ->zip ->mysql backup ->sub-folder ->setup.bmp ->setup.exe
What I expect it to look like:
backup ->sub-folder ->setup.bmp ->setup.exe
The script:
#!/usr/bin/perl use strict; use warnings; use Archive::Zip qw/ :ERROR_CODES :CONSTANTS/; use Archive::Zip::Tree; my $dir = 'C:\Windows\Desktop\carl\zip\mysql'; my $file = 'C:\Windows\Desktop\carl\test.zip'; my $name = 'backup'; my $zip = Archive::Zip->new(); $zip->addTree( $dir, $name ) == AZ_OK or die("could not add tree"); $zip->writeToFileNamed( $file ) == AZ_OK or die("could not write file");

Replies are listed 'Best First'.
Re: Zip::Archive creates unwanted empty directory
by traveler (Parson) on Jul 26, 2002 at 21:44 UTC
    I believe the empty tree is put there so the extracting software can extract it to exactly where it came from. That is, when extracting the program can create C:...carl. I suggest something like:
    my $dir = 'C:\Windows\Desktop\carl\zip\mysql'; my $file = 'C:\Windows\Desktop\carl\test.zip'; my $name = 'backup'; my $zip = Archive::Zip->new(); chdir $dir; $zip->addTree( ".", $name ) == AZ_OK or die("could not add tree"); $zip->writeToFileNamed( $file ) == AZ_OK or die("could not write file");
    HTH, --traveler
      This gives me exactly what I was wanting,
      Thanks!