{"id":11473,"date":"2014-01-29T16:24:02","date_gmt":"2014-01-29T10:54:02","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11473"},"modified":"2016-11-30T10:31:58","modified_gmt":"2016-11-30T05:01:58","slug":"working-with-zip-files-in-node-js","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/working-with-zip-files-in-node-js\/","title":{"rendered":"Working with zip files in node.js"},"content":{"rendered":"<p>Large files can be really difficult to share and exchange. Most email service providers do not allow to share heavy files and most of the times we end up zipping them together for easy sharing.<\/p>\n<p>Zipping a file can be of great convenience for you and the end user both.<\/p>\n<p>Based on different use cases, In this blog, we will discuss how we can zip a buffer data and save it to disk. We will be using a good module for zipping files or unzipping files, named &#8216;adm-zip&#8217;.<\/p>\n<p><strong>adm-zip:<\/strong>\u00a0A Javascript implementation of zip for <a title=\"node.js development\" href=\"http:\/\/www.tothenew.com\/mean-node-js-development-consulting\" target=\"_blank\">nodejs<\/a>. Allows user\/s to create or extract zip files both in memory or to\/from disk. To know more, you can access its <a title=\"adm-zip github page\" href=\"https:\/\/github.com\/cthackers\/adm-zip\" target=\"_blank\">github page<\/a>.<\/p>\n<p><strong>Use cases:<\/strong><\/p>\n<p>Zipping a file:<\/p>\n<ol>\n<li><span style=\"color: #333333;font-family: 'Helvetica Neue', Helvetica, Arial, 'Nimbus Sans L', sans-serif;font-style: normal\"><span style=\"color: #333333;font-family: 'Helvetica Neue', Helvetica, Arial, 'Nimbus Sans L', sans-serif;font-style: normal\"><span style=\"color: #333333;font-family: 'Helvetica Neue', Helvetica, Arial, 'Nimbus Sans L', sans-serif;font-style: normal\">Zipping a file present in the form of buffer:<br \/>\nLets assume we have some data in buffer named &#8216;dataBuffer&#8217;<br \/>\n<\/span><\/span><\/span><\/p>\n<p>[js]<br \/>\nvar zip=require(&#8216;adm-zip&#8217;);<br \/>\nvar dataBuffer\u00a0= new Buffer(&quot;hello,this is test data&quot;,&#8217;utf-8&#8242;);\/\/console.log(dataBuffer.toString());<br \/>\nvar zipper = new zip();<br \/>\nzipper.addFile(&#8216;test.txt&#8217;,dataBuffer);<br \/>\n[\/js]<\/p>\n<\/li>\n<li>Zipping a file present on disk :<br \/>\nSometimes we have file\/s saved on our disk, to make zip of those file we use &#8220;addLocalFile&#8221;. It will zip the file and add it to the archive .<br \/>\nExample:<\/p>\n<p>[js]var zip=require(&#8216;adm-zip&#8217;);<br \/>\nvar zipper = new zip();<br \/>\nzipper.addLocalFile(&#8216;\/home\/user\/node\/test.txt&#8217;);<br \/>\n[\/js]<\/p>\n<p>It will add file &#8220;test.txt&#8221; \u00a0that resides in node directory into the archive.<br \/>\nWe can also add some local file\/s in pre-existing archive by giving it to that archive in second argument.<\/p>\n<p>[js]<br \/>\nzipper.addLocalFile(&#8216;\/home\/user\/node\/test.txt&#8217;,&#8217;\/home\/user\/node\/test.zip&#8217;);<br \/>\n[\/js]<\/p>\n<p><span style=\"background-color: #f4f4f4;font-family: 'Courier 10 Pitch', Courier, monospace;font-size: 13px;font-style: normal;line-height: 1.5\">For adding multiple files<br \/>\n<\/span><\/p>\n<p>[js]<br \/>\nfileArray.forEach(function(file){<br \/>\nzipper.addLocalFile(file,&#8217;\/home\/user\/node\/test.zip&#8217;);<br \/>\n});<br \/>\n[\/js]<\/p>\n<\/li>\n<\/ol>\n<p>Zipping folder from disk:<\/p>\n<p>Suppose we have a directory on disk and we need to zip out the whole directory. In such cases, we will use &#8220;<strong>addLocalFolder<\/strong>&#8221; method which allow us to add whole directory to the archive.<\/p>\n<p>[js]<br \/>\nvar zip=require(&#8216;adm-zip&#8217;);<br \/>\nvar zipper = new zip();<br \/>\nzipper.addLocalFolder(&#8216;\/home\/user\/test\/&#8217;,&#8217;\/home\/user\/test.zip&#8217;);<br \/>\n[\/js]<\/p>\n<p>Similarly to put group of directories into the archive:<\/p>\n<p>[js]<br \/>\ndirectoryArray.forEach(function(dirPath){<br \/>\nzipper.addLocalFolder(dirPath,&#8217;\/home\/user\/test.zip&#8217;);<br \/>\n});<br \/>\n[\/js]<\/p>\n<p>After zipping, we will be required to write those files somewhere, be it on disk or in response. Below, we have mentioned some methods to write these files:<\/p>\n<ul>\n<li>To write the archive file on disk:<\/li>\n<\/ul>\n<p>[js]<br \/>\nzip.writeZip(\/*target file name*\/&quot;\/home\/user\/files.zip&quot;);<br \/>\n&lt;\/span&gt;<br \/>\n[\/js]<\/p>\n<ul>\n<li>To write archive in response<\/li>\n<\/ul>\n<p>[js]<br \/>\nvar zipBuffer = zip.toBuffer();\/\/ or write everything to disk<br \/>\n&lt;\/span&gt;[\/js]<\/p>\n<p>We can send this buffer data in response to a request and setting filename in headers of response.<\/p>\n<p>Hope this blog was helpful for working with\u00a0zip files in <a title=\"Nodejs development\" href=\"http:\/\/www.tothenew.com\/mean-node-js-development-consulting\">node.js<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Large files can be really difficult to share and exchange. Most email service providers do not allow to share heavy files and most of the times we end up zipping them together for easy sharing. Zipping a file can be of great convenience for you and the end user both. Based on different use cases, [&hellip;]<\/p>\n","protected":false},"author":66,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":76},"categories":[1185],"tags":[4260,4259,1292,1291,1293],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11473"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11473"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11473\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}