def _create_header(info, format_, encoding, errors): """Return a header block. info is a dictionary with file information, format must be one of the *_FORMAT constants. """ parts = [ tarfile.stn(info.get("name", ""), 100, encoding, errors), tarfile.itn(info.get("mode", 0), 8, format_), tarfile.itn(info.get("uid", 0), 8, format_), tarfile.itn(info.get("gid", 0), 8, format_), tarfile.itn(info.get("size", 0), 12, format_), tarfile.itn(info.get("mtime", 0), 12, format_), b" " * 8, # checksum field info.get("type", tarfile.REGTYPE), tarfile.stn(info.get("linkname", ""), 100, encoding, errors), info.get("magic", tarfile.POSIX_MAGIC), tarfile.stn(info.get("uname", ""), 32, encoding, errors), tarfile.stn(info.get("gname", ""), 32, encoding, errors), tarfile.itn(info.get("devmajor", 0), 8, format_), tarfile.itn(info.get("devminor", 0), 8, format_), tarfile.stn(info.get("prefix", ""), 155, encoding, errors) ] buf = struct.pack("%ds" % tarfile.BLOCKSIZE, b"".join(parts)) chksum = tarfile.calc_chksums(buf[-tarfile.BLOCKSIZE:])[0] buf = buf[:-364] + bytes("%06o\0" % chksum, "ascii") + buf[-357:] return buf
def test_char_fields(self): self.assertEqual(tarfile.stn("foo", 8, "ascii", "strict"), b"foo\0\0\0\0\0") self.assertEqual(tarfile.stn("foobar", 3, "ascii", "strict"), b"foo") self.assertEqual(tarfile.nts(b"foo\0\0\0\0\0", "ascii", "strict"), "foo") self.assertEqual(tarfile.nts(b"foo\0bar\0", "ascii", "strict"), "foo")
def create_ext_sparse_headers(self): for ext_hdr in range(4, len(self.sparsemap), 21): sparse_parts = [ self.sparse_header_chunk(i).decode(tarfile.ENCODING, 'surrogateescape') for i in range(ext_hdr, ext_hdr + 21) ] sparse_parts.append('\1' if ext_hdr + 21 < len(self.sparsemap) else '\0') yield tarfile.stn(''.join(sparse_parts), 512, tarfile.ENCODING, 'surrogateescape')
def get_gnu_header(self): '''Part placed in 'prefix' field of posix header''' parts = [ tarfile.itn(self.mtime, 12, tarfile.GNU_FORMAT), # atime tarfile.itn(self.mtime, 12, tarfile.GNU_FORMAT), # ctime tarfile.itn(0, 12, tarfile.GNU_FORMAT), # offset tarfile.stn('', 4, tarfile.ENCODING, 'surrogateescape'), #longnames b'\0', # unused_pad2 ] parts += [self.sparse_header_chunk(i) for i in range(4)] parts += [ b'\1' if len(self.sparsemap) > 4 else b'\0', # isextended tarfile.itn(self.realsize, 12, tarfile.GNU_FORMAT), # realsize ] return b''.join(parts)
print ("\n==========================\n") import zlib, base64 text = b'STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW ' code = base64.b64encode(zlib.compress(text,9)) print (code) import zlib, base64 s='eNoLDnF09lbwD3MNcvPxD1cIHhxcAE9UKaU=' data = zlib.decompress(base64.b64decode(s)) print (data) print ("\n==========================\n") import tarfile text = b'STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW STACK OVERFLOW ' code = tarfile.stn(text) print (code)
def update_event(self, inp=-1): self.set_output_val(0, tarfile.stn(self.input(0), self.input(1), self.input(2), self.input(3)))
def test_char_fields(self): self.assertEqual(tarfile.stn("foo", 8, "ascii", "strict"), b"foo\0\0\0\0\0") self.assertEqual(tarfile.stn("foobar", 3, "ascii", "strict"), b"foo") self.assertEqual(tarfile.nts(b"foo\0\0\0\0\0", "ascii", "strict"), "foo") self.assertEqual(tarfile.nts(b"foo\0bar\0", "ascii", "strict"), "foo")