Skip to content

Add OLEWrite, the counterpart of OLERead - #65

Merged
Progi1984 merged 1 commit into
PHPOffice:masterfrom
businessarshgoyal:devin/1788631917-ole-write
Sep 5, 2026
Merged

Add OLEWrite, the counterpart of OLERead#65
Progi1984 merged 1 commit into
PHPOffice:masterfrom
businessarshgoyal:devin/1788631917-ole-write

Conversation

@businessarshgoyal

Copy link
Copy Markdown
Contributor

Summary

Microsoft\OLERead can read an OLE compound document, but nothing here can write one, so every PHPOffice project that emits a binary Microsoft format has to carry its own compound-file writer. This adds Microsoft\OLEWrite, a minimal version 3 (512 byte sectors) compound file writer that is the direct counterpart of OLERead:

$content = (new OLEWrite())
    ->addStream('WordDocument', $wordDocument)
    ->addStream(chr(5) . 'SummaryInformation', $summary)
    ->write();

It handles what a real compound file needs and nothing more: streams under 4096 bytes go into the mini stream (with its own mini FAT), larger ones get whole sectors, names are sorted with the compound-file ordering (length first, then case-insensitive), and the directory entries are emitted as a balanced tree so readers that walk the tree — including OLERead — find every stream. It throws when a name is empty or longer than 31 characters, and when the document would need more than the 109 FAT sector numbers the header can reference (no DIFAT sectors are written).

Tests round-trip documents through OLERead (small, large and empty streams) and cover the rejected cases. phpunit, phpstan (level 7) and phpmd pass locally.

Context: PHPOffice/PHPWord#2929 adds a .doc writer and currently carries this class as PhpWord\Shared\OLEWrite; per the maintainer's request there it belongs in Common first, after which PHPWord will depend on it instead of shipping its own copy.

Written by Devin

Co-Authored-By: business.arshgoyal@gmail.com <business.arshgoyal@gmail.com>
@Progi1984
Progi1984 merged commit 0982b6b into PHPOffice:master Sep 5, 2026
23 checks passed
@Progi1984 Progi1984 added this to the 1.0.6 milestone Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants