Created
July 26, 2017 14:55
-
-
Save yannick/1e77689b27e11dfc9a2b87e5df59c1e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import shutil | |
| import struct | |
| import random | |
| import time | |
| import datetime | |
| import zlib | |
| from io import BytesIO | |
| import io | |
| def randomstring(length=0): | |
| return "test".encode("utf-8") | |
| def write32u(output, value): | |
| # The L format writes the bit pattern correctly whether signed | |
| # or unsigned. | |
| output.write(struct.pack("<L", value)) | |
| crc = zlib.crc32(b"") | |
| size = 0 | |
| f = io.BytesIO() | |
| f.seek(0) | |
| f.write(b'\037\213') #header | |
| f.write(b'\010') # compression method | |
| f.write(chr(8).encode('latin-1')) | |
| write32u(f, int(time.time())) | |
| f.write(b'\002') | |
| f.write(b'\377') | |
| f.write("filename.json".encode('latin-1') + b'\000') | |
| compress = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0) | |
| data = b'' | |
| for i in range(1000): | |
| rs = randomstring(20) | |
| data += compress.compress(rs) | |
| crc = zlib.crc32(rs, crc) | |
| size += len(rs) | |
| flu = compress.flush() | |
| data += flu | |
| f.write(data) | |
| write32u(f, crc) | |
| write32u(f, size & 0xffffffff) | |
| length = f.tell() | |
| f.seek(0) | |
| with open("some111.txt.gz", "wb") as fle: | |
| shutil.copyfileobj(f, fle, length) | |
| f.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment