Compound File Binary File Format extractor for JavaScript
npx @tessl/cli install tessl/npm-cfb@1.2.0A pure JavaScript implementation for parsing and manipulating Compound File Binary (CFB) format files, commonly used by Microsoft Office applications and other software that store data in structured container formats.
npm install cfb// CommonJS (Node.js)
const CFB = require('cfb');
// ES Modules (if using bundler with compatibility)
import CFB from 'cfb';// Read a CFB file
const fs = require('fs');
const CFB = require('cfb');
// Read from file
const cfb = CFB.read('document.xls', {type: 'file'});
// Find a specific stream/storage
const workbook = CFB.find(cfb, 'Workbook');
if (workbook) {
console.log('Found workbook with', workbook.content.length, 'bytes');
}
// Create new CFB container
const newCfb = CFB.utils.cfb_new();
CFB.utils.cfb_add(newCfb, 'MyData', Buffer.from('Hello World'));
// Write to file
CFB.writeFile(newCfb, 'output.cfb');The CFB library provides three main layers of functionality:
The main CFB object provides essential functions for working with CFB files:
// Version information
const version: string;
// Reading CFB files
function parse(file: CFB$Blob, options?: CFB$ParsingOptions): CFB$Container;
function read(input: CFB$Blob | string, options?: CFB$ParsingOptions): CFB$Container;
// Finding content
function find(cfb: CFB$Container, path: string): CFB$Entry | null;
// Writing CFB files
function write(cfb: CFB$Container, options?: CFB$WritingOptions): any;
function writeFile(cfb: CFB$Container, filename: string, options?: CFB$WritingOptions): void;
// Utility functions
const utils: CFB$Utils;Core functionality for reading and writing CFB format files with support for multiple input/output formats. Handles various input types including files, buffers, arrays, and strings.
// Parse raw binary data into CFB container
function parse(file: CFB$Blob, options?: CFB$ParsingOptions): CFB$Container;
// Read from various sources (files, buffers, strings)
function read(input: CFB$Blob | string, options?: CFB$ParsingOptions): CFB$Container;
// Write CFB container to various formats
function write(cfb: CFB$Container, options?: CFB$WritingOptions): any;
function writeFile(cfb: CFB$Container, filename: string, options?: CFB$WritingOptions): void;Tools for finding and accessing files and directories within CFB containers. Provides case-insensitive search and direct access to container structure.
// Find entry by path or filename (case-insensitive)
function find(cfb: CFB$Container, path: string): CFB$Entry | null;
// Direct access to container structure
interface CFB$Container {
FullPaths: string[]; // All file/storage paths
FileIndex: CFB$Entry[]; // Corresponding entry objects
}
interface CFB$Entry {
name: string; // Entry name
type: number; // Entry type (stream, storage, etc.)
content: CFB$Blob; // Raw content data
size: number; // Content size
}Utilities for creating, modifying, and managing CFB container structures. Enables full CRUD operations on CFB containers with optimization support.
// Container management utilities
interface CFB$Utils {
cfb_new(opts?: any): CFB$Container;
cfb_add(cfb: CFB$Container, name: string, content: any, opts?: CFB$AddOpts): CFB$Entry;
cfb_del(cfb: CFB$Container, name: string): boolean;
cfb_mov(cfb: CFB$Container, old_name: string, new_name: string): boolean;
cfb_gc(cfb: CFB$Container): void;
}
interface CFB$AddOpts {
unsafe?: boolean; // Skip safety checks for bulk operations
}The library includes comprehensive TypeScript definitions for all interfaces and options:
// Core container and entry types
interface CFB$Container {
FullPaths: string[]; // Array of all file/storage paths
FileIndex: CFB$Entry[]; // Array of corresponding entries
raw?: { // Raw binary data (when parsed with raw: true)
header: CFB$Blob;
sectors: CFB$Blob[];
};
}
interface CFB$Entry {
name: string; // Case-sensitive internal name
type: number; // CFB type (see CFB$EntryType)
content: CFB$Blob; // Raw content data
ct?: Date; // Creation time
mt?: Date; // Modification time
color: number; // Red/Black tree color
clsid: string; // Class ID as hex string
state: number; // User-defined state bits
start: number; // Starting sector
size: number; // Data size in bytes
storage?: string; // Storage location
ctype?: string; // Content type
}
// Data types and options
type CFB$Blob = number[] | Uint8Array;
interface CFB$ParsingOptions {
type?: 'base64' | 'binary' | 'buffer' | 'file' | 'array';
WTF?: boolean; // Throw errors on unknown features
raw?: boolean; // Include raw data in output
}
interface CFB$WritingOptions {
type?: 'base64' | 'binary' | 'buffer' | 'file' | 'array';
WTF?: boolean;
fileType?: 'cfb' | 'zip' | 'mad'; // Output file format
root?: string; // Override root entry name
compression?: boolean; // Enable compression (ZIP only)
}
enum CFB$EntryType {
unknown = 0,
storage = 1, // Directory/folder
stream = 2, // File/data stream
lockbytes = 3,
property = 4,
root = 5
}// Enable strict error handling
const cfb = CFB.read(data, { WTF: true });
// Handle parsing failures
try {
const cfb = CFB.read(corruptedData);
} catch (error) {
console.error('Failed to parse CFB file:', error.message);
}