def test_names(self): all_names = [ '1/sub-one', 'one.txt', '诶比屁.txt', 'Füße.txt', '2/sub-two.txt', 'symlink', '1', '2', 'uncompressed', 'max-compressed'] self.ae(all_names, list(names(simple_rar))) all_names.remove('symlink'), all_names.remove('1'), all_names.remove('2') self.ae(all_names, list(names(simple_rar, only_useful=True)))
def extract_first_alphabetically(stream): from calibre.libunzip import sort_key names_ = sorted([ x for x in names(stream) if os.path.splitext(x)[1][1:].lower() in { 'png', 'jpg', 'jpeg', 'gif', 'webp'}], key=sort_key) return extract_member(stream, name=names_[0], match=None)
def test_multipart(self): self.ae(list(names(multipart_rar)), ['Fifteen_Feet_of_Time.pdf']) for v in (True, False): with TempDir() as tdir: extract(multipart_rar, tdir, verify_data=v) h = next(headers(multipart_rar)) raw = open(os.path.join(tdir, h['filename']), 'rb').read() self.ae(len(raw), h['unpack_size']) self.ae(hashlib.sha1(raw).hexdigest(), 'a9fc6a11d000044f17fcdf65816348ce0be3b145')
def do_test(stream): c = comment(stream) if c != b'some comment\n': raise ValueError('Comment not read: %r != %r' % (c, b'some comment\n')) if set(names(stream)) != { '1/sub-one', 'one.txt', '2/sub-two.txt', '诶比屁.txt', 'Füße.txt', 'uncompressed', 'max-compressed'}: raise ValueError('Name list does not match') with TemporaryDirectory('test-unrar') as tdir: extract(stream, tdir) for name in tdata: if name not in '1 2 symlink'.split(): with open(os.path.join(tdir, name), 'rb') as s: if s.read() != tdata[name]: raise ValueError('Did not extract %s properly' % name) for name in tdata: if name not in '1 2 symlink'.split(): d = extract_member(stream, name=name) if d is None or d[1] != tdata[name]: raise ValueError( 'Failed to extract %s %r != %r' % (name, d, tdata[name]))
def extract_cover_image(stream): from calibre.libunzip import sort_key, name_ok for name in sorted(names(stream), key=sort_key): if name_ok(name): return extract_member(stream, name=name, match=None)
def names(path_or_stream): from unrardll import names with StreamAsPath(path_or_stream) as path: for name in names(path, only_useful=True): yield name