compound-binary-file-js
Version:
This is an implementation of [Compound Binary File v.3](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cfb/53989ce4-7b05-4f8d-829b-d08d6148375b) \ Allows reading existing files, creation of the/write operation
47 lines (44 loc) • 2.53 kB
text/typescript
import {CompoundFile} from "../src/CompoundFile";
import { expect, assert } from "chai";
import {RootStorageDirectoryEntry} from "../src/directory/RootStorageDirectoryEntry";
describe('compound file test', () => {
it('create new', () => {
const compoundFile = new CompoundFile();
const rootStorage = compoundFile.getRootStorage();
assert.isNull(rootStorage.getLeftSibling());
assert.isNull(rootStorage.getRightSibling());
assert.isNull(rootStorage.getChild());
expect(rootStorage.getDirectoryEntryName()).eq(RootStorageDirectoryEntry.NAME);
expect(rootStorage.getDirectoryEntryNameLengthUTF8()).eq(10);
});
it('rewrite compound file', () => {
const compoundFile = new CompoundFile();
const rootStorage = compoundFile.getRootStorage();
rootStorage.addStorage("first");
rootStorage.addStorage("second");
rootStorage.addStream("stream1", [1,2,3,4]);
const copy = compoundFile.rewrite();
assert.isNotNull(copy.getRootStorage().findChild(dirEntry => "first" === dirEntry.getDirectoryEntryName()));
assert.isNotNull(copy.getRootStorage().findChild(dirEntry => "second" === dirEntry.getDirectoryEntryName()));
assert.isNotNull(copy.getRootStorage().findChild(dirEntry => "stream1" === dirEntry.getDirectoryEntryName()));
});
it('save and the read', () => {
const compoundFile = new CompoundFile();
const rootStorage = compoundFile.getRootStorage();
const storage1 = rootStorage.addStorage("storage1");
let storage2 = rootStorage.addStorage("storage2");
rootStorage.addStream("stream1", [1,2,3,4]);
storage2.addStream("stream2", [1,2,3,4]);
let storage21 = storage2.addStorage("storage21");
storage21.addStorage("storage211");
const copy = CompoundFile.fromBytes(compoundFile.asBytes());
expect(copy.getRootStorage().storages().length).eq(2);
assert.isNotNull(copy.getRootStorage().findChild(dirEntry => "storage1" === dirEntry.getDirectoryEntryName()));
storage2 = copy.getRootStorage().findChild(dirEntry => "storage2" === dirEntry.getDirectoryEntryName());
assert.isNotNull(storage2);
expect(storage2.children().length).eq(2);
storage21 = storage2.findChild(dirEntry => "storage21" === dirEntry.getDirectoryEntryName());
assert.isNotNull(storage2);
expect(storage21.children().length).eq(1);
});
});