CORE
Archive
Read and write OOXML ZIP archives, parse relationships
OOXML files (.docx, .pptx) are ZIP archives containing XML parts. The archive module provides low-level utilities for reading and writing these archives.
Reading Archives
unzipToMap
Unzip an OOXML file into a Map<string, Uint8Array>:
import { readFileSync } from "node:fs";
import { unzipToMap } from "@office-open/core";
const zip = unzipToMap(readFileSync("document.docx"));
Read Functions
import { readTextFromZip, readXmlFromZip, readBinaryFromZip } from "@office-open/core";
// Read text content
const contentTypes = readTextFromZip(zip, "[Content_Types].xml");
// Read and parse XML
const documentXml = readXmlFromZip(zip, "word/document.xml");
// Read binary data (images, etc.)
const imageData = readBinaryFromZip(zip, "word/media/image1.png");
readAllXmlParts
Parse all XML parts in the archive, skipping binary files:
import { readAllXmlParts } from "@office-open/core";
const parts = readAllXmlParts(zip);
// { "[Content_Types].xml": Element, "word/document.xml": Element, ... }
listFiles
List files with a path prefix:
import { listFiles } from "@office-open/core";
const mediaFiles = listFiles(zip, "word/media/");
// ["word/media/image1.png", "word/media/image2.jpg"]
Writing Archives
zipToBuffer
Create a ZIP buffer from a file map:
import { zipToBuffer } from "@office-open/core";
const files = new Map<string, Uint8Array | string>();
files.set("word/document.xml", xmlString);
files.set("word/media/image.png", imageBuffer);
const zipBuffer = zipToBuffer(files);
Relationships
OOXML uses .rels files to define relationships between parts.
parseRels
Parse a relationships file:
import { parseRels } from "@office-open/core";
const rels = parseRels(zip, "word/_rels/document.xml.rels");
// [{ id: "rId1", target: "styles.xml", type: "...", targetMode: "External" }, ...]
Relationship Interface
interface Relationship {
id: string;
target: string;
type: string;
targetMode?: string;
}
Complete Example
import { readFileSync, writeFileSync } from "node:fs";
import { unzipToMap, zipToBuffer, readXmlFromZip, listFiles } from "@office-open/core";
// Read archive
const zip = unzipToMap(readFileSync("input.docx"));
// List all parts
const allFiles = listFiles(zip, "");
console.log("Files:", allFiles);
// Read XML part
const document = readXmlFromZip(zip, "word/document.xml");
// Modify the map
zip.delete("word/settings.xml");
// Write modified archive
writeFileSync("output.docx", zipToBuffer(zip));
Utility Functions
| Function | Description |
|---|---|
uint8ToBase64(data) | Convert Uint8Array to base64 string |
getImageType(fileName) | Determine image type from file extension |
elementToXml(el) | Serialize Element to XML string |