diff --git a/src/docx/opc/phys_pkg.py b/src/docx/opc/phys_pkg.py index 5ec32237c..ee31ac79d 100644 --- a/src/docx/opc/phys_pkg.py +++ b/src/docx/opc/phys_pkg.py @@ -1,7 +1,7 @@ """Provides a general interface to a `physical` OPC package, such as a zip file.""" import os -from zipfile import ZIP_DEFLATED, ZipFile, is_zipfile +from zipfile import ZIP_DEFLATED, BadZipFile, ZipFile, is_zipfile from docx.opc.exceptions import PackageNotFoundError from docx.opc.packuri import CONTENT_TYPES_URI @@ -73,7 +73,12 @@ class _ZipPkgReader(PhysPkgReader): def __init__(self, pkg_file): super(_ZipPkgReader, self).__init__() - self._zipf = ZipFile(pkg_file, "r") + try: + self._zipf = ZipFile(pkg_file, "r") + except BadZipFile: + raise PackageNotFoundError( + "Package not found or not a valid OPC package file" + ) def blob_for(self, pack_uri): """Return blob corresponding to `pack_uri`. diff --git a/tests/opc/test_phys_pkg.py b/tests/opc/test_phys_pkg.py index 6de0d868b..f02b10f78 100644 --- a/tests/opc/test_phys_pkg.py +++ b/tests/opc/test_phys_pkg.py @@ -68,6 +68,10 @@ def it_raises_when_pkg_path_is_not_a_package(self): with pytest.raises(PackageNotFoundError): PhysPkgReader("foobar") + def it_raises_when_pkg_stream_is_not_a_zip(self): + with pytest.raises(PackageNotFoundError): + PhysPkgReader(io.BytesIO(b"not a zip")) + class DescribeZipPkgReader: def it_is_used_by_PhysPkgReader_when_pkg_is_a_zip(self):