def denit(self): '''Closes and deallocates the archive reader/writer.''' if getattr(self, '_a', None) is None: return try: if self.mode == 'r': _libarchive.archive_read_close(self._a) _libarchive.archive_read_free(self._a) elif self.mode == 'w': _libarchive.archive_write_close(self._a) _libarchive.archive_write_free(self._a) finally: # We only want one try at this... self._a = None
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)