diff --git a/docx/opc/phys_pkg.py b/docx/opc/phys_pkg.py index c86a51994..8db7e5cfe 100644 --- a/docx/opc/phys_pkg.py +++ b/docx/opc/phys_pkg.py @@ -8,7 +8,7 @@ import os -from zipfile import ZipFile, is_zipfile, ZIP_DEFLATED +from zipfile import ZipFile, ZipInfo, is_zipfile, ZIP_DEFLATED from .compat import is_string from .exceptions import PackageNotFoundError @@ -152,4 +152,5 @@ def write(self, pack_uri, blob): Write *blob* to this zip package with the membername corresponding to *pack_uri*. """ - self._zipf.writestr(pack_uri.membername, blob) + zinfo = ZipInfo(filename=pack_uri.membername, date_time=(1980, 1, 1, 0, 0, 0)) + self._zipf.writestr(zinfo, blob) diff --git a/docx/opc/pkgwriter.py b/docx/opc/pkgwriter.py index fccda6cd8..b557b53ea 100644 --- a/docx/opc/pkgwriter.py +++ b/docx/opc/pkgwriter.py @@ -30,9 +30,10 @@ def write(pkg_file, pkg_rels, parts): content types of the parts. """ phys_writer = PhysPkgWriter(pkg_file) - PackageWriter._write_content_types_stream(phys_writer, parts) + sorted_parts = sorted(parts, key=lambda p: p.partname) + PackageWriter._write_content_types_stream(phys_writer, sorted_parts) PackageWriter._write_pkg_rels(phys_writer, pkg_rels) - PackageWriter._write_parts(phys_writer, parts) + PackageWriter._write_parts(phys_writer, sorted_parts) phys_writer.close() @staticmethod diff --git a/docx/opc/rel.py b/docx/opc/rel.py index 7dba2af8e..cd719d5fd 100644 --- a/docx/opc/rel.py +++ b/docx/opc/rel.py @@ -78,7 +78,7 @@ def xml(self): as a .rels file in an OPC package. """ rels_elm = CT_Relationships.new() - for rel in self.values(): + for rel in sorted(self.values(), key=lambda r: r._rId): rels_elm.add_rel( rel.rId, rel.reltype, rel.target_ref, rel.is_external )