self.assertIn(b'number in executable: 5', output)
-class TestDataOffsetPrependedZip(unittest.TestCase):
- """Test .data_offset on reading zip files with an executable prepended."""
-
- def setUp(self):
- self.exe_zip = findfile('exe_with_zip', subdir='archivetestdata')
- self.exe_zip64 = findfile('exe_with_z64', subdir='archivetestdata')
-
- def _test_data_offset(self, name):
- with zipfile.ZipFile(name) as zipfp:
- self.assertEqual(zipfp.data_offset, 713)
-
- def test_data_offset_with_exe_prepended(self):
- self._test_data_offset(self.exe_zip)
-
- def test_data_offset_with_exe_prepended_zip64(self):
- self._test_data_offset(self.exe_zip64)
-
-class TestDataOffsetZipWrite(unittest.TestCase):
- """Test .data_offset for ZipFile opened in write mode."""
-
- def setUp(self):
- os.mkdir(TESTFNDIR)
- self.addCleanup(rmtree, TESTFNDIR)
- self.test_path = os.path.join(TESTFNDIR, 'testoffset.zip')
-
- def test_data_offset_write_no_prefix(self):
- with io.BytesIO() as fp:
- with zipfile.ZipFile(fp, "w") as zipfp:
- self.assertEqual(zipfp.data_offset, 0)
-
- def test_data_offset_write_with_prefix(self):
- with io.BytesIO() as fp:
- fp.write(b"this is a prefix")
- with zipfile.ZipFile(fp, "w") as zipfp:
- self.assertEqual(zipfp.data_offset, 16)
-
- def test_data_offset_append_with_bad_zip(self):
- with io.BytesIO() as fp:
- fp.write(b"this is a prefix")
- with zipfile.ZipFile(fp, "a") as zipfp:
- self.assertEqual(zipfp.data_offset, 16)
-
- def test_data_offset_write_no_tell(self):
- # The initializer in ZipFile checks if tell raises AttributeError or
- # OSError when creating a file in write mode when deducing the offset
- # of the beginning of zip data
- class NoTellBytesIO(io.BytesIO):
- def tell(self):
- raise OSError("Unimplemented!")
- with NoTellBytesIO() as fp:
- with zipfile.ZipFile(fp, "w") as zipfp:
- self.assertIsNone(zipfp.data_offset)
-
-
class EncodedMetadataTests(unittest.TestCase):
file_names = ['\u4e00', '\u4e8c', '\u4e09'] # Han 'one', 'two', 'three'
file_content = [
self._lock = threading.RLock()
self._seekable = True
self._writing = False
- self._data_offset = None
try:
if mode == 'r':
self._didModify = True
try:
self.start_dir = self.fp.tell()
- self._data_offset = self.start_dir
except (AttributeError, OSError):
self.fp = _Tellable(self.fp)
self.start_dir = 0
# even if no files are added to the archive
self._didModify = True
self.start_dir = self.fp.tell()
- self._data_offset = self.start_dir
else:
raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")
except:
# self.start_dir: Position of start of central directory
self.start_dir = offset_cd + concat
- # store the offset to the beginning of data for the
- # .data_offset property
- self._data_offset = concat
-
if self.start_dir < 0:
raise BadZipFile("Bad offset for central directory")
fp.seek(self.start_dir, 0)
zinfo._end_offset = end_offset
end_offset = zinfo.header_offset
- @property
- def data_offset(self):
- """The offset to the start of zip data in the file or None if
- unavailable."""
- return self._data_offset
-
def namelist(self):
"""Return a list of file names in the archive."""
return [data.filename for data in self.filelist]