Giter Site home page Giter Site logo

xlsx-zip's Introduction

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);
}

xlsx-zip's People

Contributors

shenx-2021 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.