我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用zipfile.LargeZipFile()。
def verify(self, password=None): try: with zipfile.ZipFile(self.filename, 'r') as z: z.setpassword(password) badfile = z.testzip() if badfile is not None: raise CRCError(badfile) except (zipfile.BadZipfile, zipfile.LargeZipFile), e: raise ArchiveError(e) except RuntimeError, e: if "encrypted" in e.args[0] or "Bad password" in e.args[0]: raise PasswordError(e) else: raise CRCError(e)
def from_zip(cls, file_obj, stages_name, stages_root): "Unpack zip from file_obj into os.path.join(stages_root, stages_name)." try: assignment_root = os.path.join(stages_root, stages_name) os.mkdir(assignment_root) with zipfile.ZipFile(file_obj, 'r') as zf: bad_filename = zf.testzip() if bad_filename is not None: raise Error('Corrupt file in zip: ' + bad_filename) # TODO: Handle case where zf.namelist() uses a lot of memory archived_files = zf.namelist() for af in archived_files: zf.extract(af, assignment_root) # TODO: The stage.save_main_script() code below is used as a workaround # to ensure that the main script is executable. Ideally, file # permissions would be preserved. stages = cls(assignment_root) for stage in stages.stages.itervalues(): stage.save_main_script() return stages except (zipfile.BadZipfile, zipfile.LargeZipFile) as e: raise Error(e)
def large_file_exception_test(self, f, compression): with zipfile.ZipFile(f, "w", compression) as zipfp: self.assertRaises(zipfile.LargeZipFile, zipfp.write, TESTFN, "another.name")
def large_file_exception_test2(self, f, compression): with zipfile.ZipFile(f, "w", compression) as zipfp: self.assertRaises(zipfile.LargeZipFile, zipfp.writestr, "another.name", self.data) if not isinstance(f, str): f.close()
def testMoreThan64kFilesAppend(self): zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) zipf.debug = 100 numfiles = (1 << 16) - 1 for i in range(numfiles): zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) self.assertEqual(len(zipf.namelist()), numfiles) with self.assertRaises(zipfile.LargeZipFile): zipf.writestr("foo%08d" % numfiles, b'') self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) zipf.debug = 100 self.assertEqual(len(zipf.namelist()), numfiles) with self.assertRaises(zipfile.LargeZipFile): zipf.writestr("foo%08d" % numfiles, b'') self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) zipf.debug = 100 self.assertEqual(len(zipf.namelist()), numfiles) numfiles2 = (1 << 16) * 3//2 for i in range(numfiles, numfiles2): zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) self.assertEqual(len(zipf.namelist()), numfiles2) zipf.close() zipf2 = zipfile.ZipFile(TESTFN, mode="r") self.assertEqual(len(zipf2.namelist()), numfiles2) for i in range(numfiles2): self.assertEqual(zipf2.read("foo%08d" % i), "%d" % (i**3 % 57)) zipf2.close()
def large_file_exception_test2(self, f, compression): with zipfile.ZipFile(f, "w", compression) as zipfp: self.assertRaises(zipfile.LargeZipFile, zipfp.writestr, "another.name", self.data)
def test_too_many_files_append(self): zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) zipf.debug = 100 numfiles = 9 for i in range(numfiles): zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) self.assertEqual(len(zipf.namelist()), numfiles) with self.assertRaises(zipfile.LargeZipFile): zipf.writestr("foo%08d" % numfiles, b'') self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) zipf.debug = 100 self.assertEqual(len(zipf.namelist()), numfiles) with self.assertRaises(zipfile.LargeZipFile): zipf.writestr("foo%08d" % numfiles, b'') self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) zipf.debug = 100 self.assertEqual(len(zipf.namelist()), numfiles) numfiles2 = 15 for i in range(numfiles, numfiles2): zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) self.assertEqual(len(zipf.namelist()), numfiles2) zipf.close() zipf2 = zipfile.ZipFile(TESTFN, mode="r") self.assertEqual(len(zipf2.namelist()), numfiles2) for i in range(numfiles2): content = zipf2.read("foo%08d" % i) self.assertEqual(content, "%d" % (i**3 % 57)) zipf2.close()
def testMoreThan64kFilesAppend(self): zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) zipf.debug = 100 numfiles = (1 << 16) - 1 for i in range(numfiles): zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) self.assertEqual(len(zipf.namelist()), numfiles) with self.assertRaises(zipfile.LargeZipFile): zipf.writestr("foo%08d" % numfiles, b'') self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) zipf.debug = 100 self.assertEqual(len(zipf.namelist()), numfiles) with self.assertRaises(zipfile.LargeZipFile): zipf.writestr("foo%08d" % numfiles, b'') self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) zipf.debug = 100 self.assertEqual(len(zipf.namelist()), numfiles) numfiles2 = (1 << 16) * 3//2 for i in range(numfiles, numfiles2): zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) self.assertEqual(len(zipf.namelist()), numfiles2) zipf.close() zipf2 = zipfile.ZipFile(TESTFN, mode="r") self.assertEqual(len(zipf2.namelist()), numfiles2) for i in range(numfiles2): content = zipf2.read("foo%08d" % i).decode('ascii') self.assertEqual(content, "%d" % (i**3 % 57)) zipf2.close()
def test_too_many_files_append(self): zipf = zipfile.ZipFile(TESTFN, "w", self.compression, allowZip64=False) zipf.debug = 100 numfiles = 9 for i in range(numfiles): zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) self.assertEqual(len(zipf.namelist()), numfiles) with self.assertRaises(zipfile.LargeZipFile): zipf.writestr("foo%08d" % numfiles, b'') self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() zipf = zipfile.ZipFile(TESTFN, "a", self.compression, allowZip64=False) zipf.debug = 100 self.assertEqual(len(zipf.namelist()), numfiles) with self.assertRaises(zipfile.LargeZipFile): zipf.writestr("foo%08d" % numfiles, b'') self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() zipf = zipfile.ZipFile(TESTFN, "a", self.compression, allowZip64=True) zipf.debug = 100 self.assertEqual(len(zipf.namelist()), numfiles) numfiles2 = 15 for i in range(numfiles, numfiles2): zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) self.assertEqual(len(zipf.namelist()), numfiles2) zipf.close() zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression) self.assertEqual(len(zipf2.namelist()), numfiles2) for i in range(numfiles2): content = zipf2.read("foo%08d" % i).decode('ascii') self.assertEqual(content, "%d" % (i**3 % 57)) zipf2.close()
def large_file_exception_test(self, f, compression): with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp: self.assertRaises(zipfile.LargeZipFile, zipfp.write, TESTFN, "another.name")
def large_file_exception_test2(self, f, compression): with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp: self.assertRaises(zipfile.LargeZipFile, zipfp.writestr, "another.name", self.data)
def open_openxml(self, _file): """ Open an OpenXML file :param _file: filename or file contents in a file object :return: nothing """ # This looks like a zip file, need to look for vbaProject.bin inside # It can be any OLE file inside the archive #...because vbaProject.bin can be renamed: # see http://www.decalage.info/files/JCV07_Lagadec_OpenDocument_OpenXML_v4_decalage.pdf#page=18 log.info('Opening ZIP/OpenXML file %s' % self.filename) try: z = zipfile.ZipFile(_file) #TODO: check if this is actually an OpenXML file #TODO: if the zip file is encrypted, suggest to use the -z option, or try '-z infected' automatically # check each file within the zip if it is an OLE file, by reading its magic: for subfile in z.namelist(): magic = z.open(subfile).read(len(olefile.MAGIC)) if magic == olefile.MAGIC: log.debug('Opening OLE file %s within zip' % subfile) ole_data = z.open(subfile).read() try: self.ole_subfiles.append( VBA_Parser(filename=subfile, data=ole_data, relaxed=self.relaxed)) except OlevbaBaseException as exc: if self.relaxed: log.info('%s is not a valid OLE file (%s)' % (subfile, exc)) log.debug('Trace:', exc_info=True) continue else: raise SubstreamOpenError(self.filename, subfile, exc) z.close() # set type only if parsing succeeds self.type = TYPE_OpenXML except OlevbaBaseException as exc: if self.relaxed: log.info('Error {0} caught in Zip/OpenXML parsing for file {1}' .format(exc, self.filename)) log.debug('Trace:', exc_info=True) else: raise except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile, IOError) as exc: # TODO: handle parsing exceptions log.info('Failed Zip/OpenXML parsing for file %r (%s)' % (self.filename, exc)) log.debug('Trace:', exc_info=True)