1*a29d375cSAndre Fischer#************************************************************** 2*a29d375cSAndre Fischer# 3*a29d375cSAndre Fischer# Licensed to the Apache Software Foundation (ASF) under one 4*a29d375cSAndre Fischer# or more contributor license agreements. See the NOTICE file 5*a29d375cSAndre Fischer# distributed with this work for additional information 6*a29d375cSAndre Fischer# regarding copyright ownership. The ASF licenses this file 7*a29d375cSAndre Fischer# to you under the Apache License, Version 2.0 (the 8*a29d375cSAndre Fischer# "License"); you may not use this file except in compliance 9*a29d375cSAndre Fischer# with the License. You may obtain a copy of the License at 10*a29d375cSAndre Fischer# 11*a29d375cSAndre Fischer# http://www.apache.org/licenses/LICENSE-2.0 12*a29d375cSAndre Fischer# 13*a29d375cSAndre Fischer# Unless required by applicable law or agreed to in writing, 14*a29d375cSAndre Fischer# software distributed under the License is distributed on an 15*a29d375cSAndre Fischer# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a29d375cSAndre Fischer# KIND, either express or implied. See the License for the 17*a29d375cSAndre Fischer# specific language governing permissions and limitations 18*a29d375cSAndre Fischer# under the License. 19*a29d375cSAndre Fischer# 20*a29d375cSAndre Fischer#************************************************************** 21*a29d375cSAndre Fischer 22*a29d375cSAndre Fischer#!/usr/bin/perl 23*a29d375cSAndre Fischer 24*a29d375cSAndre Fischeruse Archive::Zip; 25*a29d375cSAndre Fischer 26*a29d375cSAndre Fischeruse strict; 27*a29d375cSAndre Fischeruse warnings; 28*a29d375cSAndre Fischer 29*a29d375cSAndre Fischer=head NAME 30*a29d375cSAndre Fischer 31*a29d375cSAndre Fischer replace_in_zip.pl - Replace a file in a zip file with another file on disk. 32*a29d375cSAndre Fischer 33*a29d375cSAndre Fischer=head SYNOPSIS 34*a29d375cSAndre Fischer 35*a29d375cSAndre Fischer replace_in_zip.pl <zip-file> <zip-entry-path> <replacement-path> <image-name>+ 36*a29d375cSAndre Fischer 37*a29d375cSAndre Fischer=cut 38*a29d375cSAndre Fischer 39*a29d375cSAndre Fischer 40*a29d375cSAndre Fischersub main (@) 41*a29d375cSAndre Fischer{ 42*a29d375cSAndre Fischer my ($zip_filename, $entry_path, $replacement_path, @image_names) = @_; 43*a29d375cSAndre Fischer 44*a29d375cSAndre Fischer if (scalar @image_names == 0) 45*a29d375cSAndre Fischer { 46*a29d375cSAndre Fischer die "usage: replace_in_zip.pl <zip-file> <zip-entry-path> <replacement-path> <image-name>+"; 47*a29d375cSAndre Fischer } 48*a29d375cSAndre Fischer 49*a29d375cSAndre Fischer # Open the archive. 50*a29d375cSAndre Fischer my $zip = Archive::Zip->new(); 51*a29d375cSAndre Fischer if ( ! -f $zip_filename || $zip->read($zip_filename) != Archive::Zip::AZ_OK) 52*a29d375cSAndre Fischer { 53*a29d375cSAndre Fischer die "can not open zip file $zip_filename"; 54*a29d375cSAndre Fischer } 55*a29d375cSAndre Fischer 56*a29d375cSAndre Fischer $entry_path .= "/" unless $entry_path =~ /\/$/; 57*a29d375cSAndre Fischer $replacement_path .= "/" unless $replacement_path =~ /\/$/; 58*a29d375cSAndre Fischer 59*a29d375cSAndre Fischer foreach my $image_basename (@image_names) 60*a29d375cSAndre Fischer { 61*a29d375cSAndre Fischer printf "replacing %s\n", $image_basename; 62*a29d375cSAndre Fischer 63*a29d375cSAndre Fischer # Get access to the entry. 64*a29d375cSAndre Fischer my $entry_name = $entry_path . $image_basename; 65*a29d375cSAndre Fischer my $member = $zip->memberNamed($entry_name); 66*a29d375cSAndre Fischer die "can not access entry $entry_name" unless defined $member; 67*a29d375cSAndre Fischer 68*a29d375cSAndre Fischer # Check the replacement file. 69*a29d375cSAndre Fischer my $replacement_filename = $replacement_path . $image_basename; 70*a29d375cSAndre Fischer die "can not read the replacement $replacement_filename" 71*a29d375cSAndre Fischer unless -f $replacement_filename; 72*a29d375cSAndre Fischer 73*a29d375cSAndre Fischer # Make the replacement. 74*a29d375cSAndre Fischer $zip->removeMember($member); 75*a29d375cSAndre Fischer my $new_member = $zip->addFile($replacement_filename, $entry_name); 76*a29d375cSAndre Fischer die "replacing failed" unless defined $new_member; 77*a29d375cSAndre Fischer } 78*a29d375cSAndre Fischer 79*a29d375cSAndre Fischer # Write zip back to file. 80*a29d375cSAndre Fischer printf "writing archive back to disk\n"; 81*a29d375cSAndre Fischer if ($zip->overwrite() != Archive::Zip::AZ_OK) 82*a29d375cSAndre Fischer { 83*a29d375cSAndre Fischer die "writing zip back to disk failed"; 84*a29d375cSAndre Fischer } 85*a29d375cSAndre Fischer} 86*a29d375cSAndre Fischer 87*a29d375cSAndre Fischermain(@ARGV); 88