Skip to content

Commit ca2c6da

Browse files
committed
Allow for more than a single deflate chunk to be created in the genzip1.py script.
1 parent 4f47068 commit ca2c6da

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

meta/test/unzip/genzip1.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,37 @@ def create_zip():
1313
content = b"Test"
1414

1515
# --- 1. Create the Raw Deflate Stream (Stored Block) ---
16-
# We are faking "compression" by creating a valid Deflate stream
17-
# that just wraps raw bytes.
18-
# Header Byte: 0x01
19-
# Bit 0 = 1 (BFINAL, this is the last block)
20-
# Bits 1-2 = 00 (BTYPE, stored/uncompressed)
21-
# LEN: 2 bytes, Little Endian length of data
22-
# NLEN: 2 bytes, Little Endian one's complement of length
23-
24-
length = len(content)
25-
nlen = (~length) & 0xFFFF
26-
27-
# Construct the payload
28-
# <HH means two unsigned short integers (2 bytes) in Little Endian
29-
deflate_header = b'\x01' + struct.pack('<HH', length, nlen)
30-
compressed_data = deflate_header + content
16+
# We must split data into 65535 byte chunks because the Deflate
17+
# "Stored" block length header is only 16-bit.
18+
compressed_data = b""
19+
offset = 0
20+
total_len = len(content)
21+
22+
while True:
23+
chunk_size = min(total_len - offset, 65535)
24+
is_final = (offset + chunk_size) == total_len
25+
26+
chunk = content[offset : offset + chunk_size]
27+
28+
# Header Byte:
29+
# Bit 0: BFINAL (1 if last block, 0 if not)
30+
# Bits 1-2: BTYPE (00 = stored)
31+
header_byte = 0x01 if is_final else 0x00
32+
33+
nlen = (~chunk_size) & 0xFFFF
34+
35+
# <BHH = Byte, UShort, UShort
36+
block_header = struct.pack('<BHH', header_byte, chunk_size, nlen)
37+
compressed_data += block_header + chunk
38+
39+
offset += chunk_size
40+
if is_final:
41+
break
3142

3243
# Calculate Metadata
3344
crc = zlib.crc32(content)
3445
compressed_size = len(compressed_data)
35-
uncompressed_size = length
46+
uncompressed_size = len(content) # Use total length here
3647

3748
# --- 2. Construct ZIP Headers ---
3849

0 commit comments

Comments
 (0)