def testZipFileWithOneFile(self): """Test the zipfile implementation.""" compressions = [zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED] for compression in compressions: outfd = io.BytesIO() # Write the zip into a file like object. infd = io.BytesIO(b"this is a test string") with utils.StreamingZipWriter(outfd, compression=compression) as writer: writer.WriteFromFD(infd, "test.txt") test_zip = zipfile.ZipFile(outfd, "r") test_zip.testzip() self.assertEqual(test_zip.namelist(), ["test.txt"]) self.assertEqual(test_zip.read("test.txt"), infd.getvalue())
def testZipFileWithSymlink(self): """Test that symlinks are preserved when unpacking generated zips.""" compressions = [zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED] for compression in compressions: outfd = io.BytesIO() infd1 = io.BytesIO(b"this is a test string") infd2 = io.BytesIO(b"this is another test string") with utils.StreamingZipWriter(outfd, compression=compression) as writer: writer.WriteFromFD(infd1, "test1.txt") writer.WriteFromFD(infd2, "subdir/test2.txt") writer.WriteSymlink("test1.txt", "test1.txt.link") writer.WriteSymlink("subdir/test2.txt", "test2.txt.link") with utils.TempDirectory() as temp_dir: zip_path = os.path.join(temp_dir, "archive.zip") with open(zip_path, "wb") as fd: fd.write(outfd.getvalue()) zip_fd = zipfile.ZipFile(outfd, "r") link_info = zip_fd.getinfo("test1.txt.link") self.assertEqual(link_info.external_attr, (0o644 | 0o120000) << 16) self.assertEqual(link_info.create_system, 3) link_contents = zip_fd.read("test1.txt.link") self.assertEqual(link_contents, "test1.txt") link_info = zip_fd.getinfo("test2.txt.link") self.assertEqual(link_info.external_attr, (0o644 | 0o120000) << 16) self.assertEqual(link_info.create_system, 3) link_contents = zip_fd.read("test2.txt.link") self.assertEqual(link_contents, "subdir/test2.txt")