Test Failed
Push — main ( 644f6b...d62c57 )
by Sat CFDI
05:32
created

satcfdi.zip   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 43
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 9

2 Functions

Rating   Name   Duplication   Size   Complexity  
A zip_create() 0 19 5
A zip_file() 0 14 4
1 1
import io
2 1
from collections import namedtuple
3 1
from zipfile import ZipFile, ZipInfo
4
5 1
ZipData = namedtuple("ZipFile", "filename data")
6
7
8 1
def zip_create(target: io.BytesIO, files: list[ZipData]):
9 1
    p = target.tell()
10
11 1
    with ZipFile(target, "w") as myzip:
12 1
        for f in files:
13 1
            zinfo = ZipInfo(
14
                filename=f.filename
15
            )
16 1
            zinfo.filename = f.filename
17 1
            zinfo.compress_type = 8
18 1
            zinfo.create_system = 0
19
20 1
            with myzip.open(zinfo, 'w') as stream:
21 1
                zinfo.flag_bits = 2056
22 1
                zinfo.external_attr = 0
23 1
                f.data(stream)
24
25 1
    with target.getbuffer() as view:  # change zip flag bytes
26 1
        view[p + 6:p + 8] = b"\x08\x08"
27
28
29
def zip_file(zipfile, files: list[ZipData]):
30
    # Create a ZipFile object in write mode
31
    with ZipFile(zipfile, 'w') as zipf:
32
        # Add the input file to the zip archive with its base name
33
        for f in files:
34
            zinfo = ZipInfo(
35
                filename=f.filename,
36
                # date_time=datetime_to_tuple(datetime.now())
37
            )
38
            zinfo.compress_type = 8
39
            zinfo.create_system = 0
40
41
            with zipf.open(zinfo, 'w') as stream:
42
                stream.write(f.data)
43