danrossiter/binary-compound-file
Composer 安装命令:
composer require danrossiter/binary-compound-file
包简介
Pure PHP library for reading Microsoft Compound Binary File Format (CFBF/OLE) documents. Supports both little-endian and big-endian files with stream wrapper interface.
README 文档
README
A modern, high-performance PHP library for reading Microsoft Compound Binary File Format (CFBF/OLE) documents.
The compound file format goes by several names: "Object Linking and Embedding (OLE) Compound File (CF)", "Compound Binary File", "Compound Document File", and "OLE2." This format creates a filesystem within a file and is used by .doc, .xls, .ppt, and other Microsoft Office files.
This library was designed to address the lack of quality options for parsing compound file binary format natively in PHP. Existing implementations were either slow or made assumptions that didn't conform to the specification. This library prioritizes speed and standards compliance, ensuring broad compatibility and excellent performance at scale.
Features
- Full CFBF Spec Compliance: Properly handles FAT, DIFAT, and mini-FAT chain structures
- Stream Wrapper: PHP stream wrapper for seamless resource-based access
- Big-Endian Support: Correctly parses both little-endian (standard) and big-endian compound files
- 64-bit File Support: Handles large files with 64-bit ulSize fields in version 4+ files
- UTF-16 Encoding: Properly converts UTF-16LE/BE stream names to UTF-8
- PHP 7.4+ Compatible: Works with PHP 7.4, 8.0, 8.1, 8.2, and 8.3+
- PSR-4 Autoloading: Composer-based installation and autoloading
- Fully Tested: Comprehensive PHPUnit test suite with unit and integration tests
Installation
Install via Composer:
composer require danrossiter/binary-compound-file
Usage
To use the library, there are two options depending on your specific needs: a stream wrapper or raw access to the parsed file fields. Below, both of these approaches are demonstrated. The stream option provides a nice abstraction if you have functions that accept a PHP resource that you want to pass in a single stream from a compound file.
Basic Usage
use DanRossiter\BinaryCompoundFile\CompoundFile; use DanRossiter\BinaryCompoundFile\StorageType; // Open the compound file $handle = fopen('document.doc', 'rb'); $cfb = new CompoundFile($handle); // Extract a specific stream $wordDocStream = $cfb->getDirectory('WordDocument'); $content = $cfb->getStream($wordDocStream); // Iterate through all streams $directories = $cfb->getDirectories(); foreach ($directories as $name => $dir) { if ($dir->getMse() === StorageType::STREAM) { $streamContent = $cfb->getStream($dir); echo "Stream: $name, Size: " . strlen($streamContent) . " bytes\n"; } } fclose($handle);
Stream Wrapper
For seamless integration with PHP's stream functions:
use DanRossiter\BinaryCompoundFile\CompoundFileStream; // Register the stream wrapper stream_wrapper_register('cfbf', CompoundFileStream::class); // Access streams using the cfbf:// protocol $handle = fopen('cfbf://document.doc#WordDocument', 'rb'); $content = stream_get_contents($handle); fclose($handle);
Examples
See the examples/ directory for complete working examples:
extract-all-streams.php- Extract all streams to individual filesstream-wrapper.php- Using the cfbf:// stream wrapperinspect-structure.php- Analyze file structure and directory treecreate-bigendian-test.php- Generate big-endian test files
Requirements
- PHP 7.4 or higher
- No external dependencies (pure PHP implementation)
Development
Running Tests
composer test
Code Quality
# Static analysis composer phpstan # Code style checking composer cs-check # Code style fixing composer cs-fix
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
Security
If you discover a security vulnerability, please see SECURITY.md for reporting procedures.
Roadmap
- Write support (currently read-only)
- Additional format validation and corruption detection
- Convenience classes:
WordFile,ExcelFile,PowerPointFilewith format-specific methods
License
GPL-3.0-or-later. See LICENSE.txt for details.
Credits
Created by Dan Rossiter
Acknowledgments
danrossiter/binary-compound-file 适用场景与选型建议
danrossiter/binary-compound-file 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xls」 「doc」 「binary」 「ppt」 「ole」 「structured-storage」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 danrossiter/binary-compound-file 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 danrossiter/binary-compound-file 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 danrossiter/binary-compound-file 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules.
A tool for display docs or api docs to others
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
统计信息
- 总下载量: 22
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2025-12-15