rtzip 0.2.0__tar.gz → 0.2.1__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.
@@ -1,5 +1,5 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rtzip
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Requires-Python: >=3.12
5
5
  Requires-Dist: obstruct==0.5.2
rtzip-0.2.1/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Rtzip
2
+ _一个以最小的数据量提取远程 zip 文件内容的库_
3
+
4
+
5
+ ## 特点
6
+ - 不与任何 HTTP 库耦合
7
+ - 依赖简单(只有用于解析二进制结构的 `obstruct`)
8
+ - 兼容标准 zip 格式、ZIP64 扩展
9
+
10
+
11
+ ## 安装
12
+ ```commandline
13
+ pip install rtzip
14
+ ```
15
+
16
+ > [!TIP]
17
+ > 本库无计划新功能,若是让自行添加功能,可克隆仓库并重写 `rtzip/remotezip.py` 下的代码
18
+ > 大多数库的功能已完成并可以直接投入使用
@@ -1,7 +1,12 @@
1
1
  [project]
2
2
  name = "rtzip"
3
- version = "0.2.0"
3
+ version = "0.2.1"
4
4
  requires-python = ">=3.12"
5
5
  dependencies = [
6
6
  "obstruct==0.5.2",
7
7
  ]
8
+
9
+ [dependency-groups]
10
+ dev = [
11
+ "niquests>=3.21.0",
12
+ ]
@@ -1,3 +1,3 @@
1
1
  from .models import EOCD, Zip64EOCD, CDEntry
2
2
  from .errors import *
3
- from .remotezip import RemoteZip
3
+ from .remotezip import RemoteZip, DataSourceCallbacks
@@ -1,5 +1,5 @@
1
1
  EOCD_SIGNATURE = b'PK\x05\x06'
2
- CDENTITY_SIGNATURE = b'PK\x01\x02'
2
+ CDENTRY_SIGNATURE = b'PK\x01\x02'
3
3
  LFH_SIGNATURE = b'PK\x03\x04'
4
4
  ZIP64_EOCD_LOCATOR_SIGNATURE = b'PK\x06\x07'
5
5
  ZIP64_EOCD_SIGNATURE = b'PK\x06\x06'
@@ -5,6 +5,8 @@ from typing import Annotated, Self
5
5
 
6
6
  import obstruct as obs
7
7
 
8
+ from rtzip.utils import exactly_get_slice
9
+
8
10
  EXTRA_FIELD_HEADER = struct.Struct("<HH")
9
11
 
10
12
 
@@ -245,13 +247,13 @@ class CDEntry(obs.Struct):
245
247
  filename_len, extra_len, comment_len = self.filename_len, self.extra_len, self.comment_len
246
248
 
247
249
  pos = offset + header_size
248
- filename = bytes(buffer[pos: pos + filename_len])
250
+ filename = bytes(exactly_get_slice(buffer, pos, filename_len))
249
251
  pos += filename_len
250
252
 
251
- extra_attrs = buffer[pos: pos + extra_len].tobytes()
253
+ extra_attrs = bytes(exactly_get_slice(buffer, pos, extra_len))
252
254
  pos += extra_len
253
255
 
254
- comment = buffer[pos: pos + comment_len].tobytes()
256
+ comment = bytes(exactly_get_slice(buffer, pos, comment_len))
255
257
  pos += comment_len
256
258
 
257
259
  self.filename = filename
@@ -1,11 +1,11 @@
1
1
  import struct
2
2
  from typing import AsyncGenerator
3
3
 
4
+ from .const import *
4
5
  from .errors import UnsupportedAlgorithmError, UnsupportedCryptoError, UnsupportedDataDescriptorError
5
6
  from .models import EOCD, Zip64EOCD, CDEntry, LocalFileHeader
6
7
  from .utils import deflate_wrapper, single_chunk_wrapper
7
8
 
8
- from .const import *
9
9
 
10
10
 
11
11
  class DataSourceCallbacks:
@@ -18,7 +18,7 @@ class DataSourceCallbacks:
18
18
  :return: bytes | bytearray | memoryview
19
19
  """
20
20
 
21
- def read_range_stream(self, offset, length) -> AsyncGenerator[bytes | bytearray | memoryview]:
21
+ def read_range_stream(self, offset, length) -> AsyncGenerator[bytes | bytearray | memoryview, None]:
22
22
  """
23
23
  流式读取时指定的一段数据
24
24
 
@@ -48,6 +48,7 @@ class RemoteZip:
48
48
  :ivar file_mapping: 构建的文件映射表,可能为 None
49
49
  :ivar is_zip64: 此 zip 文件是否启动了 zip64 扩展,默认为 False
50
50
  """
51
+
51
52
  async def _read_range(self, offset, length):
52
53
  return await self.source.read_range(offset, length)
53
54
 
@@ -78,6 +79,7 @@ class RemoteZip:
78
79
  chunk_size = initial_chunk
79
80
 
80
81
  last_start = await self._get_total_size()
82
+ # print("File Size:", last_start)
81
83
  cnt = 0
82
84
 
83
85
  buffer = b''
@@ -174,6 +176,8 @@ class RemoteZip:
174
176
  nonlocal buffer
175
177
  entry, nbytes = CDEntry.from_buffer(buffer)
176
178
 
179
+ # print(f'{len(files) + 1:03} Get Entry', entry)
180
+
177
181
  if self.is_zip64:
178
182
  entry.fix_by_zip64()
179
183
 
@@ -181,13 +185,14 @@ class RemoteZip:
181
185
  buffer = buffer[nbytes + 4:]
182
186
 
183
187
  async for chunk in self._read_range_stream(eocd.cd_offset, eocd.cd_size):
188
+ # print("Read Chunk", len(chunk))
184
189
  # print(chunk)
185
190
  buffer.extend(chunk)
186
191
 
187
- while buffer and buffer[:4] == CDENTITY_SIGNATURE:
192
+ while buffer and buffer[:4] == CDENTRY_SIGNATURE:
188
193
  try:
189
194
  consume_entry()
190
- except struct.error:
195
+ except (struct.error, AssertionError):
191
196
  continue
192
197
 
193
198
  if buffer and not ignore_extra:
@@ -13,3 +13,9 @@ async def deflate_wrapper(gen):
13
13
 
14
14
  async def single_chunk_wrapper(data: Buffer):
15
15
  yield data
16
+
17
+
18
+ def exactly_get_slice(__obj, __offset, __length):
19
+ result = __obj[__offset:__offset + __length]
20
+ assert len(result) == __length
21
+ return result
@@ -1,5 +1,5 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rtzip
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Requires-Python: >=3.12
5
5
  Requires-Dist: obstruct==0.5.2
@@ -1,3 +1,4 @@
1
+ README.md
1
2
  pyproject.toml
2
3
  src/rtzip/__init__.py
3
4
  src/rtzip/const.py
File without changes
File without changes