diff --git a/src/hwpx/opc/package.py b/src/hwpx/opc/package.py index 05d3e55..25cad43 100644 --- a/src/hwpx/opc/package.py +++ b/src/hwpx/opc/package.py @@ -490,16 +490,25 @@ def _save_to_zip(self, pkg_file: str | Path | BinaryIO) -> None: self._version.mark_clean() self._validate_structure() with ZipFile(pkg_file, "w") as zf: - self._write_mimetype(zf) - for name in sorted(self._files): - if name == self.MIMETYPE_PATH: - continue - data = self._files[name] - info = ZipInfo(name) - info.compress_type = ZIP_DEFLATED - zf.writestr(info, data) + self._write_archive(zf) + + def _write_archive(self, zf: ZipFile) -> None: + self._write_mimetype(zf) + for name in sorted(self._files): + if name == self.MIMETYPE_PATH: + continue + self._write_zip_entry(zf, name, self._files[name], ZIP_DEFLATED) + + @staticmethod + def _write_zip_entry(zf: ZipFile, path: str, payload: bytes, compress_type: int) -> None: + info = ZipInfo(path) + info.compress_type = compress_type + zf.writestr(info, payload) def _write_mimetype(self, zf: ZipFile) -> None: - info = ZipInfo(self.MIMETYPE_PATH) - info.compress_type = ZIP_STORED - zf.writestr(info, self._files[self.MIMETYPE_PATH]) + self._write_zip_entry( + zf, + self.MIMETYPE_PATH, + self._files[self.MIMETYPE_PATH], + ZIP_STORED, + ) diff --git a/tests/test_opc_package.py b/tests/test_opc_package.py index b1ccdbc..6425166 100644 --- a/tests/test_opc_package.py +++ b/tests/test_opc_package.py @@ -66,3 +66,16 @@ def test_missing_required_files_raise_structure_error() -> None: with pytest.raises(HwpxStructureError): HwpxPackage.open(_build_package(include_version=False)) + + +def test_save_preserves_expected_compress_type_per_entry() -> None: + package = HwpxPackage.open(_build_package()) + + output = package.save() + with ZipFile(io.BytesIO(output), "r") as archive: + infos = archive.infolist() + + assert infos[0].filename == "mimetype" + assert infos[0].compress_type == ZIP_STORED + for info in infos[1:]: + assert info.compress_type == ZIP_DEFLATED