2018-09-04 07:14:20 +00:00
|
|
|
# Container File Blobs
|
2013-09-05 18:55:36 +00:00
|
|
|
|
2018-09-04 07:14:20 +00:00
|
|
|
Pure JS implementation of various container file formats, including ZIP and CFB.
|
2013-09-05 18:55:36 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
[![Build Status](https://travis-ci.org/SheetJS/js-cfb.svg?branch=master)](https://travis-ci.org/SheetJS/js-cfb)
|
|
|
|
[![Coverage Status](http://img.shields.io/coveralls/SheetJS/js-cfb/master.svg)](https://coveralls.io/r/SheetJS/js-cfb?branch=master)
|
|
|
|
[![Dependencies Status](https://david-dm.org/sheetjs/js-cfb/status.svg)](https://david-dm.org/sheetjs/js-cfb)
|
|
|
|
[![NPM Downloads](https://img.shields.io/npm/dt/cfb.svg)](https://npmjs.org/package/cfb)
|
|
|
|
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-cfb?pixel)](https://github.com/SheetJS/js-cfb)
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
In the browser:
|
|
|
|
|
|
|
|
```html
|
|
|
|
<script src="dist/cfb.min.js" type="text/javascript"></script>
|
|
|
|
```
|
2013-09-05 18:55:36 +00:00
|
|
|
|
2017-08-09 06:50:59 +00:00
|
|
|
With [npm](https://www.npmjs.org/package/cfb):
|
2013-09-05 18:55:36 +00:00
|
|
|
|
2017-02-24 05:11:45 +00:00
|
|
|
```bash
|
2017-09-14 21:14:22 +00:00
|
|
|
$ npm install cfb
|
2013-09-05 18:55:36 +00:00
|
|
|
```
|
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
The `xlscfb.js` file is designed to be embedded in [js-xlsx](http://git.io/xlsx)
|
2013-10-29 18:50:54 +00:00
|
|
|
|
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
## Library Usage
|
2013-10-29 18:50:54 +00:00
|
|
|
|
|
|
|
In node:
|
|
|
|
|
2017-02-24 05:11:45 +00:00
|
|
|
```js
|
|
|
|
var CFB = require('cfb');
|
|
|
|
```
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
For example, to get the Workbook content from an Excel 2003 XLS file:
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2017-02-24 05:11:45 +00:00
|
|
|
```js
|
|
|
|
var cfb = CFB.read(filename, {type: 'file'});
|
2017-09-14 21:14:22 +00:00
|
|
|
var workbook = CFB.find(cfb, 'Workbook');
|
2017-08-09 06:50:59 +00:00
|
|
|
var data = workbook.content;
|
2017-02-24 05:11:45 +00:00
|
|
|
```
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2017-08-09 06:50:59 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
## Command-Line Utility Usage
|
|
|
|
|
2020-07-09 06:12:05 +00:00
|
|
|
The [`cfb-cli`](https://www.npmjs.com/package/cfb-cli) module ships with a CLI
|
|
|
|
tool for manipulating and inspecting supported files.
|
2017-10-20 20:36:54 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
|
|
|
|
## JS API
|
|
|
|
|
|
|
|
TypeScript definitions are maintained in `types/index.d.ts`.
|
2013-10-29 18:50:54 +00:00
|
|
|
|
|
|
|
The CFB object exposes the following methods and properties:
|
|
|
|
|
|
|
|
`CFB.parse(blob)` takes a nodejs Buffer or an array of bytes and returns an
|
|
|
|
parsed representation of the data.
|
|
|
|
|
2018-09-04 07:14:20 +00:00
|
|
|
`CFB.read(blob, opts)` wraps `parse`.
|
|
|
|
|
|
|
|
`CFB.find(cfb, path)` performs a case-insensitive match for the path (or file
|
|
|
|
name, if there are no slashes) and returns an entry object or null if not found.
|
|
|
|
|
|
|
|
`CFB.write(cfb, opts)` generates a file based on the container.
|
|
|
|
|
|
|
|
`CFB.writeFile(cfb, filename, opts)` creates a file with the specified name.
|
|
|
|
|
|
|
|
### Parse Options
|
|
|
|
|
|
|
|
`CFB.read` takes an options argument. `opts.type` controls the behavior:
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
| `type` | expected input |
|
2018-09-04 07:14:20 +00:00
|
|
|
|------------|:----------------------------------------------------------------|
|
2017-10-20 20:36:54 +00:00
|
|
|
| `"base64"` | string: Base64 encoding of the file |
|
|
|
|
| `"binary"` | string: binary string (byte `n` is `data.charCodeAt(n)`) |
|
2021-09-06 19:53:23 +00:00
|
|
|
| `"buffer"` | nodejs Buffer |
|
2017-10-20 20:36:54 +00:00
|
|
|
| `"file"` | string: path of file that will be read (nodejs only) |
|
|
|
|
| (default) | buffer or array of 8-bit unsigned int (byte `n` is `data[n]`) |
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2017-08-09 06:50:59 +00:00
|
|
|
|
2018-09-04 07:14:20 +00:00
|
|
|
### Write Options
|
|
|
|
|
|
|
|
`CFB.write` and `CFB.writeFile` take options argument.
|
|
|
|
|
|
|
|
`opts.type` controls the behavior:
|
2017-09-14 21:14:22 +00:00
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
| `type` | output |
|
2018-09-04 07:14:20 +00:00
|
|
|
|------------|:----------------------------------------------------------------|
|
2017-10-20 20:36:54 +00:00
|
|
|
| `"base64"` | string: Base64 encoding of the file |
|
|
|
|
| `"binary"` | string: binary string (byte `n` is `data.charCodeAt(n)`) |
|
2021-09-06 19:53:23 +00:00
|
|
|
| `"buffer"` | nodejs Buffer |
|
2018-03-05 03:49:40 +00:00
|
|
|
| `"file"` | string: path of file that will be created (nodejs only) |
|
2017-10-20 20:36:54 +00:00
|
|
|
| (default) | buffer if available, array of 8-bit unsigned int otherwise |
|
2017-09-14 21:14:22 +00:00
|
|
|
|
2018-09-04 07:14:20 +00:00
|
|
|
`opts.fileType` controls the output file type:
|
|
|
|
|
2020-07-09 06:12:05 +00:00
|
|
|
| `fileType` | output |
|
|
|
|
|:-------------------|:------------------------|
|
|
|
|
| `'cfb'` (default) | CFB container |
|
|
|
|
| `'zip'` | ZIP file |
|
|
|
|
| `'mad'` | MIME aggregate document |
|
2018-09-04 07:14:20 +00:00
|
|
|
|
|
|
|
`opts.compression` enables DEFLATE compression for ZIP file type.
|
2017-09-14 21:14:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Utility Functions
|
|
|
|
|
|
|
|
The utility functions are available in the `CFB.utils` object. Functions that
|
|
|
|
accept a `name` argument strictly deal with absolute file names:
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
- `.cfb_new(?opts)` creates a new container object.
|
|
|
|
- `.cfb_add(cfb, name, ?content, ?opts)` adds a new file to the `cfb`.
|
2018-04-09 06:33:22 +00:00
|
|
|
Set the option `{unsafe:true}` to skip existence checks (for bulk additions)
|
2017-09-14 21:14:22 +00:00
|
|
|
- `.cfb_del(cfb, name)` deletes the specified file
|
|
|
|
- `.cfb_mov(cfb, old_name, new_name)` moves the old file to new path and name
|
2020-03-13 05:58:41 +00:00
|
|
|
- `.use_zlib(require("zlib"))` loads a nodejs `zlib` instance.
|
2018-09-04 07:14:20 +00:00
|
|
|
|
|
|
|
By default, the library uses a pure JS inflate/deflate implementation. NodeJS
|
|
|
|
`zlib.InflateRaw` exposes the number of bytes read in versions after `8.11.0`.
|
|
|
|
If a supplied `zlib` does not support the required features, a warning will be
|
|
|
|
displayed in the console and the pure JS fallback will be used.
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
|
|
|
|
## Container Object Description
|
|
|
|
|
|
|
|
The objects returned by `parse` and `read` have the following properties:
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2014-11-03 04:02:42 +00:00
|
|
|
- `.FullPaths` is an array of the names of all of the streams (files) and
|
2013-10-29 18:50:54 +00:00
|
|
|
storages (directories) in the container. The paths are properly prefixed from
|
|
|
|
the root entry (so the entries are unique)
|
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
- `.FileIndex` is an array, in the same order as `.FullPaths`, whose values are
|
|
|
|
objects following the schema:
|
2017-09-14 21:14:22 +00:00
|
|
|
|
|
|
|
```typescript
|
|
|
|
interface CFBEntry {
|
|
|
|
name: string; /** Case-sensitive internal name */
|
|
|
|
type: number; /** 1 = dir, 2 = file, 5 = root ; see [MS-CFB] 2.6.1 */
|
|
|
|
content: Buffer | number[] | Uint8Array; /** Raw Content */
|
|
|
|
ct?: Date; /** Creation Time */
|
|
|
|
mt?: Date; /** Modification Time */
|
2020-07-09 06:12:05 +00:00
|
|
|
ctype?: String; /** Content-Type (for MAD) */
|
2017-09-14 21:14:22 +00:00
|
|
|
}
|
|
|
|
```
|
2013-10-29 18:50:54 +00:00
|
|
|
|
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
## License
|
2013-09-05 18:55:36 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
Please consult the attached LICENSE file for details. All rights not explicitly
|
|
|
|
granted by the Apache 2.0 License are reserved by the Original Author.
|
2013-09-05 18:55:36 +00:00
|
|
|
|
2014-06-24 04:00:39 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
## References
|
2017-02-24 05:11:45 +00:00
|
|
|
|
2020-03-13 05:58:41 +00:00
|
|
|
- `MS-CFB`: Compound File Binary File Format
|
|
|
|
- ZIP `APPNOTE.TXT`: .ZIP File Format Specification
|
2018-09-04 07:14:20 +00:00
|
|
|
- RFC1951: https://www.ietf.org/rfc/rfc1951.txt
|
2020-07-09 06:12:05 +00:00
|
|
|
- RFC2045: https://www.ietf.org/rfc/rfc2045.txt
|
|
|
|
- RFC2557: https://www.ietf.org/rfc/rfc2557.txt
|
2014-06-24 04:00:39 +00:00
|
|
|
|