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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use Archive::Zip - fails zip creation

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
HI,

Occasionaly the code fails when creating the final ZIP.
I suspect that some of the files are locked by other application even for read.
Is there any way to verify that all files that were added to the archive are readable ,prior to creating the final Archive ?
thanks

===============
unless ($zip->writeToFileNamed($ZIPNAME) == AZ_OK) {
print STDOUT "Zip write error $!\n",'RED';
print STDOUT "Zip write error $!\n"
}



Long live king Moshiach !
 
Well, without bother to look more into your code or the Archive::Zip, I would suggest that you simply handle the file operations yourself and use the "addString( $stringOrStringRef, $name )" construct instead of "addFile( $fileName [, $newName ] )".

That's not a perfect solution since the modified date of the file will be lost. But there might be a work around to set that as well.

Just don't forget to use binmode.

- Miller
 
Read the docs. There are plenty of options.

At the very least you can do the directory processing yourself. It wouldn't be hard, and maybe if you use that method there would be some error checking that you could add. Who knows.

- Miller
 
Thanks,I did consider this.
However the problem happens when the physicall zip is created ,not at the stage when files/dirs are logically added.
Then some files may appear locked by some application ...

Long live king Moshiach !
 
Wrong. If you're doing the file reads yourself and only using the "addString" method, then everything happens in real time. Then you can easily error check for failed reads and either abandon that one file or wait until it is readable again.

- Miller
 
Moshiach,

When you let Archive::Zip handle the file operations, I've observed that it waits to read files until you actually call writeToFileNamed. In theory if there was an error, it would give you a specific message about why a particular file could not be read. But you described how this wasn't the case.

That means that errors do not occur in real time, as in when you make each individual addFile call. To avoid this problem, use addString instead of addFile, and read the files yourself.

Observe the following script. If you use the two commented lines instead of the addFile line, errors will be thrown to croak if a file can't be read.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Archive::Zip[/green] [red]qw([/red][purple]:ERROR_CODES :CONSTANTS[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]File::Find[/green][red];[/red]
[black][b]use[/b][/black] [green]File::Slurp[/green] [red]qw([/red][purple]read_file[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]File::Spec::Functions[/green] [red]qw([/red][purple]abs2rel rel2abs updir catdir catfile[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[gray][i]# The Directory to be zipped recursively[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dir[/blue] = [maroon]rel2abs[/maroon][red]([/red][red]'[/red][purple]temp[/purple][red]'[/red][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$parent[/blue] = [maroon]catdir[/maroon][red]([/red][blue]$dir[/blue], updir[red])[/red][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][red]{[/red]
	[purple]no_chdir[/purple] => [fuchsia]1[/fuchsia],
	[purple]wanted[/purple]   => [url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [red]{[/red]
		[black][b]my[/b][/black] [blue]$fqfn[/blue] = [blue]$File::Find::name[/blue][red];[/red]
		[black][b]my[/b][/black] [blue]$rfn[/blue] = [maroon]abs2rel[/maroon][red]([/red][blue]$fqfn[/blue], [blue]$parent[/blue][red])[/red][red];[/red]
		
		[olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/-X.html][black][b]-d[/b][/black][/url] [blue]$fqfn[/blue][red])[/red] [red]{[/red]
			[blue]$zip[/blue]->[maroon]addDirectory[/maroon][red]([/red][blue]$fqfn[/blue], [blue]$rfn[/blue][red])[/red][red];[/red]
			
		[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/-X.html][black][b]-f[/b][/black][/url] [blue]$fqfn[/blue][red])[/red] [red]{[/red]
			[blue]$zip[/blue]->[maroon]addFile[/maroon][red]([/red][blue]$fqfn[/blue], [blue]$rfn[/blue][red])[/red][red];[/red]
			[gray][i]### Replace above line with below two.[/i][/gray]
			[gray][i]#my $data = read_file($fqfn, binmode => ':raw');[/i][/gray]
			[gray][i]#$zip->addString(\$data, $rfn);[/i][/gray]
			
		[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
			[url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [red]"[/red][purple]Unknown entity: [blue]$fqfn[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
		[red]}[/red]
		
		[url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url][red];[/red]
	[red]}[/red]
[red]}[/red], [blue]$dir[/blue][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$archive[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$parent[/blue], [red]'[/red][purple]temp.zip[/purple][red]'[/red][red])[/red][red];[/red]

[gray][i]# Save the Zip file[/i][/gray]
[olive][b]unless[/b][/olive] [red]([/red] [blue]$zip[/blue]->[maroon]writeToFileNamed[/maroon][red]([/red][blue]$archive[/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]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
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]
[li]File::Slurp[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top