40735

zip the folder using built-in modules

<strong>Edit</strong> -> Can someone suggest edits to my answer, for instance I'm not sure if exec is better or spawn?

<hr>

Is it possible to zip the directory/folder with it's contents using zlib and other built-in modules?

I'm looking for a way to do it without external dependencies.

The other option is to run local processes on mac, windows etc. for zip, tar etc., I'm sure there are command line utilities on either of the operating system

This is not an answer but it's somehow related to what I'm looking for, it's spawning a local process to zip.

Another link I'm looking at.

Unix command for zip | exec and spawn

The commands I tried on terminal which worked,

<ol> <li>/usr/bin/zip test.zip /resources/html/article</li> <li>du -hs test.zip</li> </ol>

Code

var zip = function(path) { const spawn = require('child_process').spawn; const exec = require('child_process').exec; exec("which zip", function (error, stdout, stderr) { if (error) { console.log(error); } else { exec(stdout + " -r " + path + "/test.zip " + path, function(error, stdout, stderr){ if(error) { console.log(error); } else { exec("du -hs test.zip", function(error, stdout, stderr){ console.log('done'); console.log(arguments); }); } }) } }); };

Answer1:

Tested on mac and works. Can someone test this on Linux? Any ideas for windows?

Notice the use of stdout.trim() to get rid of an extra \n character returned from console.

function execute(command) { const exec = require('child_process').exec; return new Promise(function(resolve, reject){ exec(command, function(error, stdout, stderr){ if(error) { reject(error); } else { stderr ? reject(stderr) : resolve(stdout.trim()); } }); }); }

<strong>Function zip</strong>

var zip = function(path) { execute("which zip") .then(function(zip){ return execute(zip + " -r abc.zip " + path); }) .then(function(result){ return execute("du -hs abc.zip"); }) .then(function(result){ console.log(result); }) .catch(console.error); };

Recommend

  • fatal error C1083: Cannot open include file: 'Carbon/ Carbon.h': No such file or directory
  • open source, multi-platform, browser-based screen capture extension? [closed]
  • Integrating in Python using Sympy
  • Meteor stuck at Extracting meteor-tool@1.4.0-1
  • Microsoft.Build.Utilities.ToolLocationHelper error on TeamCity
  • writing file in heroku filesystem and reading it with web app
  • What is the difference between Socket.Send and Stream.Write? (in relation to tcp ip connections)
  • Implementing HMAC-SHA256 for Keybase in Javascript
  • Objective C - Create a framework for my iphone apps?
  • Unable to get CAP_CHOWN and CAP_DAC_OVERRIDE working for regular user
  • Installing samr in in R 3.4
  • How to start server for Selenium grid Java Maven setup
  • Cassandra 2.1: Recursion by nesting UDT's
  • How do I pip install from Google Code?
  • Create a multiple screen android application
  • Which browser have this strange user agent? (IOS device)
  • Does the Azure table storage API cache results?
  • How to apply a custom handlers to only specific folder
  • OSX - always hide certain files
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Overlapping controls in Windows XP
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Why cepheus don't send int without quotes to orion?
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Install PHP intl extension on MacOS
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Using $this when not in object context
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Sony Xperia Z Tablet not found by adb
  • Perl system calls when running as another user using sudo
  • Javascript convert timezone issue
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How do you join a server to an Active Directory (domain)?
  • how does django model after text[] in postgresql [duplicate]
  • Cant find why the layout is getting smaller