You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

4 years ago
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # read files from within a tar file
  6. #
  7. # History:
  8. # 95-06-18 fl Created
  9. # 96-05-28 fl Open files in binary mode
  10. #
  11. # Copyright (c) Secret Labs AB 1997.
  12. # Copyright (c) Fredrik Lundh 1995-96.
  13. #
  14. # See the README file for information on usage and redistribution.
  15. #
  16. from . import ContainerIO
  17. ##
  18. # A file object that provides read access to a given member of a TAR
  19. # file.
  20. class TarIO(ContainerIO.ContainerIO):
  21. def __init__(self, tarfile, file):
  22. """
  23. Create file object.
  24. :param tarfile: Name of TAR file.
  25. :param file: Name of member file.
  26. """
  27. fh = open(tarfile, "rb")
  28. while True:
  29. s = fh.read(512)
  30. if len(s) != 512:
  31. raise IOError("unexpected end of tar file")
  32. name = s[:100].decode('utf-8')
  33. i = name.find('\0')
  34. if i == 0:
  35. raise IOError("cannot find subfile")
  36. if i > 0:
  37. name = name[:i]
  38. size = int(s[124:135], 8)
  39. if file == name:
  40. break
  41. fh.seek((size + 511) & (~511), 1)
  42. # Open region
  43. ContainerIO.ContainerIO.__init__(self, fh, fh.tell(), size)