OLD | NEW |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """The ZIP file system implementation.""" | 2 """The ZIP file system implementation.""" |
3 | 3 |
4 from __future__ import unicode_literals | 4 from __future__ import unicode_literals |
5 | 5 |
6 import zipfile | 6 import zipfile |
7 | 7 |
8 # This is necessary to prevent a circular import. | |
9 import dfvfs.vfs.zip_file_entry | |
10 | |
11 from dfvfs.lib import definitions | 8 from dfvfs.lib import definitions |
12 from dfvfs.lib import errors | 9 from dfvfs.lib import errors |
13 from dfvfs.path import zip_path_spec | 10 from dfvfs.path import zip_path_spec |
14 from dfvfs.resolver import resolver | 11 from dfvfs.resolver import resolver |
15 from dfvfs.vfs import file_system | 12 from dfvfs.vfs import file_system |
| 13 from dfvfs.vfs import zip_file_entry |
16 | 14 |
17 | 15 |
18 class ZipFileSystem(file_system.FileSystem): | 16 class ZipFileSystem(file_system.FileSystem): |
19 """File system that uses zipfile. | 17 """File system that uses zipfile. |
20 | 18 |
21 Attributes: | 19 Attributes: |
22 encoding (str): encoding of the file entry name. | 20 encoding (str): encoding of the file entry name. |
23 """ | 21 """ |
24 | 22 |
25 LOCATION_ROOT = '/' | 23 LOCATION_ROOT = '/' |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 117 |
120 Returns: | 118 Returns: |
121 ZipFileEntry: a file entry or None. | 119 ZipFileEntry: a file entry or None. |
122 """ | 120 """ |
123 if not self.FileEntryExistsByPathSpec(path_spec): | 121 if not self.FileEntryExistsByPathSpec(path_spec): |
124 return | 122 return |
125 | 123 |
126 location = getattr(path_spec, 'location', None) | 124 location = getattr(path_spec, 'location', None) |
127 | 125 |
128 if len(location) == 1: | 126 if len(location) == 1: |
129 return dfvfs.vfs.zip_file_entry.ZipFileEntry( | 127 return zip_file_entry.ZipFileEntry( |
130 self._resolver_context, self, path_spec, is_root=True, | 128 self._resolver_context, self, path_spec, is_root=True, |
131 is_virtual=True) | 129 is_virtual=True) |
132 | 130 |
133 kwargs = {} | 131 kwargs = {} |
134 try: | 132 try: |
135 kwargs['zip_info'] = self._zip_file.getinfo(location[1:]) | 133 kwargs['zip_info'] = self._zip_file.getinfo(location[1:]) |
136 except KeyError: | 134 except KeyError: |
137 kwargs['is_virtual'] = True | 135 kwargs['is_virtual'] = True |
138 | 136 |
139 return dfvfs.vfs.zip_file_entry.ZipFileEntry( | 137 return zip_file_entry.ZipFileEntry( |
140 self._resolver_context, self, path_spec, **kwargs) | 138 self._resolver_context, self, path_spec, **kwargs) |
141 | 139 |
142 def GetRootFileEntry(self): | 140 def GetRootFileEntry(self): |
143 """Retrieves the root file entry. | 141 """Retrieves the root file entry. |
144 | 142 |
145 Returns: | 143 Returns: |
146 ZipFileEntry: a file entry or None. | 144 ZipFileEntry: a file entry or None. |
147 """ | 145 """ |
148 path_spec = zip_path_spec.ZipPathSpec( | 146 path_spec = zip_path_spec.ZipPathSpec( |
149 location=self.LOCATION_ROOT, parent=self._path_spec.parent) | 147 location=self.LOCATION_ROOT, parent=self._path_spec.parent) |
(...skipping 21 matching lines...) Expand all Loading... |
171 """ | 169 """ |
172 location = getattr(path_spec, 'location', None) | 170 location = getattr(path_spec, 'location', None) |
173 if location is None: | 171 if location is None: |
174 raise errors.PathSpecError('Path specification missing location.') | 172 raise errors.PathSpecError('Path specification missing location.') |
175 | 173 |
176 if not location.startswith(self.LOCATION_ROOT): | 174 if not location.startswith(self.LOCATION_ROOT): |
177 raise errors.PathSpecError('Invalid location in path specification.') | 175 raise errors.PathSpecError('Invalid location in path specification.') |
178 | 176 |
179 if len(location) > 1: | 177 if len(location) > 1: |
180 return self._zip_file.getinfo(location[1:]) | 178 return self._zip_file.getinfo(location[1:]) |
OLD | NEW |