How to zip a directory, recursively, without getting malformed files when you try to unzip in node.js -
so far have tried adm-zip , easy-zip. both of them create zip successful has malformed files:
this happens variety of file types (including regular pictures , html pages). suspect file type isn't issue. either way, can't use if doesn't work perfectly.
some people have suggested node-archive there no instructions how zip file, let alone recursively zip directory while keeping file structure.
update
as requested here code using (adm-zip)
var zip = require("adm-zip"); var zip = new zip(); zip.addlocalfolder("c:\\test"); zip.writezip("c:\\test\\color.zip");
as had mentioned, use node-archiver
. can nested folders node-archiver
by:
var archiver = require('archiver'); var archive = archiver('zip') // create file writestream - let's call writestream archive.pipe(writestream); // subfiles/folders given folder - can done readdir // take @ http://nodejs.org/api/fs.html#fs_fs_readdir_path_callback // , https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search // appending archiver name has slashes in treat payload subfolder/file // iterate through files previous walk iterate_through_results_from_walk // http://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback get_read_stream_on_file_from_fs archive.append((new buffer(data), 'utf-8'), {name: name_with_slashes_for_path}); archive.finalize(function (err) { // handle error if there 1 });
hopefully pretty nudge in right direction (basically of steps there you). if isn't clear yet:
- create archive 'zip' option.
- pipe writestream created want save zip file.
- walk through folder recursively, saving paths in collection (array) go along.
- iterate through collection of paths, opening each file.
- when have data of each file, create
buffer
, pass patharchive.append
. - call
archive.finalize
.
for sake of readability, , because believe have given pretty steps need, did not include of code (most notably walk - laid out in link anyhow).
Comments
Post a Comment