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