def repack(self, output_file=None): """ rewrites the COMBINE archive with all changes and metadata into a temp file and then attemps to replace to original archive. Works only with archive, which really exist on the filesystem (no StringIO) """ if output_file is None: try: new_file = tempfile.NamedTemporaryFile(dir=os.path.dirname( self._archive), delete=False) except: new_file = tempfile.NamedTemporaryFile(delete=False) else: new_file = output_file new_zip = zipfile.ZipFile(new_file, mode='a') # add main entries self._write_metadata( ) # write metadata first, so the ArchiveEntry is updated self._write_manifest(zip_file=new_zip) # add all entries for (location, entry) in self.entries.items(): if location in self.ARCHIVE_REFERENCE or location == self.MANIFEST_LOCATION: # skip root entry (representing the archive itself) and the two main entries (manifest and metadata) continue if entry.zipinfo is None: entry.zipinfo = self._zip.getinfo(location) buffer = self._zip.read(entry.zipinfo) new_zip.writestr(entry.zipinfo, buffer) # close both zip files new_zip.close() self._zip.close() if output_file is None: # remove old file and move new one os.remove(self._archive) shutil.move(new_file.name, self._archive) else: if not isinstance(self._archive, (str, unicode)): # is a file descriptor self._archive.close() self._archive = new_file # open new zip file self._zip = zipfile.ZipFile(self._archive, mode='a')
def __init__(self, archive): super(CombineArchive, self).__init__() self._archive = archive self._zip = zipfile.ZipFile(archive, mode='a') self.entries = dict() self._read_manifest() self._read_metadata()
def pack(self): """ writes any change of manifest or metadate into the COMBINE archive """ # add main entries self._write_metadata( ) # write metadata first, so the ArchiveEntry is updated self._write_manifest() # close and reopen zipfile, so the zip dictionary gots written self._zip.close() self._zip = zipfile.ZipFile(self._archive, mode='a')
def test_localrepo_cert(): r = WaptLocalRepo('c:/wapt/cache') r.update_packages_index() #list files import custom_zip as zipfile import StringIO with zipfile.ZipFile(open(r.packages_path,'rb'),allowZip64=True) as zip: packages_lines = codecs.decode(zip.read(name='Packages'),'UTF-8').splitlines() names = zip.namelist() #print packages_lines for fn in names: if fn.startswith('ssl/'): cert = SSLCertificate(crt_string=zip.read(name=fn)) print cert.cn print('Done')