Giter Club home page Giter Club logo

node-hk-zip's Introduction

NodeJS unzip implementation

This module has been developed for a specific use case (unzipping specific files with a flexible API). It supports only the most common compression method deflate. There are some missing features, such as CRC check, encrypted files, and ZIP 64 support. I plan to add them later. if you like this and want to enhance/add missing features or add more public functions, feel free to come up with a pull request, but before doing that, please check CONTRIBUTING.md

Motivation

Most of the open-source ZIP libraries are written in old javascript, weakly tested, not well structured, and mostly depending on other open-source libraries, which makes it hard to manage, perform patches, and updates. This module is written in Typescript. Which allows to create more abstractions around the idea, provides type checking and OOP capabilities.

Good sides

  • Fully typed thanks to Typescript
  • 0 external dependencies
  • Easy to scale
  • Easy to test
  • Flexible API

Todo list

  • Implement CRC-32 check
  • Add support for encrypted ZIPs
  • Add support for more compression methods
  • Add support for ZIP64
  • Add zipping functionality
  • Add streaming API
  • Add more unit tests

Installation

NPM

npm install node-hk-zip

Yarn

yarn add node-hk-zip

Getting started

 import fs from 'fs';
 
 import {ZipFile, ZipEntry} from 'node-hk-zip';
 
 const data: Buffer = fs.readFileSync('..some zip file path'); // IMPORTANT: Usage of sync method for demo purposes only
 
 const zip: ZipFile = new ZipFile(data);
 const entries: ZipEntry[] = zip.listAllEntries();
 
 entries.forEach((e: ZipEntry) => {
    console.log(e.describe());
    console.log(e.decompress());
 });

ZipFile API

List all entries in the ZIP file - listAllEntries(): ZipEntry[]

 const zip: ZipFile = new ZipFile(data);
 const entries: ZipEntry[] = zip.listAllEntries();
 
 entries.forEach((e: ZipEntry) => {
    console.log(e.describe());
 });

Sample output

{ isDirectory: false, name: 'test.png', path: 'test.png' }
{ isDirectory: true, name: '__MACOSX', path: '__MACOSX/' }
{ isDirectory: false, name: '._test.png', path: '__MACOSX/._test.png' }
{ isDirectory: false, name: 'test.txt', path: 'test.txt' }
{ isDirectory: false, name: '._test.txt',path: '__MACOSX/._test.txt' }

Find an entry in the ZIP file - findEntries(string[]): ZipEntry[]

 const zip: ZipFile = new ZipFile(data);
 const paths: string[] = ['test.png'];
 const entries: ZipEntry[] = zip.findEntries(paths);
 
 entries.forEach((e: ZipEntry) => {
    console.log(e.describe());
 });

Sample output

{ isDirectory: false, name: 'test.png', path: 'test.png' }

Find matching entries by RegExp - findMatchingEntries(reg: RegExp[]): ZipEntry[]

 const zip: ZipFile = new ZipFile(data);
 const regs: RegExp = [/^[a-z]+\.(png)/, /^[a-z]+\.(txt)/];
 const entries: ZipEntry[] = zip.findMatchingEntries(regs);
 
 entries.forEach((e: ZipEntry) => {
    console.log(e.describe());
 });

Sample output

{ isDirectory: false, name: 'test.png', path: 'test.png' }
{ isDirectory: false, name: 'test.txt', path: 'test.txt' }

ZipEntry API

Decompress file data - decompress(): Promise<Buffer>

 const zip: ZipFile = new ZipFile(data);
 const paths: string[] = ['test.png'];
 const entries: ZipEntry[] = zip.findEntries(paths);
 
 entries.forEach(async (e: ZipEntry) => {
   const info = e.describe();
   const data = await e.decompress();
   fs.writeFileSync(info.name, data); // IMPORTANT: Usage of sync method for demo purposes only
 });

Sample output

Writes decompressed test.png under project root.

Getter for LocalFileHeader size - getLocalHeaderSize(): number

 const zip: ZipFile = new ZipFile(data);
 const paths: string[] = ['test.png'];
 const entries: ZipEntry[] = zip.findEntries(paths);
 
 entries.forEach(async (e: ZipEntry) => {
   console.log(e.getLocalHeaderSize());
 });

Sample output

Prints LocalFileHeader size

Checks if the entry is a directory - isDirectory(): boolean

 const zip: ZipFile = new ZipFile(data);
 const paths: string[] = ['__MACOSX/'];
 const entries: ZipEntry[] = zip.findEntries(paths);
 
 entries.forEach(async (e: ZipEntry) => {
   console.log(e.isDirectory());
 });

Sample output

ture // __MACOSX/' is a directory

Getter for entry name - getName(): string

 const zip: ZipFile = new ZipFile(data);
 const paths: string[] = ['test.png'];
 const entries: ZipEntry[] = zip.findEntries(paths);
 
 entries.forEach(async (e: ZipEntry) => {
   console.log(e.getName());
 });

Sample output

Prints test.png

Getter for entry's full path in the zip file - getPath(): string

 const zip: ZipFile = new ZipFile(data);
 const paths: string[] = ['__MACOSX/._test.png'];
 const entries: ZipEntry[] = zip.findEntries(paths);
 
 entries.forEach(async (e: ZipEntry) => {
   console.log(e.getPath());
 });

Sample output

Prints __MACOSX/._test.png

Describe entry in the zip file - describe(): IZipEntryDescription

 const zip: ZipFile = new ZipFile(data);
 const paths: string[] = ['test.png'];
 const entries: ZipEntry[] = zip.findEntries(paths);
 
 entries.forEach(async (e: ZipEntry) => {
   console.log(e.describe());
 });

Sample output

{ isDirectory: false, name: 'test.png', path: 'test.png' }

node-hk-zip's People

Contributors

dependabot[bot] avatar hackash avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.