npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zip-process

v0.2.6

Published

Tiny module for easy modify & repack files in zip archives and zip-containers

Downloads

8

Readme

zip-process

Tiny module for easy modify & repack files in zip archives and zip-containers like Excel files. Based on JSZip.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install zip-process

API

Imagine that we have .xlsx file (zip container). And need to change all occurences 'John' to 'Fedor'. !Don't do that if not sure for avoid side-effects.

Node.js


const zipProcess = require('zip-process');
const fs = require(fs);

// zipProcess options
const options = {}; // All default

// zipProcess calbacks (processors)
const callbacks = {	
  // We split binary && text processing, for binary use key 'binary', for text files 'string'
  string: { 
    // aplly path filter we need to check all .xml && .xml.rels files
    filter: relativePath =>  !!/\.xml(\.rels)?$/.test(relativePath),
			
    // just change it, and return new value
    callback: (data, zipFileName) =>  data && data.replace(/\bJohn\b/g, 'Fedor')
  }
}


fs.readFile('./john.xlsx', function(err, source) {

  if (err) { throw err; }

  zipProcess(source, options, callbacks).then( out => {

    const changed = source !== out;

    if (changed) {
      fs.writeFileSync('./fedor.xlsx', out);
    }

  }) ; // enf of zipProcess

}); // end of fs.readFile

VanillaJS with FileSaver


var xhr = new XMLHTTPRequest();
xhr.open("GET", './john.xlsx', true);
xhr.responseType = 'arraybuffer';
xhr.addEventListener('load',function(){
  if (xhr.status === 200){

    zipProcess(xhr.response, {}, {
      string: {
        filter: relativePath =>  !!/\.xml(\.rels)?$/.test(relativePath),
        callback: data =>  data && data.replace(/\bJohn\b/g, 'Fedor')
      }
    }).then((result) => {

      FileSaver.saveAs(
        result 
        'fedor.xlsx',
        true
       );

    });
  }
})
xhr.send();

As your might guess, I wrote this module special for work with Excel files, but other types of zip works fine too ;)

function zipProcess(content, options, callbacks)

Process zip content (binary) and returns promise. Where promise get in parameter new packed content.

content parameter

Zip file content (readed as binary).

options parameter

If ommited, blank or undefined, defaults options will be used.

Awailable options:

  • removeSignature: Remove file if callback return exactly signature (default null)
  • compression: compress file 'DEFLATE' or not 'STORE' (default 'DEFLATE') as options.compession in JSZip.generateAsync
  • extendOptions : all other options for options in JSZip.generateAsync (default {})
  • type: (default: for Node application 'nodebuffer', for browser 'blob' ) as options.type in JSZip.generateAsync

callbacks parameter

Hash of {string, binary, ...} with same structure {filter, callback, options}:

{
  string: {
    filter: function(relativePath, fileInfo){ ... },
      callback: function(fileContent, relativePath, zipObject){ ... },
      options: { ... }
    },
    binary: {
      filter: ...
      callback: ...
      options: ...
    },
}

Section keys: {string}, {binary} and others

String section used for work with text files. This mean that this files will be decoded by zipObject.async('string'). So binary files will be decoded by zipObject.async('unit8array'). Hint! you can use any other key for section, it will be translated to zipObject.async(). Available types you can see in JSZip documentation.

Warning! I dont know how national encoded files will works. I prefer to use UTF8 any way.

Section structure

filter: function(relativePath, fileInfo)

This function allow process only files within zip that you want. Realized directly by JSZip.filter.

If ommited, behaviour defined by section keys:

  • string - files with extension where defined mime-charset ( mime.lookup(mime.charset(fileName)) ) or xml or xml based extensions
  • binary - all files which not string
  • any other key - all files in archive
callback: function(fileContent, relativePath, zipObject)

If ommited, nothing changed.

This call back must return changed or unchanged content, undefined or signature for remove this file from archive.

  • If content changed, target file will be repacked with new content.
  • If content unchanged or undefined will nothing changed.
  • If content === options.removeSignature (default null), target file will be removed from archive.
options: {removeSignature: ...}

Can override option removeSignature.

webpack 1.x troubles

I use mime-types, which use mime-db, which includes json file directly to code. So If you have not configure json-loader yet, just do it.

History

0.2.6 - fix for IE support 0.2.5 and above - fog of development

Author

Alexander (mclander) Maksimenko , Sbertech, Moscow, Russia.

Any comments and pull request will be appreciated.