def test_init_mode(self): try: enczipstream.ZipFile(mode='w') except Exception as err: self.fail(err) for mode in ['wb', 'r', 'rb', 'a', 'ab']: self.assertRaises(Exception, enczipstream.ZipFile, mode=mode) for mode in ['wb', 'r', 'rb', 'a', 'ab']: self.assertRaises(Exception, enczipstream.ZipFile, mode=mode + '+')
def test_write_file(self): z = enczipstream.ZipFile(mode='w') for fileobj in self.fileobjs: z.write(fileobj.name) f = tempfile.NamedTemporaryFile(suffix='zip', delete=False) for chunk in z: f.write(chunk) f.close() z2 = zipfile.ZipFile(f.name, 'r') self.assertFalse(z2.testzip()) os.remove(f.name)
def test_write_file_wrong_pwd(self): z = enczipstream.ZipFile(mode='w', pwd=self.password) for fileobj in self.fileobjs: z.write(fileobj.name) f = tempfile.NamedTemporaryFile(suffix='zip', delete=False) for chunk in z: f.write(chunk) f.close() z2 = zipfile.ZipFile(f.name, 'r') z2.setpassword(self.wrong_password) self.assertRaises(RuntimeError, z2.testzip) os.remove(f.name)
def test_writestr(self): z = enczipstream.ZipFile(mode='w') with open(SAMPLE_FILE_RTF, 'rb') as fp: z.writestr('sample.rtf', fp.read()) f = tempfile.NamedTemporaryFile(suffix='zip', delete=False) for chunk in z: f.write(chunk) f.close() z2 = zipfile.ZipFile(f.name, 'r') self.assertFalse(z2.testzip()) os.remove(f.name)
def test_writestr_wrong_pwd(self): z = enczipstream.ZipFile(mode='w', pwd=self.password) with open(SAMPLE_FILE_RTF, 'rb') as fp: z.writestr('sample.rtf', fp.read()) f = tempfile.NamedTemporaryFile(suffix='zip', delete=False) for chunk in z: f.write(chunk) f.close() z2 = zipfile.ZipFile(f.name, 'r') z2.setpassword(self.wrong_password) self.assertRaises(RuntimeError, z2.testzip) os.remove(f.name)
def test_write_file_pwd_compress(self): z = enczipstream.ZipFile(mode='w', compression=enczipstream.ZIP_DEFLATED, pwd=self.password) for fileobj in self.fileobjs: z.write(fileobj.name) f = tempfile.NamedTemporaryFile(suffix='zip', delete=False) for chunk in z: f.write(chunk) f.close() z2 = zipfile.ZipFile(f.name, 'r') z2.setpassword(self.password) self.assertFalse(z2.testzip()) os.remove(f.name)
def test_writestr_pwd_compress(self): z = enczipstream.ZipFile(mode='w', compression=enczipstream.ZIP_DEFLATED, pwd=self.password) with open(SAMPLE_FILE_RTF, 'rb') as fp: z.writestr('sample.rtf', fp.read()) f = tempfile.NamedTemporaryFile(suffix='zip', delete=False) for chunk in z: f.write(chunk) f.close() z2 = zipfile.ZipFile(f.name, 'r') z2.setpassword(self.password) self.assertFalse(z2.testzip()) os.remove(f.name)
def test_write_iterable(self): z = enczipstream.ZipFile(mode='w') def string_generator(): for _ in range(10): yield b'enczipstream\x01\n' data = [string_generator(), string_generator()] for i, d in enumerate(data): z.write_iter(iterable=d, arcname='data_{0}'.format(i)) f = tempfile.NamedTemporaryFile(suffix='zip', delete=False) for chunk in z: f.write(chunk) f.close() z2 = zipfile.ZipFile(f.name, 'r') self.assertFalse(z2.testzip()) os.remove(f.name)
def test_init_no_args(self): enczipstream.ZipFile()
def test_write_iterable_no_archive(self): z = enczipstream.ZipFile(mode='w') self.assertRaises(TypeError, z.write_iter, iterable=range(10))
# -*- coding: utf-8 -*- import os import enczipstream import zipfile with enczipstream.ZipFile(mode='w', compression=enczipstream.ZIP_DEFLATED, pwd=b"password") as z: z.write('LICENSE') z.write('LICENSE', arcname='stuff/LICENSE') with open('tests/sample.rtf', 'rb') as fp: z.writestr('sample.rtf', fp.read()) for root, directories, files in os.walk('zipstream'): for filename in files: path = os.path.join(root, filename) z.write(path, path) with open('test.zip', 'wb') as f: for chunk in z: f.write(chunk) with zipfile.ZipFile('test.zip') as z: z.setpassword(b"password") z.testzip()