Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/sage_bootstrap/uncompress/zip_file.py
4055 views
1
"""
2
Zip file support
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2016 Volker Braun <[email protected]>
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 2 of the License, or
11
# (at your option) any later version.
12
# http://www.gnu.org/licenses/
13
#*****************************************************************************
14
15
from __future__ import print_function
16
17
import zipfile
18
from sage_bootstrap.uncompress.filter_os_files import filter_os_files
19
20
21
class SageZipFile(zipfile.ZipFile):
22
"""
23
Wrapper for zipfile.ZipFile to provide better API fidelity with
24
SageTarFile insofar as it's used by this script.
25
"""
26
27
@classmethod
28
def can_read(cls, filename):
29
"""
30
Given an archive filename, returns True if this class can read and
31
process the archive format of that file.
32
"""
33
34
return zipfile.is_zipfile(filename)
35
36
@property
37
def names(self):
38
"""
39
List of filenames in the archive.
40
41
Filters out names of OS-related files that shouldn't be in the
42
archive (.DS_Store, etc.)
43
"""
44
45
return filter_os_files(self.namelist())
46
47
def extractbytes(self, member):
48
"""
49
Return the contents of the specified archive member as bytes.
50
51
If the member does not exist, returns None.
52
"""
53
54
if member in self.namelist():
55
return self.read(member)
56
57
58
59