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

satcfdi.zip.zip_file()   A

Complexity

Conditions 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nop 2
dl 0
loc 14
ccs 0
cts 0
cp 0
crap 20
rs 9.95
c 0
b 0
f 0
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