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