def test_size_required(self): z = ZipStream(files=[ ZipFile('file.txt', None, lambda: BytesIO(b'test'), None, None), ]) with self.assertRaises(ZipFileSizeRequired): z.size()
def test_generate_zip64_size(self): z = ZipStream(files=[ ZipFile(b'file.txt', 5 * 1024 * 1024 * 1024, lambda: None, datetime.datetime(2008, 11, 10, 17, 53, 59), None), ]) self.assertEqual(z.size(), 5 * 1024 * 1024 * 1024 + 260)
def test_size_while_generate(self): z = ZipStream(files=[ ZipFile('file.txt', 4, lambda: BytesIO(b'test'), None, None), ]) it = iter(z.generate()) next(it) with self.assertRaises(ZipFileInProgress): z.size()
def test_filename_too_long(self): filename = ''.join('a' for i in range(100000)) z = ZipStream(files=[ ZipFile(filename, None, lambda: BytesIO(b'test'), None, None), ]) with self.assertRaises(FileNameTooLong): for _ in z.generate(): pass
def download(): remote_file_size = 1024 def get_remote_file(): res = requests.get('https://httpbin.org/range/%d' % remote_file_size, stream=True) raw = res.raw raw.decode_content = True return raw z = ZipStream(files=[ ZipFile('file.txt', 4, lambda: BytesIO(b'test'), None, None), ZipFile('emptydir/', None, None, None, None), ZipFile('dir/remote.txt', remote_file_size, get_remote_file, None, None), ]) size = z.size() res = Response(z.generate(), mimetype='application/zip') res.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip') res.headers['Content-Length'] = str(size) return res
def test_skip(self): def skip(): raise ZipFileSkip() z = ZipStream(files=[ ZipFile('file.txt', 4, lambda: BytesIO(b'test'), datetime.datetime(2008, 11, 10, 17, 53, 59), None), ZipFile('dir/', None, None, datetime.datetime(2011, 4, 16, 6, 24, 31), None), ZipFile(u'dir/skip', 3, skip, datetime.datetime(2011, 4, 16, 6, 24, 31), b'lorem ipsum'), ]) calculated_size = z.size() data = BytesIO() for chunk in z.generate(): data.write(chunk) zf = zipfile.ZipFile(BytesIO(data.getvalue())) self.assertEqual(len(zf.infolist()), 2) self.assertNotEqual(calculated_size, len(data.getvalue()))
def test_generate(self): z = ZipStream(files=[ ZipFile('file.txt', 4, lambda: BytesIO(b'test'), datetime.datetime(2008, 11, 10, 17, 53, 59), None), ZipFile('dir/', None, None, datetime.datetime(2011, 4, 16, 6, 24, 31), None), ZipFile(u'dir/ČŠŽ', 3, lambda: BytesIO(b'\x42\x42\x42'), datetime.datetime(2011, 4, 16, 6, 24, 31), b'lorem ipsum'), ], comment=b'zip comment') data = BytesIO() for chunk in z.generate(): data.write(chunk) # import hexdump; hexdump.hexdump(data.getvalue()) # with open('test.zip', 'wb') as f: # f.write(data.getvalue()) zf = zipfile.ZipFile(BytesIO(data.getvalue())) entries = [{ key: getattr(entry, key) for key in zipfile.ZipInfo.__slots__ if not key.startswith('_') } for entry in zf.infolist()] self.assertEqual( entries, [ { 'orig_filename': b'file.txt' if IS_PY2 else 'file.txt', 'filename': 'file.txt', 'date_time': (2008, 11, 10, 17, 53, 58), 'compress_type': 0, 'comment': b'', 'extra': b'\x55\x54\x05\x00\x01\x37\x75\x18\x49', 'create_system': 0, 'create_version': 20, 'extract_version': 20, 'reserved': 0, 'flag_bits': 8, 'volume': 0, 'internal_attr': 0, 'external_attr': 0, 'header_offset': 0, 'CRC': 3632233996, 'compress_size': 4, 'file_size': 4, }, { 'orig_filename': b'dir/' if IS_PY2 else 'dir/', 'filename': 'dir/', 'date_time': (2011, 4, 16, 6, 24, 30), # seconds // 2 'compress_type': 0, 'comment': b'', 'extra': b'\x55\x54\x05\x00\x01\x1f\x36\xa9\x4d', 'create_system': 0, 'create_version': 20, 'extract_version': 20, 'reserved': 0, 'flag_bits': 8, 'volume': 0, 'internal_attr': 0, 'external_attr': 0x10, 'header_offset': 67, 'CRC': 0, 'compress_size': 0, 'file_size': 0, }, { 'orig_filename': b'dir/\xc4\x8c\xc5\xa0\xc5\xbd' if IS_PY2 else 'dir/ČŠŽ', 'filename': 'dir/ČŠŽ', 'date_time': (2011, 4, 16, 6, 24, 30), # seconds // 2 'compress_type': 0, 'comment': b'lorem ipsum', 'extra': b'\x55\x54\x05\x00\x01\x1f\x36\xa9\x4d', 'create_system': 0, 'create_version': 20, 'extract_version': 20, 'reserved': 0, 'flag_bits': 2056, 'volume': 0, 'internal_attr': 0, 'external_attr': 0, 'header_offset': 126, 'CRC': 3603074439, 'compress_size': 3, 'file_size': 3, } ]) self.assertEqual(zf.open('file.txt').read(), b'test') self.assertEqual(zf.open('dir/').read(), b'') self.assertEqual(zf.open(u'dir/ČŠŽ').read(), b'BBB') self.assertEqual(zf.comment, b'zip comment') self.assertEqual(z.size(), len(data.getvalue()))