Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Archive::Zip -> How do I create an archive without directories? 1

Status
Not open for further replies.

dmazzini

Programmer
Joined
Jan 20, 2004
Messages
480
Location
US
Hi

Does somebody knows how to create a zip file (.zip) without directories path?

Basically scripts create zip file, but when you extract it using winzip, it unzips the files including full path.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]Archive::Zip[/green] [red]qw([/red][purple]:ERROR_CODES :CONSTANTS[/purple][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red][blue]%SCF[/blue],[blue]$scf_file[/blue],[blue]$trs_file[/blue],[blue]$zip_file[/blue],[blue]$rnc_scf[/blue],[blue]$wbts_scf[/blue],[blue]$rnc_trs[/blue],[blue]$wbts_trs[/blue][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$dirscf[/blue] = [red]qq([/red][purple]C:[purple][b]\\[/b][/purple]Documents and Settings[purple][b]\\[/b][/purple]dmazzini[purple][b]\\[/b][/purple]Desktop[purple][b]\\[/b][/purple]3GDATA[purple][b]\\[/b][/purple]scf[/purple][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$dirtrs[/blue] = [red]qq([/red][purple]C:[purple][b]\\[/b][/purple]Documents and Settings[purple][b]\\[/b][/purple]dmazzini[purple][b]\\[/b][/purple]Desktop[purple][b]\\[/b][/purple]3GDATA[purple][b]\\[/b][/purple]trs[/purple][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$dirzip[/blue] = [red]qq([/red][purple]C:[purple][b]\\[/b][/purple]Documents and Settings[purple][b]\\[/b][/purple]dmazzini[purple][b]\\[/b][/purple]Desktop[/purple][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/chdir.html][black][b]chdir[/b][/black][/url] [blue]$dirscf[/blue][red];[/red]
[black][b]my[/b][/black] [blue]@scf_files[/blue] =[url=http://perldoc.perl.org/functions/glob.html][black][b]glob[/b][/black][/url][red]([/red][red]'[/red][purple]*.xml[/purple][red]'[/red][red])[/red][red];[/red]
[black][b]chdir[/b][/black] [blue]$dirtrs[/blue][red];[/red]
[black][b]my[/b][/black] [blue]@trs_files[/blue] =[black][b]glob[/b][/black][red]([/red][red]'[/red][purple]*.xml[/purple][red]'[/red][red])[/red][red];[/red]

[olive][b]foreach[/b][/olive] [blue]$scf_file[/blue] [red]([/red][blue]@scf_files[/blue][red])[/red][red]{[/red]
        [gray][i]#[WBTS-SCFILE]-[RNC-3000]-[WBTS-TEST1SECTOR].xml[/i][/gray]
        [olive][b]if[/b][/olive] [red]([/red][blue]$scf_file[/blue] =~ [red]/[/red][purple][purple][b]\[[/b][/purple]WBTS-SCFILE[purple][b]\][/b][/purple]-[purple][b]\[[/b][/purple]RNC-(.+)[purple][b]\][/b][/purple]-[purple][b]\[[/b][/purple]WBTS-(.+)[purple][b]\][/b][/purple][purple][b]\.[/b][/purple]xml[/purple][red]/[/red][red])[/red][red]{[/red]
           [blue]$rnc_scf[/blue]=[blue]$1[/blue][red];[/red]
           [blue]$wbts_scf[/blue]=[blue]$2[/blue][red];[/red]
            [olive][b]foreach[/b][/olive] [blue]$trs_file[/blue] [red]([/red][blue]@trs_files[/blue][red])[/red][red]{[/red]
                    [gray][i]#[WBTS-TRSFILE]-[RNC-3000]-[WBTS-TEST1SECTOR].xml[/i][/gray]
                    [olive][b]if[/b][/olive] [red]([/red][blue]$trs_file[/blue] =~ [red]/[/red][purple][purple][b]\[[/b][/purple]WBTS-TRSFILE[purple][b]\][/b][/purple]-[purple][b]\[[/b][/purple]RNC-(.+)[purple][b]\][/b][/purple]-[purple][b]\[[/b][/purple]WBTS-(.+)[purple][b]\][/b][/purple][purple][b]\.[/b][/purple]xml[/purple][red]/[/red][red])[/red][red]{[/red]
                        [blue]$rnc_trs[/blue]=[blue]$1[/blue][red];[/red]
                        [blue]$wbts_trs[/blue]=[blue]$2[/blue][red];[/red]
                        [olive][b]if[/b][/olive] [red]([/red][blue]$rnc_scf[/blue] eq [blue]$rnc_trs[/blue] && [blue]$wbts_scf[/blue] eq [blue]$wbts_trs[/blue][red])[/red] [red]{[/red]
                            [gray][i]#print "$rnc_trs=>$wbts_trs matchs $rnc_scf=>$wbts_scf\n";[/i][/gray]
                            [blue]$SCF[/blue][red]{[/red][blue]$scf_file[/blue][red]}[/red]=[blue]$trs_file[/blue][red];[/red]
                        [red]}[/red]
                     [red]}[/red]  
        
           [red]}[/red]
       [red]}[/red]        
[red]}[/red]

[olive][b]while[/b][/olive] [red]([/red] [red]([/red] [blue]$scf_file[/blue], [blue]$trs_file[/blue] [red])[/red] = [url=http://perldoc.perl.org/functions/each.html][black][b]each[/b][/black][/url][red]([/red] [blue]%SCF[/blue] [red])[/red] [red])[/red] [red]{[/red]
    [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Creating Zip File for [blue]$scf_file[/blue] => [blue]$trs_file[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [black][b]my[/b][/black] [blue]$zip[/blue] = Archive::Zip->[maroon]new[/maroon][red]([/red][red])[/red][red];[/red]
    [olive][b]if[/b][/olive] [red]([/red][blue]$scf_file[/blue] =~ [red]/[/red][purple][purple][b]\[[/b][/purple]WBTS-SCFILE[purple][b]\][/b][/purple]-[purple][b]\[[/b][/purple]RNC-(.+)[purple][b]\][/b][/purple]-[purple][b]\[[/b][/purple]WBTS-(.+)[purple][b]\][/b][/purple][purple][b]\.[/b][/purple]xml[/purple][red]/[/red][red])[/red][red]{[/red]
        [blue]$rnc_scf[/blue]=[blue]$1[/blue][red];[/red]
        [blue]$wbts_scf[/blue]=[blue]$2[/blue][red];[/red]
        [blue]$zip_file[/blue]=[blue]$rnc_scf[/blue].[red]"[/red][purple]_[/purple][red]"[/red].[blue]$wbts_scf[/blue]. [red]"[/red][purple].zip[/purple][red]"[/red] [red];[/red]
    [red]}[/red]
    [blue]$scf_file[/blue]= [red]qq([/red][purple]$dirscf[purple][b]\\[/b][/purple]$scf_file[/purple][red])[/red][red];[/red]
    [blue]$trs_file[/blue]= [red]qq([/red][purple]$dirtrs[purple][b]\\[/b][/purple]$trs_file[/purple][red])[/red][red];[/red]
    [blue]$zip_file[/blue] = [red]qq([/red][purple]$dirzip[purple][b]\\[/b][/purple]$zip_file[/purple][red])[/red][red];[/red]
     
    [black][b]my[/b][/black] [blue]$file_member[/blue] = [blue]$zip[/blue]->[maroon]addFile[/maroon][red]([/red][blue]$scf_file[/blue][red])[/red][red];[/red]
    [blue]$file_member[/blue] = [blue]$zip[/blue]->[maroon]addFile[/maroon][red]([/red][blue]$trs_file[/blue][red])[/red][red];[/red]
    [olive][b]unless[/b][/olive] [red]([/red] [blue]$zip[/blue]->[maroon]writeToFileNamed[/maroon][red]([/red][blue]$zip_file[/blue][red])[/red] == AZ_OK [red])[/red] [red]{[/red] [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]'[/red][purple]write error[/purple][red]'[/red][red];[/red][red]}[/red]
    [blue]$scf_file[/blue]=[blue]$trs_file[/blue]=[red]"[/red][purple][/purple][red]"[/red][red];[/red]   
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Other Modules used :
[ul]
[li]Archive::Zip[/li]
[/ul]
[/tt]



[/code]


dmazzini
GSM/UMTS System and Telecomm Consultant

 
Use
$zip->addTree()
It gives you the option of changing the root path of the files as you add them.
That's the only thing I saw. I usually create a temp directory, copy everything in to it, then create the zip on that.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
So, what you say is that basically it's impossible to create zip files without directories path?


The problem that I have is that another application created by other company, which reads the zip files just without directories. So, I can not "feed" this application with zip files created by Archive::Zip.

I am wondering, why is it not possible to create zip files without full path.....it might be a TODO for Archive::Zip Module.



dmazzini
GSM/UMTS System and Telecomm Consultant

 
Hi guys

I have found the solution how to create zip files without directories.

Here solution without use Archive::Zip Module.

Code:
 [olive][b]while[/b][/olive] [red]([/red] [red]([/red] [blue]$scf_file[/blue], [blue]$trs_file[/blue] [red])[/red] = [url=http://perldoc.perl.org/functions/each.html][black][b]each[/b][/black][/url][red]([/red] [blue]%SCF[/blue] [red])[/red] [red])[/red] [red]{[/red]
    [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Creating Zip File for [blue]$scf_file[/blue] => [blue]$trs_file[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [olive][b]if[/b][/olive] [red]([/red][blue]$scf_file[/blue] =~ [red]/[/red][purple][purple][b]\[[/b][/purple]WBTS-SCFILE[purple][b]\][/b][/purple]-[purple][b]\[[/b][/purple]RNC-(.+)[purple][b]\][/b][/purple]-[purple][b]\[[/b][/purple]WBTS-(.+)[purple][b]\][/b][/purple][purple][b]\.[/b][/purple]xml[/purple][red]/[/red][red])[/red][red]{[/red]
        [blue]$rnc_scf[/blue]=[blue]$1[/blue][red];[/red]
        [blue]$wbts_scf[/blue]=[blue]$2[/blue][red];[/red]
        [blue]$zip_file[/blue]=[blue]$rnc_scf[/blue].[red]"[/red][purple]_[/purple][red]"[/red].[blue]$wbts_scf[/blue]. [red]"[/red][purple].zip[/purple][red]"[/red] [red];[/red]
    [red]}[/red]
    [blue]$scf_file[/blue]= [red]qq([/red][purple]$dirscf[purple][b]\\[/b][/purple]$scf_file[/purple][red])[/red][red];[/red]
    [blue]$trs_file[/blue]= [red]qq([/red][purple]$dirtrs[purple][b]\\[/b][/purple]$trs_file[/purple][red])[/red][red];[/red]
    [blue]$zip_file[/blue] = [red]qq([/red][purple]$dirzip[purple][b]\\[/b][/purple]$zip_file[/purple][red])[/red][red];[/red]  
     
    [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$zippath[/blue] = [red]qq([/red][purple]\"C:[purple][b]\\[/b][/purple]Program Files[purple][b]\\[/b][/purple]WinZip[purple][b]\\[/b][/purple]winzip32.exe\" \-min \-A \"$zip_file\" \"$scf_file\" \"$trs_file\"[/purple][red])[/red][red];[/red] 
    [gray][i]#print "CMD:$zippath\n";[/i][/gray]
    [url=http://perldoc.perl.org/functions/system.html][black][b]system[/b][/black][/url][red]([/red][blue]$zippath[/blue][red])[/red][red];[/red]  
    [blue]$scf_file[/blue]=[blue]$trs_file[/blue]=[red]"[/red][purple][/purple][red]"[/red][red];[/red]   
  [red]}[/red]

This page provided me Winzip Command Line cmds:



Cheers




dmazzini
GSM/UMTS System and Telecomm Consultant
 
dmazzini,

The addFile function lets you specify a second parameter which is the specific name you want to give the file within the zip. This can be a simple name, or it can be a path as well.

Take this hacked together script. It accepts a directory as a parameter and recursively zips up the contents of that directory. If you pass it a boolean of 1 as a second parameter, then it will not save any path information, otherwise it will save everything as relative paths.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Archive::Zip[/green][red];[/red]
[black][b]use[/b][/black] [green]File::Find[/green][red];[/red]
[black][b]use[/b][/black] [green]File::Spec::Functions[/green] [red]qw([/red][purple]abs2rel[/purple][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dir[/blue] = [red]'[/red][purple]C:\devel\text[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]zip_tree[/maroon] [red]{[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$dir[/blue], [blue]$flatten[/blue][red])[/red] = [blue]@_[/blue][red];[/red]
	
	[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Invalid parameter [blue]$dir[/blue][/purple][red]"[/red] [olive][b]unless[/b][/olive] [url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] [blue]$dir[/blue] && [url=http://perldoc.perl.org/functions/-X.html][black][b]-d[/b][/black][/url] [blue]$dir[/blue][red];[/red]
	
	[black][b]my[/b][/black] [blue]$zip[/blue] = Archive::Zip->[maroon]new[/maroon][red]([/red][red])[/red][red];[/red]
	
	[maroon]find[/maroon][red]([/red][black][b]sub[/b][/black] [red]{[/red]
		[black][b]my[/b][/black] [blue]$name[/blue] = [blue]$flatten[/blue] ? [blue]$_[/blue] : [maroon]abs2rel[/maroon][red]([/red][blue]$File::Find::name[/blue], [blue]$dir[/blue][red])[/red][red];[/red]
		[blue]$zip[/blue]->[maroon]addFile[/maroon][red]([/red][blue]$File::Find::name[/blue], [blue]$name[/blue][red])[/red][red];[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]add [blue]$File::Find::name[/blue] -> [blue]$name[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red], [blue]$dir[/blue][red])[/red][red];[/red]
	
	[blue]$zip[/blue]->[maroon]writeToFileNamed[/maroon][red]([/red][red]"[/red][purple][blue]$dir[/blue].zip[/purple][red]"[/red][red])[/red][red];[/red]
[red]}[/red]

[maroon]zip_tree[/maroon][red]([/red][blue]$dir[/blue], [fuchsia]1[/fuchsia][red])[/red][red];[/red]
[tt]------------------------------------------------------------
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Find - Traverse a directory tree.[/li]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
Other Modules used :
[ul]
[li]Archive::Zip[/li]
[/ul]
[/tt]

- Miller
 
Hi Miller

When I tried the script, It gave me this error

Code:
C:\Documents and Settings\dmazzini\Desktop\WinzipPerl\3GDATA\TRS>perl tektipzip.pl
The stat preceding -l _ wasn't an lstat at C:/Perl/site/lib/Archive/Zip.pm line 2885.

Any ideas..

Cheers

dmazzini
GSM/UMTS System and Telecomm Consultant

 
BTW, line 2885 correspond to this routine of the Zip.pm

Code:
# ----------------------------------------------------------------------
# class Archive::Zip::NewFileMember
# Used when adding a pre-existing file to an archive
# ----------------------------------------------------------------------

package Archive::Zip::NewFileMember;
use vars qw( @ISA );
@ISA = qw ( Archive::Zip::FileMember );

BEGIN { use Archive::Zip qw( :CONSTANTS :ERROR_CODES :UTILITY_METHODS ) }

# Given a file name, set up for eventual writing.
sub _newFromFileNamed	# Archive::Zip::NewFileMember
{
	my $class = shift;
	my $fileName = shift;
	return undef if ! ( -r $fileName && ( -f _ || -l _ ) );
	my $self = $class->new( @_ );
	$self->fileName( $fileName );
	$self->{'externalFileName'} = $fileName;
	$self->{'compressionMethod'} = COMPRESSION_STORED;
	my @stat = stat( _ );
	$self->{'compressedSize'} = $self->{'uncompressedSize'} = $stat[7];
	$self->desiredCompressionMethod( ( $self->compressedSize() > 0 )
		? COMPRESSION_DEFLATED
		: COMPRESSION_STORED );
	$self->unixFileAttributes( $stat[2] );
	$self->setLastModFileDateTimeFromUnix( $stat[9] );
	$self->isTextFile( -T _ );
	return $self;
}


dmazzini
GSM/UMTS System and Telecomm Consultant

 
No idea unfortunately.

Here are the version strings for the two boxes I quickly tested the script on.

Windows 2000:
Code:
>perl -MArchive::Zip -e "print $Archive::Zip::VERSION"
1.18

Linux:
Code:
$ perl -MArchive::Zip -e 'print $Archive::Zip::VERSION'
1.20

- Miller
 
Hi Miller

Now your script works for me. I just update Archive::Zip Module from PPM and it worked.

So, the problem was Module was not updated.

Code:
C:\Documents and Settings\dmazzini>perl -MArchive::Zip -e "p
0.11
C:\Documents and Settings\dmazzini>ppm
PPM interactive shell (2.2.0) - type 'help' for available co
PPM> search zip
Packages available from [URL unfurl="true"]http://ppm.ActiveState.com/cgibin/PP[/URL]
Archive-Zip           [1.14 ] Provide an interface to ZIP ar
Compress-Raw-Bzip2    [2.003] Low-Level Interface to bzip2 c
Scrape-USPS-ZipLookup [1.1  ] Programmatic interaction with
                              Lookup web application
SelfUnzip             [0.01 ] Perl extension for blah blah b
Tie-Gzip              [0.01 ] gzip and gunzip with a small m
PPM> archive-Zip
Unknown or ambiguous command 'archive-zip'; type 'help' for
PPM> install Archive-Zip
Install package 'Archive-Zip?' (y/N): y
Installing package 'Archive-Zip'...
Downloading [URL unfurl="true"]http://ppm.activestate.com/PPMPackages/5.6/MSWin[/URL]
Installing C:\Perl\html\site\lib\Archive\Zip.html
Installing C:\Perl\html\site\lib\Archive\Zip\FAQ.html
Installing C:\Perl\html\site\lib\Archive\Zip\MemberRead.html
Installing C:\Perl\html\site\lib\Archive\Zip\Tree.html
Installing C:\Perl\site\lib\Archive\Zip.pm
Installing C:\Perl\site\lib\Archive\Zip.pod
Installing C:\Perl\site\lib\Archive\Zip\BufferedFileHandle.p
Installing C:\Perl\site\lib\Archive\Zip\FAQ.pod
Installing C:\Perl\site\lib\Archive\Zip\MemberRead.pm
Installing C:\Perl\site\lib\Archive\Zip\MockFileHandle.pm
Installing C:\Perl\site\lib\Archive\Zip\Tree.pm
PPM> exit
Quit!

C:\Documents and Settings\dmazzini>perl -MArchive::Zip -e "p
1.13

C:\Documents and Settings\dmazzini\Desktop\WinzipPerl>perl t
add C:\devel\text -> .
add C:\devel\text/[WBTS-TRSFILE]-[RNC-1]-[WBTS-13W016].xml -
add C:\devel\text/[WBTS-TRSFILE]-[RNC-3000]-[WBTS-TEST1SECTO
add C:\devel\text/[WBTS-TRSFILE]-[RNC-3000]-[WBTS-TEST2SECTO
add C:\devel\text/[WBTS-TRSFILE]-[RNC-3000]-[WBTS-TEST3SECTO

Many thanks



dmazzini
GSM/UMTS System and Telecomm Consultant
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top