rtzip 0.5.3__tar.gz → 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rtzip-0.6.0/PKG-INFO +4 -0
- rtzip-0.6.0/pyproject.toml +27 -0
- rtzip-0.6.0/pyproject.toml.orig +35 -0
- rtzip-0.6.0/src/rtzip/__init__.py +8 -0
- {rtzip-0.5.3 → rtzip-0.6.0}/src/rtzip/algorithm_register.py +3 -2
- rtzip-0.5.3/src/rtzip/codec_wrappers.py → rtzip-0.6.0/src/rtzip/builtin_codecs.py +8 -8
- rtzip-0.6.0/src/rtzip/builtin_sources.py +48 -0
- {rtzip-0.5.3 → rtzip-0.6.0}/src/rtzip/const.py +1 -1
- rtzip-0.6.0/src/rtzip/context.py +81 -0
- rtzip-0.6.0/src/rtzip/errors.py +50 -0
- {rtzip-0.5.3 → rtzip-0.6.0}/src/rtzip/models.py +139 -104
- rtzip-0.6.0/src/rtzip/path_methods.py +24 -0
- rtzip-0.6.0/src/rtzip/remotezip.py +722 -0
- rtzip-0.6.0/src/rtzip/safety_wrappers.py +14 -0
- rtzip-0.6.0/src/rtzip/stream_io.py +81 -0
- rtzip-0.5.3/src/rtzip/zip_aes.py → rtzip-0.6.0/src/rtzip/wz_aes.py +110 -82
- {rtzip-0.5.3 → rtzip-0.6.0}/src/rtzip/wzaes_backends/__init__.py +0 -0
- {rtzip-0.5.3 → rtzip-0.6.0}/src/rtzip/wzaes_backends/cryptography_impl.py +1 -1
- {rtzip-0.5.3 → rtzip-0.6.0}/src/rtzip/wzaes_backends/pycryptodome_impl.py +3 -3
- rtzip-0.6.0/src/rtzip/wzaes_backends/tgcrypto_c_impl.py +33 -0
- {rtzip-0.5.3 → rtzip-0.6.0}/src/rtzip/zip_crypto.py +8 -2
- rtzip-0.5.3/PKG-INFO +0 -6
- rtzip-0.5.3/pyproject.toml +0 -19
- rtzip-0.5.3/pyproject.toml.orig +0 -21
- rtzip-0.5.3/src/rtzip/__init__.py +0 -6
- rtzip-0.5.3/src/rtzip/errors.py +0 -14
- rtzip-0.5.3/src/rtzip/path_methods.py +0 -17
- rtzip-0.5.3/src/rtzip/remotezip.py +0 -488
rtzip-0.6.0/PKG-INFO
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "uv_build"
|
|
3
|
+
requires = ["uv_build < 0.13"]
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rtzip"
|
|
7
|
+
version = "0.6.0"
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
dependencies = []
|
|
10
|
+
|
|
11
|
+
[dependency-groups]
|
|
12
|
+
dev = [
|
|
13
|
+
"colorama>=0.4.6",
|
|
14
|
+
"cryptography>=50.0.1",
|
|
15
|
+
"niquests>=3.21.0",
|
|
16
|
+
"pycryptodome>=3.23.0",
|
|
17
|
+
"pyrefly>=1.4.1",
|
|
18
|
+
"rich>=15.0.0",
|
|
19
|
+
"tgcrypto-c-lectr==1.3.2",
|
|
20
|
+
]
|
|
21
|
+
tgcrypto-c-lectr = ["tgcrypto-c-lectr==1.3.2"]
|
|
22
|
+
pycryptodome = ["pycryptodome>=3.23.0"]
|
|
23
|
+
|
|
24
|
+
[[tool.uv.index]]
|
|
25
|
+
name = "aliyun"
|
|
26
|
+
default = true
|
|
27
|
+
url = "https://mirrors.aliyun.com/pypi/simple"
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = 'uv_build'
|
|
3
|
+
requires = [
|
|
4
|
+
'uv_build < 0.13'
|
|
5
|
+
]
|
|
6
|
+
|
|
7
|
+
[project]
|
|
8
|
+
name = "rtzip"
|
|
9
|
+
version = "0.6.0"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
dependencies = []
|
|
12
|
+
|
|
13
|
+
[dependency-groups]
|
|
14
|
+
dev = [
|
|
15
|
+
"colorama>=0.4.6",
|
|
16
|
+
"cryptography>=50.0.1",
|
|
17
|
+
"niquests>=3.21.0",
|
|
18
|
+
"pycryptodome>=3.23.0",
|
|
19
|
+
"pyrefly>=1.4.1",
|
|
20
|
+
"rich>=15.0.0",
|
|
21
|
+
"tgcrypto-c-lectr==1.3.2"
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
tgcrypto-c-lectr = [
|
|
25
|
+
"tgcrypto-c-lectr==1.3.2"
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
pycryptodome = [
|
|
29
|
+
"pycryptodome>=3.23.0"
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[[tool.uv.index]]
|
|
33
|
+
name = 'aliyun'
|
|
34
|
+
default = true
|
|
35
|
+
url = "https://mirrors.aliyun.com/pypi/simple"
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from .algorithm_register import algorithm_handler, get_algorithm_handler
|
|
2
|
+
from .errors import *
|
|
3
|
+
from .models import EOCD, Zip64EOCD, CDEntry, LocalFileHeader
|
|
4
|
+
from .remotezip import RemoteZip, DataSource
|
|
5
|
+
|
|
6
|
+
from . import builtin_codecs
|
|
7
|
+
from . import builtin_sources
|
|
8
|
+
# from . import safety_wrappers
|
|
@@ -2,6 +2,7 @@ from typing import AsyncGenerator, Any, Protocol, runtime_checkable
|
|
|
2
2
|
|
|
3
3
|
from .errors import UnsupportedAlgorithmError
|
|
4
4
|
from .models import CDEntry, LocalFileHeader
|
|
5
|
+
from .context import Context
|
|
5
6
|
|
|
6
7
|
type IndexAcceptedTypes = bytes | bytearray | memoryview
|
|
7
8
|
|
|
@@ -13,7 +14,7 @@ class AlgorithmHandler(Protocol):
|
|
|
13
14
|
raw_data: AsyncGenerator[IndexAcceptedTypes, None],
|
|
14
15
|
entry: CDEntry,
|
|
15
16
|
header: LocalFileHeader,
|
|
16
|
-
context:
|
|
17
|
+
context: Context,
|
|
17
18
|
/
|
|
18
19
|
) -> AsyncGenerator[IndexAcceptedTypes, None]:
|
|
19
20
|
"""
|
|
@@ -65,7 +66,7 @@ def get_algorithm_handler(algorithm: int) -> AlgorithmHandler:
|
|
|
65
66
|
def get_algorithm_handler_safe(algorithm: int) -> AlgorithmHandler | None:
|
|
66
67
|
return algorithm_mapping.get(algorithm)
|
|
67
68
|
|
|
68
|
-
def
|
|
69
|
+
def is_algorithm_handler_available(algorithm: int) -> bool:
|
|
69
70
|
"""
|
|
70
71
|
判断某个压缩算法是否已有注册的包装器
|
|
71
72
|
|
|
@@ -4,7 +4,7 @@ from .algorithm_register import algorithm_handler
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
@algorithm_handler(0x0008)
|
|
7
|
-
async def deflate_wrapper(gen, entry, header,
|
|
7
|
+
async def deflate_wrapper(gen, entry, header, ctx):
|
|
8
8
|
import zlib
|
|
9
9
|
decompresser = zlib.decompressobj(-15) # 禁用 zlib 头部
|
|
10
10
|
async for chunk in gen:
|
|
@@ -15,13 +15,12 @@ async def deflate_wrapper(gen, entry, header, ext):
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
@algorithm_handler(0x0000)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
yield chunk
|
|
18
|
+
def raw_wrapper(gen, entry, header, ctx):
|
|
19
|
+
return gen
|
|
21
20
|
|
|
22
21
|
|
|
23
22
|
@algorithm_handler(0x000E)
|
|
24
|
-
async def lzma_wrapper(gen, entry, header,
|
|
23
|
+
async def lzma_wrapper(gen, entry, header, ctx):
|
|
25
24
|
import lzma
|
|
26
25
|
decompressor = lzma.LZMADecompressor()
|
|
27
26
|
|
|
@@ -40,8 +39,9 @@ async def lzma_wrapper(gen, entry, header, ext):
|
|
|
40
39
|
f' – possibly concatenated streams or extra metadata'
|
|
41
40
|
)
|
|
42
41
|
|
|
42
|
+
|
|
43
43
|
@algorithm_handler(0x000C)
|
|
44
|
-
async def bz2_wrapper(gen, entry, header,
|
|
44
|
+
async def bz2_wrapper(gen, entry, header, ctx):
|
|
45
45
|
import bz2
|
|
46
46
|
decompressor = bz2.BZ2Decompressor()
|
|
47
47
|
|
|
@@ -66,7 +66,7 @@ async def bz2_wrapper(gen, entry, header, ext):
|
|
|
66
66
|
|
|
67
67
|
if sys.version_info >= (3, 14):
|
|
68
68
|
@algorithm_handler(0x000D)
|
|
69
|
-
async def zstd_wrapper(gen, entry, header,
|
|
69
|
+
async def zstd_wrapper(gen, entry, header, ctx): # Not Tested
|
|
70
70
|
import warnings
|
|
71
71
|
warnings.warn("ZStandard support is experimental and has not been tested")
|
|
72
72
|
|
|
@@ -87,4 +87,4 @@ if sys.version_info >= (3, 14):
|
|
|
87
87
|
raise ValueError(
|
|
88
88
|
f"ZStandard stream contains {len(decompressor.unused_data)} bytes of trailing data after the end marker – "
|
|
89
89
|
f"possibly concatenated streams or extra metadata"
|
|
90
|
-
)
|
|
90
|
+
)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from .errors import NotEnoughDataError
|
|
2
|
+
from .remotezip import DataSource
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LocalFileSourceSync(DataSource):
|
|
7
|
+
def __init__(self, file):
|
|
8
|
+
self.file = file
|
|
9
|
+
self.fp = open(file, 'rb')
|
|
10
|
+
self.chunk_size = 1024 * 1024
|
|
11
|
+
|
|
12
|
+
async def read_range_stream(self, offset, length):
|
|
13
|
+
self.fp.seek(offset)
|
|
14
|
+
size = 0
|
|
15
|
+
while True:
|
|
16
|
+
elapsed = length - size
|
|
17
|
+
|
|
18
|
+
if elapsed < self.chunk_size:
|
|
19
|
+
data = self.fp.read(elapsed)
|
|
20
|
+
|
|
21
|
+
if len(data) < elapsed:
|
|
22
|
+
raise NotEnoughDataError(length, size + len(data))
|
|
23
|
+
|
|
24
|
+
yield data
|
|
25
|
+
return
|
|
26
|
+
|
|
27
|
+
else:
|
|
28
|
+
data = self.fp.read(self.chunk_size)
|
|
29
|
+
|
|
30
|
+
if len(data) < self.chunk_size:
|
|
31
|
+
raise NotEnoughDataError(length, size + len(data))
|
|
32
|
+
|
|
33
|
+
yield data
|
|
34
|
+
|
|
35
|
+
size += len(data)
|
|
36
|
+
|
|
37
|
+
async def read_range(self, offset, length):
|
|
38
|
+
self.fp.seek(offset)
|
|
39
|
+
data = self.fp.read(length)
|
|
40
|
+
if len(data) < length:
|
|
41
|
+
raise NotEnoughDataError(length, len(data))
|
|
42
|
+
return data
|
|
43
|
+
|
|
44
|
+
async def get_total_size(self):
|
|
45
|
+
return os.fstat(self.fp.fileno()).st_size
|
|
46
|
+
|
|
47
|
+
async def close(self):
|
|
48
|
+
self.fp.close()
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _merge(ctx: 'Context', other: 'Context') -> 'Context':
|
|
5
|
+
dct = {}
|
|
6
|
+
|
|
7
|
+
for f in dataclasses.fields(ctx):
|
|
8
|
+
if f.name == 'extras':
|
|
9
|
+
continue
|
|
10
|
+
|
|
11
|
+
x = getattr(ctx, f.name)
|
|
12
|
+
if f.default is not dataclasses.MISSING and f.default is x:
|
|
13
|
+
continue
|
|
14
|
+
|
|
15
|
+
if f.default_factory is not dataclasses.MISSING and not bool(x):
|
|
16
|
+
continue
|
|
17
|
+
|
|
18
|
+
dct[f.name] = x
|
|
19
|
+
|
|
20
|
+
dct['extras'] = dict(ctx.extras)
|
|
21
|
+
dct['extras'].update(other.extras)
|
|
22
|
+
|
|
23
|
+
return dataclasses.replace(ctx, **dct)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclasses.dataclass(slots=True)
|
|
27
|
+
class Context:
|
|
28
|
+
pwd: bytes | None = None
|
|
29
|
+
|
|
30
|
+
# EOCD Scan
|
|
31
|
+
eocd_scan_initial_chunk_size: int = 256
|
|
32
|
+
eocd_scan_max_retry: int = 4
|
|
33
|
+
eocd_scan_rechunk_factor: int | float = 1
|
|
34
|
+
eocd_scan_use_cache: bool = True
|
|
35
|
+
|
|
36
|
+
# Extras
|
|
37
|
+
extras: dict = dataclasses.field(default_factory=dict)
|
|
38
|
+
|
|
39
|
+
# Helpers
|
|
40
|
+
|
|
41
|
+
def __setattr__(self, name, value):
|
|
42
|
+
try:
|
|
43
|
+
super(Context, self).__setattr__(name, value)
|
|
44
|
+
except AttributeError:
|
|
45
|
+
self.extras[name] = value
|
|
46
|
+
|
|
47
|
+
def __getattr__(self, name):
|
|
48
|
+
if name in self.extras:
|
|
49
|
+
return self.extras[name]
|
|
50
|
+
else:
|
|
51
|
+
raise AttributeError(name)
|
|
52
|
+
|
|
53
|
+
def __or__(self, other):
|
|
54
|
+
if isinstance(other, Context):
|
|
55
|
+
return _merge(self, other)
|
|
56
|
+
|
|
57
|
+
elif isinstance(other, dict):
|
|
58
|
+
other_extras = {
|
|
59
|
+
k: v
|
|
60
|
+
for k, v in other.items()
|
|
61
|
+
if k not in self.extras
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
extras = dict(self.extras)
|
|
65
|
+
extras.update(other_extras)
|
|
66
|
+
|
|
67
|
+
other_normals = {
|
|
68
|
+
k: v
|
|
69
|
+
for k, v in other.items()
|
|
70
|
+
if k in self.extras
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return dataclasses.replace(
|
|
74
|
+
self, **other_normals, extras=extras
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
else:
|
|
78
|
+
return NotImplemented
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
_context_fields = set(dataclasses.fields(Context))
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
class RtZipError(Exception):
|
|
2
|
+
...
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class UnsupportedAlgorithmError(RtZipError):
|
|
6
|
+
...
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class WrongPasswordError(RtZipError):
|
|
10
|
+
...
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BadZipError(RtZipError):
|
|
14
|
+
...
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class WinzipAESError(RtZipError):
|
|
18
|
+
...
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class BadWinzipAESData(WinzipAESError):
|
|
22
|
+
...
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class HMACError(WinzipAESError):
|
|
26
|
+
...
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class NotEnoughDataError(BadZipError):
|
|
30
|
+
def __init__(self, require, available):
|
|
31
|
+
super().__init__(require, available)
|
|
32
|
+
self.require = require
|
|
33
|
+
self.available = available
|
|
34
|
+
|
|
35
|
+
def __str__(self):
|
|
36
|
+
return f"require {self.require} byte(s) but only {self.available} available"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class InvalidOffsetError(BadZipError):
|
|
40
|
+
def __init__(self, start, length, available):
|
|
41
|
+
super().__init__(start, length, available)
|
|
42
|
+
self.start = start
|
|
43
|
+
self.length = length
|
|
44
|
+
self.available = available
|
|
45
|
+
|
|
46
|
+
def __str__(self):
|
|
47
|
+
return (
|
|
48
|
+
f"invalid offset {self.start} + {self.length} "
|
|
49
|
+
f"(available: {self.available})"
|
|
50
|
+
)
|