Skip to content

Commit 7ffa8dc

Browse files
simplify
1 parent d384268 commit 7ffa8dc

File tree

2 files changed

+22
-63
lines changed

2 files changed

+22
-63
lines changed

src/launchpad/artifacts/providers/zip_provider.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import zipfile
22

33
from pathlib import Path
4-
from typing import List, Set
4+
from typing import List
5+
6+
import zipfile_zstd # noqa: F401 - Import registers zstd handler with zipfile
57

68
from launchpad.utils.file_utils import cleanup_directory, create_temp_directory
79
from launchpad.utils.logging import get_logger
@@ -11,9 +13,6 @@
1113
DEFAULT_MAX_FILE_COUNT = 100000
1214
DEFAULT_MAX_UNCOMPRESSED_SIZE = 10 * 1024 * 1024 * 1024
1315

14-
# Compression method constants
15-
COMPRESSION_ZSTD = 93 # Zstandard compression method
16-
1716

1817
class UnreasonableZipError(ValueError):
1918
"""Raised when a zip file exceeds reasonable limits."""
@@ -99,39 +98,12 @@ def extract_to_temp_directory(self) -> Path:
9998

10099
return temp_dir
101100

102-
def _detect_compression_methods(self, zip_path: str) -> Set[int]:
103-
"""Detect compression methods used in the zip file.
104-
105-
Args:
106-
zip_path: Path to the zip file
107-
108-
Returns:
109-
Set of compression method integers used in the zip file
110-
"""
111-
with zipfile.ZipFile(zip_path, "r") as zf:
112-
return {info.compress_type for info in zf.infolist()}
113-
114101
def _safe_extract(self, zip_path: str, extract_path: str):
115102
"""Extract the zip contents to a temporary directory, ensuring that the paths are safe from path traversal attacks.
116103
117104
Supports both standard compression methods and Zstandard compression.
118105
"""
119106
base_dir = Path(extract_path)
120-
121-
# Detect if zstandard compression is used
122-
compression_methods = self._detect_compression_methods(zip_path)
123-
uses_zstd = COMPRESSION_ZSTD in compression_methods
124-
125-
if uses_zstd:
126-
logger.debug("Detected Zstandard compression in zip file")
127-
try:
128-
import zipfile_zstd # noqa: F401
129-
except ImportError:
130-
raise RuntimeError(
131-
"Zstandard-compressed zip file detected, but zipfile-zstd package is not installed. "
132-
"Install it with: pip install zipfile-zstd"
133-
)
134-
135107
with zipfile.ZipFile(zip_path, "r") as zip_ref:
136108
check_reasonable_zip(zip_ref)
137109
for member in zip_ref.namelist():

tests/unit/artifacts/providers/test_zip_provider.py

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import pytest
77

88
from launchpad.artifacts.providers.zip_provider import (
9-
COMPRESSION_ZSTD,
109
UnreasonableZipError,
1110
UnsafePathError,
1211
ZipProvider,
@@ -89,37 +88,6 @@ def test_invalid_zip_file(self) -> None:
8988
with pytest.raises(zipfile.BadZipFile):
9089
provider.extract_to_temp_directory()
9190

92-
def test_detect_compression_methods(self, hackernews_xcarchive: Path) -> None:
93-
provider = ZipProvider(hackernews_xcarchive)
94-
methods = provider._detect_compression_methods(str(hackernews_xcarchive))
95-
96-
# Verify detection works and standard zips use deflate compression, not zstd
97-
assert zipfile.ZIP_DEFLATED in methods
98-
assert COMPRESSION_ZSTD not in methods
99-
100-
def test_extract_zstd_zip(self) -> None:
101-
"""Test that zstd-compressed zips can be extracted when zipfile-zstd is available."""
102-
try:
103-
import zipfile_zstd # noqa: F401
104-
except ImportError:
105-
pytest.skip("zipfile-zstd not installed")
106-
107-
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as temp_file:
108-
temp_path = Path(temp_file.name)
109-
110-
with zipfile.ZipFile(temp_path, "w") as zf:
111-
zf.writestr("test.txt", "content", compress_type=COMPRESSION_ZSTD)
112-
113-
try:
114-
provider = ZipProvider(temp_path)
115-
temp_dir = provider.extract_to_temp_directory()
116-
117-
assert temp_dir.exists()
118-
assert (temp_dir / "test.txt").exists()
119-
assert (temp_dir / "test.txt").read_text() == "content"
120-
finally:
121-
temp_path.unlink(missing_ok=True)
122-
12391

12492
class TestIsSafePath:
12593
def test_valid_paths(self) -> None:
@@ -157,3 +125,22 @@ def test_max_file_size(self, hackernews_xcarchive: Path) -> None:
157125
# iOS fixture is ~32MB uncompressed, so limit of 10MB should fail
158126
with pytest.raises(UnreasonableZipError, match="exceeding the limit of 10.0MB"):
159127
check_reasonable_zip(zf, max_uncompressed_size=10 * 1024 * 1024)
128+
129+
def test_extract_zstd_zip(self) -> None:
130+
"""Test that zstd-compressed zips can be extracted."""
131+
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as temp_file:
132+
temp_path = Path(temp_file.name)
133+
134+
# Create a zstd-compressed zip (compression method 93)
135+
with zipfile.ZipFile(temp_path, "w") as zf:
136+
zf.writestr("test.txt", "content", compress_type=93)
137+
138+
try:
139+
provider = ZipProvider(temp_path)
140+
temp_dir = provider.extract_to_temp_directory()
141+
142+
assert temp_dir.exists()
143+
assert (temp_dir / "test.txt").exists()
144+
assert (temp_dir / "test.txt").read_text() == "content"
145+
finally:
146+
temp_path.unlink(missing_ok=True)

0 commit comments

Comments
 (0)