GithubHelp home page GithubHelp logo

node-native-zip's Introduction

This project has been abandoned in favor of node-archiver

If you're in need of a non-binary ZIP library for node.js, I'd like to redirect you to node-archiver.

node-native-zip

All the current ZIP solutions for node.js are wrappers around existing zip executables, spawning on demand. To all of you who rather have a native implementation of zip'ing in javascript there is node-native-zip. This package works with Buffer objects, which allows you to do complex in-memory stuff with the least amount of overhead.

It has been inspired by JSZip.

How to install

Via NPM:

npm install node-native-zip

Via GIT:

git clone git://github.com/janjongboom/node-native-zip.git

How to use

There are two ways to feed files into a new .zip file. Either by adding Buffer objects, or by adding an array of files.

Adding Buffer objects

    var fs = require("fs");
    var zip = require("node-native-zip");
    
    var archive = new zip();
    
    archive.add("hello.txt", new Buffer("Hello world", "utf8"));
    
    var buffer = archive.toBuffer();
    fs.writeFile("./test1.zip", buffer, function () {
        console.log("Finished");
    });

Adding files from the file system

    var fs = require("fs");
    var zip = require("node-native-zip");
    
    var archive = new zip();
    
    archive.addFiles([ 
        { name: "moehah.txt", path: "./test/moehah.txt" },
        { name: "images/suz.jpg", path: "./test/images.jpg" }
    ], function (err) {
        if (err) return console.log("err while adding files", err);
        
        var buff = archive.toBuffer();
        
        fs.writeFile("./test2.zip", buff, function () {
            console.log("Finished");
        });
    });

API Reference

There are three API methods:

  • add(name, data), the 'name' is the name within the .zip file. To create a folder structure, add '/'
  • addFiles(files, callback), where files is an array containing objects in the form { name: "name/in/zip.file", path: "file-system.path" }. Callback is a function that takes 1 parameter 'err' which indicates whether an error occured.
  • toBuffer(), creates a new buffer and writes the zip file to it

Compression?

The library currently doesn't do any compression. It stores the files via STORE. Main reason is that the compression call is synchronous at the moment, so the thread will block during compression, something to avoid. However, it is possible to add compression methods by implementing the following interface.

    module.exports = (function () {
        return {
            indicator : [ 0x00, 0x00 ],
            compress : function (content) {
                // content is a Buffer object.
                // you have to return a new Buffer too.
            }
        };
    }());

The indicator is an array consisting of two bytes indicating the compression technology. For example: [ 0x00, 0x00] is STORE, [ 0x08, 0x00] is DEFLATE.

The compress function is a method that transforms an incoming Buffer into a new one.

The easiest to implement is probably deflate, because there is a sample in JSZip. You will only need to change the inner workings from string-based to Buffer-based.

Unzipping

Unzipping is more complex because of all the different compression algorithms that you may encounter in the wild. So it's not covered. Use existing libraries for that.

node-native-zip's People

Contributors

janjongboom avatar syzer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-native-zip's Issues

estimating total zip size beforehand?

Hi, is there a built in way to estimate the final file size of a zip with no compression based on the sum of the file sizes?

I'm streaming a series of files from S3 into a zip and streaming those to the browser, but it would be nice to be able to send a Content-Length header too.

Thanks.

license

Could you add a license for this project?

CRC should be computed on raw data rather than compressed data

I added support for deflate in my fork and noticed that the CRC were wrong. They should be computed on the uncompressed data.

I could send you a pull request from my fork but I used streamline.js to simplify the async bits and I'm not sure you wanna go in this direction.

Bruno

Issues in extracting zip folder in safari

Is it possible to open the zip folder that is created using node-native-zip module in safari?. It is working fine for me in all other browsers but i am getting an error in safari when trying extract it.

No way to add an empty folder?

I'm about to use node-native-zip in my hobby project but I don't seem to find a way of adding an empty folder to the zip archive. Aren't there any approaches to do it with node-native-zip?

Add license

Please add a license for this project. Thanks!

OSX Archive Utility: Error 1 - Operation not permitted

Hi, this seems to be the same issue as reported for PHPZip here:

Grandt/PHPZip#4

Other tools, such as Archive and even "unzip" via terminal, can open the zip file with no dramas - it's just the Archive Utility built into OSX that has dramas.

From the discusion:

The root cause is a mix between an odd behaviour of the deflate
algorithm in the PHP implementation of zlib, and a bug in the Mac OSX
"Archive Utility"

Has anyone else encountered this?

I can't get images + text files to work together...

I'm trying to use this for a packager type thing i'm building for bootstrap... and I'm running into the issue that if i add an image + a file, the images unpack as executables named 0.

I can successfully add multiple images (which unpack correctly)...

I can also successfully add multiple text files (which unpack correctly)...

it appears to just be the combination of images and text files...

any ideas?

node-native-zip 压缩文件时,向addFiles方法中添加远程文件(http://siburuxue/....../1472544228848.html)

最近在使用您写的第三方note-native-zip将文件写入Zip 并打包下载,但是在实现功能的时候 发现并不能将远程的文件加入压缩包,查看代码的时候发现在readFile在读取远程文件的时候会异常。末学在异常的部分添加的对远程文件的处理,希望您的下一个版本能将这个功能加入到第三方,这样就不用再远程文件时在额外的写其他的逻辑了。Thank you。

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.