Skip to content

shenX-2021/xlsx-zip

Repository files navigation

Xlsx-Zip

NPM

Xlsx-Zip is a library that adapts for Office Xlsx Zip format. You can use it to compress xlsx's metadata, but can't extract.

this library is inspired by node-compress-commons and node-zip.

Feature

  • Zip format compatible with Office Xlsx
  • Support zip64
  • Support both stream and buffer output

Why this project

Microsoft Office Xlsx Zip doesn't adapt standard Zip(PK-ZIP) format completely, which doesn't support ZIP64 Central Directory. So most Zip library can't generate a standard Office Xlsx file. The library is created to generate it perfectly.

Reference

Install

npm i xlsx-zip

Usage

Buffer

const { XlsxZip } = require('xlsx-zip');
const fs = require('fs');

async function main() {
  // don't set `stream` parameter
  const xlsxZip = new XlsxZip();

  const dirPath = 'directory/path';
  // add by directory
  await xlsxZip.addDirectory(null, dirPath);
  // add by Buffer
  await xlsxZip.add('entry/path1', Buffer.from('entry'));
  // add by Buffer list
  await xlsxZip.add('entry/path1', [Buffer.from('entry')]);
  // add by file stream
  const rs = fs.createReadStream('file/path');
  await xlsxZip.add('entry/path2', rs);

  const buf = await xlsxZip.finish();

  fs.writeFileSync('./output.xlsx', buf);
}

Stream

const { XlsxZip } = require('xlsx-zip');
const fs = require('fs');

async function main() {
  const stream = fs.createWriteStream('./output.xlsx');
  const xlsxZip = new XlsxZip({ stream });

  stream.on('close', () => {
    console.log('done');
  });

  const dirPath = 'directory/path';
  // add by directory
  await xlsxZip.addDirectory(null, dirPath);
  // add by Buffer
  await xlsxZip.add('entry/path1', Buffer.from('entry'));
  // add by Buffer list
  await xlsxZip.add('entry/path1', [Buffer.from('entry')]);
  // add by file stream
  const rs = fs.createReadStream('file/path');
  await xlsxZip.add('entry/path2', rs);

  await xlsxZip.finish();
}

RegExp

you can filter some path you don't want to add into ZIP stream with regExp parameter

const { XlsxZip } = require('xlsx-zip');
const fs = require('fs');

async function main() {
  const xlsxZip = new XlsxZip({
    regExp: '.DS_Store'
  });
  // or
  const xlsxZip = new XlsxZip({
    regExp: new RegExp('.DS_Store')
  });

  const dirPath = 'directory/path';
  // add by directory
  await xlsxZip.addDirectory(null, dirPath);

  const buf = await xlsxZip.finish();

  fs.writeFileSync('./output.xlsx', buf);
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published