def write(self, data): if self.closed: raise Exception('Cannot write to closed stream.') if self.buffer: self.buffer.write(data) else: _libarchive.archive_write_data_from_str(self.archive._a, data) self.bytes += len(data)
def write(self, member, data=None): '''Writes a string buffer to the archive as the given entry.''' if isinstance(member, basestring): member = self.entry_class(pathname=member, encoding=self.encoding) if data: member.size = len(data) member.to_archive(self) if data: _libarchive.archive_write_data_from_str(self._a, data) _libarchive.archive_write_finish_entry(self._a)
def close(self): if self.closed: return if self.buffer: self.entry.size = self.buffer.tell() self.entry.to_archive(self.archive) _libarchive.archive_write_data_from_str(self.archive._a, self.buffer.getvalue()) _libarchive.archive_write_finish_entry(self.archive._a) # Call archive.close() with _defer True to let it know we have been # closed and it is now safe to actually close. self.archive.close(_defer=True) self.archive = None self.closed = True
def write(self, member, data=None): '''Writes a string buffer to the archive as the given entry.''' if isinstance(member, str): member = self.entry_class(pathname=member, encoding=self.encoding) if data: member.size = len(data) member.to_archive(self) if data: if PY3: if isinstance(data, bytes): result = _libarchive.archive_write_data_from_str( self._a, data) else: result = _libarchive.archive_write_data_from_str( self._a, data.encode('utf8')) else: result = _libarchive.archive_write_data_from_str(self._a, data) _libarchive.archive_write_finish_entry(self._a)
def create(container, archivepath, checked): if not isinstance(container, FileSystem): logging.info("container '{0}' is not FileSystem".format(container)) return False archivepath = os.path.abspath(archivepath) BUFFER_SIZE = 2048 with libarchive.Archive(archivepath, 'w') as larchive: a = larchive._a for node in checked: if node.is_dir(): continue path = s2u(node.get_path()) pathname = os.path.join(*node.get_data_array()[1:]) if isinstance(pathname, unicode): pathname = pathname.encode('utf8', errors='replace') st = os.stat(path) entry = _libarchive.archive_entry_new() _libarchive.archive_entry_set_pathname(entry, pathname) _libarchive.archive_entry_set_size(entry, st.st_size) _libarchive.archive_entry_set_filetype( entry, stat.S_IFMT(st.st_mode) ) _libarchive.archive_entry_set_mtime(entry, st.st_mtime, 0) _libarchive.archive_entry_set_perm(entry, stat.S_IMODE(st.st_mode)) _libarchive.archive_write_header(a, entry) f = open(path, 'r') data = f.read(BUFFER_SIZE) count = len(data) while count > 0: _libarchive.archive_write_data_from_str(a, data) data = f.read(BUFFER_SIZE) count = len(data) f.close() _libarchive.archive_entry_free(entry) _libarchive.archive_write_close(a)
def create(container, archivepath, checked): if not isinstance(container, FileSystem): logging.info("container '{0}' is not FileSystem".format(container)) return False archivepath = os.path.abspath(u2s(archivepath)) BUFFER_SIZE = 2048 with libarchive.Archive(archivepath, 'w') as larchive: a = larchive._a for node in checked: if node.is_dir(): continue path = u2s(node.get_path()) pathname = u2s(os.path.join(*node.get_data_array()[1:])) st = os.stat(path) entry = _libarchive.archive_entry_new() _libarchive.archive_entry_set_pathname(entry, pathname) _libarchive.archive_entry_set_size(entry, st.st_size) _libarchive.archive_entry_set_filetype(entry, stat.S_IFMT(st.st_mode)) _libarchive.archive_entry_set_mtime(entry, st.st_mtime, 0) _libarchive.archive_entry_set_perm(entry, stat.S_IMODE(st.st_mode)) _libarchive.archive_write_header(a, entry) f = open(path, 'r') data = f.read(BUFFER_SIZE) count = len(data) while count > 0: _libarchive.archive_write_data_from_str(a, data) data = f.read(BUFFER_SIZE) count = len(data) f.close() _libarchive.archive_entry_free(entry) _libarchive.archive_write_close(a)