Perl Archive::Zip creates unnecessary folders -


assuming have following array of file names wish zip

my @files = ("c:\windows\perl\test1.txt", "c:\windows\perl\test2.txt", "c:\windows\perl\test3.txt");

if

$obj = archive::zip->new();  foreach (@files)  {     $obj->addfile($_);     }  $obj->writetofilenamed("zippedfolders.zip"); 

when open zippedfolders.zip see contains subfolders, namely windows , perl, latter contains test1, test2, , test3. reason, folders getting zipped up.

how can make files zipped , not have click windows perl folders access zipped files?

as have see, if use addfile add disk file archive, archive::zip adds archive member same path file had originally. if want stored different can pass second parameter used name , path of archive member created.

for purposes suggest use core file::basename module remove path filename , pass basename second parameter.

the code below demonstrates.

something else need aware of can't use single backslashes in perl double quotes - seen escaping following character , disappear. can use pairs of backslashes in string, use single quotes instead, use forward slashes instead (perl sort things out) or if there no spaces in filenames can use qw() have in program.

use strict; use warnings;  use archive::zip; use file::basename 'basename';  @files = qw/   c:\windows\perl\test1.txt   c:\windows\perl\test2.txt   c:\windows\perl\test3.txt /;  $zip = archive::zip->new;  foreach $file (@files) {   $member = basename $file;   printf qq{adding file "%s" archive member "%s"\n}, $file, $member;   $zip->addfile($file, $member);    }  $zip->writetofilenamed('zippedfolders.zip'); 

output

adding file "c:\windows\perl\test1.txt" archive member "test1.txt" adding file "c:\windows\perl\test2.txt" archive member "test2.txt" adding file "c:\windows\perl\test3.txt" archive member "test3.txt" 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -