xref: /aosp_15_r20/external/icu/tools/ziputil.py (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker# Copyright 2017 The Android Open Source Project
2*0e209d39SAndroid Build Coastguard Worker#
3*0e209d39SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*0e209d39SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*0e209d39SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*0e209d39SAndroid Build Coastguard Worker#
7*0e209d39SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*0e209d39SAndroid Build Coastguard Worker#
9*0e209d39SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*0e209d39SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*0e209d39SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*0e209d39SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*0e209d39SAndroid Build Coastguard Worker# limitations under the License.
14*0e209d39SAndroid Build Coastguard Worker
15*0e209d39SAndroid Build Coastguard Worker"""Utility methods to work with Zip archives."""
16*0e209d39SAndroid Build Coastguard Worker
17*0e209d39SAndroid Build Coastguard Workertry:
18*0e209d39SAndroid Build Coastguard Worker    import itertools.izip as zip
19*0e209d39SAndroid Build Coastguard Workerexcept ImportError:
20*0e209d39SAndroid Build Coastguard Worker    pass
21*0e209d39SAndroid Build Coastguard Worker
22*0e209d39SAndroid Build Coastguard Workerfrom operator import attrgetter
23*0e209d39SAndroid Build Coastguard Workerfrom zipfile import ZipFile
24*0e209d39SAndroid Build Coastguard Workerimport os
25*0e209d39SAndroid Build Coastguard Worker
26*0e209d39SAndroid Build Coastguard Worker
27*0e209d39SAndroid Build Coastguard Workerdef ZipCompare(path_a, path_b):
28*0e209d39SAndroid Build Coastguard Worker  """Compares the contents of two Zip archives, returns True if equal."""
29*0e209d39SAndroid Build Coastguard Worker
30*0e209d39SAndroid Build Coastguard Worker  if not os.path.isfile(path_a) or not os.path.isfile(path_b):
31*0e209d39SAndroid Build Coastguard Worker    return False
32*0e209d39SAndroid Build Coastguard Worker
33*0e209d39SAndroid Build Coastguard Worker  with ZipFile(path_a, 'r') as zip_a:
34*0e209d39SAndroid Build Coastguard Worker    info_a = zip_a.infolist()
35*0e209d39SAndroid Build Coastguard Worker
36*0e209d39SAndroid Build Coastguard Worker  with ZipFile(path_b, 'r') as zip_b:
37*0e209d39SAndroid Build Coastguard Worker    info_b = zip_b.infolist()
38*0e209d39SAndroid Build Coastguard Worker
39*0e209d39SAndroid Build Coastguard Worker  if len(info_a) != len(info_b):
40*0e209d39SAndroid Build Coastguard Worker    return False
41*0e209d39SAndroid Build Coastguard Worker
42*0e209d39SAndroid Build Coastguard Worker  info_a.sort(key=attrgetter('filename'))
43*0e209d39SAndroid Build Coastguard Worker  info_b.sort(key=attrgetter('filename'))
44*0e209d39SAndroid Build Coastguard Worker
45*0e209d39SAndroid Build Coastguard Worker  return all(
46*0e209d39SAndroid Build Coastguard Worker      a.filename == b.filename and
47*0e209d39SAndroid Build Coastguard Worker      a.file_size == b.file_size and
48*0e209d39SAndroid Build Coastguard Worker      a.CRC == b.CRC
49*0e209d39SAndroid Build Coastguard Worker      for a, b in zip(info_a, info_b))
50