예제 #1
0
 def add_file(self, path, squash=False):
     meta = Metadata()
     if path[0] in "=@:+":
         meta.flag = path[0]
         path = path[1:]
     else:
         meta.flag = '='
     meta.filename = os.path.basename(path)
     meta.pathname = os.path.dirname(path) if not squash else ''
     meta.original_filesize = os.path.getsize(path)
     meta.timestamp = int(os.path.getmtime(path))
     try:
         with open(path, 'rb') as f:
             data = f.read()
     except Exception as e:
         print('Unable to add %s, skipping (reason: %s)',
               path, str(e))
         return
     meta.md5sum = hashlib.md5(data).hexdigest()
     if meta.flag in '@+':
         data = zlib.compress(data, 9)
     meta.compressed_size = len(data)
     if meta.flag in ':+':
         data = xxtea.encrypt(data, self.passwd)
         meta.encrypted_nbytes = len(data)
     if self.metadatas:
         meta.file_location = (self.metadatas[-1].file_location +
                               self.metadatas[-1].size)
     self._file.seek(meta.file_location or ArkManager.HEADER_SIZE,
                     os.SEEK_SET)
     self._file.write(data)
     self.metadatas.append(meta)
     self.file_count = len(self.metadatas)
     self.metadata_offset = self._file.tell()
예제 #2
0
 def _write_metadata(self):
     self._file.seek(self.metadata_offset, os.SEEK_SET)
     data = b''.join(meta.raw_data for meta in self.metadatas)
     data = xxtea.encrypt(data, self.passwd)
     self._file.write(data)