{"id":535,"date":"2021-07-13T14:10:53","date_gmt":"2021-07-13T21:10:53","guid":{"rendered":"https:\/\/sfcompy.com\/blog\/?p=535"},"modified":"2021-08-14T14:49:17","modified_gmt":"2021-08-14T21:49:17","slug":"zip-unzip-file-and-folders-macos-cli","status":"publish","type":"post","link":"https:\/\/sfcompy.com\/blog\/zip-unzip-file-and-folders-macos-cli\/","title":{"rendered":"Zip or UnZip files and folders &#8211; MacOS CLI"},"content":{"rendered":"\n<p><span class=\"has-inline-color has-black-color\">Many times you like to group files and folders into a single file(archive),  The motivation for that is sometimes because you like efficiently (smaller space, fast upload) to send the group of files and folder over the internet (mail , ftp..etc)as a single file, in other cases you just want to upload the group of files and folder to a web-server or a cloud storage in a single operation, you can find many other methods  and technics how to do so, but one of the most simple and the efficient ways is by using the traditional ZIP command<\/span><\/p>\n\n\n\n<p><span class=\"has-inline-color has-black-color\">Two main reasons to use the ZIP command , the first reason its because the ZIP command is a build-in archive tool that can be found in every MacOS machines , the second reason as you already probably knows, its because that the ZIP command  its also a tool to compress the information that the files carries, means the single archive file will consume less storage space then the total storage space that the all files will consume all together as individuals files , of course their are many other data compression method that are relatively more efficient and will compress better for various type of files(such as audio\\video..etc) , but the ZIP algorithm over the years proof<\/span> <span class=\"has-inline-color has-black-color\">its efficiency and simplicity  to archives and compress files and data<\/span>. <span class=\"has-inline-color has-black-color\">Another important reason why we may benefits from using the ZIP tool, its because  it is a Cross-Platform tool<\/span>, <span class=\"has-inline-color has-black-color\">means its been supported by most of the popular Operating System,  you can extract the same archive Zip file in Windows, Mac&#8217;s or Linux machines without the need to install extra software &#8211; its a  build-in tool <\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#--------------------------------------------------------------\n# ZIP:\n#--------------------------------------------------------------\n# To demonstrate how to use the ZIP command to archive and compress \n# one or more files, we will use two types of files for that, one is a \n# simple text file ['txt' type] and the second file is an image ['png' type]\n\n# The text file 'report.txt' size is 6232 Bytes (6.232KB)\n# The image file 'mypic.png' size is 126340 Bytes (126KB)\n\n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; ls -ltr\n-rw-------@ 1 sfcompy  staff  126340 Mar 23 13:43 pic1.png\n-rw-r--r--@ 1 sfcompy  staff    6232 Jul  3 14:25 report.txt<\/span>\n\n#------------------------------------------------------------\n# To archive the text file we can use the following command format:\n# zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]\n# The simplified version for this example is: zip archivename.zip myfile\n# archivename.zip: is the name of the archive file \n# myfile:  is the file you like to archive\n#------------------------------------------------------------\n\n# To archive and compress 'report.txt' file:\n\n<span style=\"color:#37eca0\" class=\"has-inline-color\">bash&gt; zip myreport.zip  report.txt\n  adding: report.txt (deflated 58%)\n\nbash&gt; ls -ltr \n-rw-r--r--  1 sfcompy  staff    2763 Jul 13 15:20 myreport.zip<\/span>\n\n# The result archive file is a compressed file with a compression ratio\n# around 58%, the archive file size now is 2763 Bytes vs 6232 Bytes\n# before the compressions\n# To calculate the compression ratio in precent: 100 x (1 - 2763\/6232))= 55.66% and as expected its around around 58% \n\n#-----------------------------------------------------------\n# now lets compress the image file \n\n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; zip myimage.zip pic1.png\n  adding: pic1.png (deflated 3%)\n\nbash&gt; ls -ltr\n -rw-r--r--  1 sfcompy  staff  122977 Jul 13 15:50 myimage.zip<\/span>\n\n# as expected the compression ration is not great as with the text\n# file (only 3%) from the reason that the 'png' file type is already\n# a version of a compressed image file\n\n#------------------------------------------------------------------\n# To archive and compress more then one file we can use the following format \n# zip myarchive.zip file1 file1 ..fileN \n\n# now lets archive and compress both of the files\n\n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; zip mytwofiles.zip report.txt pic1.png \n   adding: report.txt (deflated 58%)\n   adding: pic1.png (deflated 3%)\n<\/span>\n# and the result is an archive file mytwofiles.zip that includes both of the files with size of 125718 Bytes as expected around 126KB:\n# (122977 + 2763 = 125740)\n \n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; ls -ltr \n-rw-r--r--  1 sfcompy  staff  125718 Jul 13 15:53 mytwofiles.zip\n<\/span>\n#----------------------------------------------------------------\n# if we like to archive and compress folders and files , we need to\n# use the recursive switch '-r' at the Zip command\n# we can use the following format \n# zip -r  myarchive.zip  myfolder \n# to see all the Zip command switches and options used the switch '-h'\n# zip -h\n\n# To simplified this process its highly recommended to copy all the \n# files and folders that you like to archive to a new folder \n\n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; mkdir myfolder\nbash&gt; cp pic1.png report.txt myfolder\nbash&gt;zip -r myarchive.zip myfolder\/\nbash&gt; ls -ltr\ntotal 512\n -rw-------@ 1 sfcompy  staff  126340 Mar 23 13:43 pic1.png\n -rw-r--r--@ 1 sfcompy  staff    6232 Jul 13 15:13 report.txt\n drwxr-xr-x  4 sfcompy  staff     128 Jul 13 16:46 myfolder\n -rw-r--r--  1 sfcompy  staff  125858 Jul 13 16:48 myarchive.zip\n<\/span>\n# as expected myarchive.zip files size is around 126KB\n\n#--------------------------------------------------------------\n# UNZIP:\n#--------------------------------------------------------------\n# To unzip a compressed and archive zip file the unzip command format \n# as the following:\n# unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]\n\n# To simplified this example we will create an new folder and then copy\n# the archive zip file into the new folder, then we will extract the archive inside \n\n# First ,we can inspect the archive zip file by using the '-l' switch\n# then we will get a full report about the archive file and content\n\n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; uzip -l myarchive.zip\nArchive:  myarchive.zip\n   Length      Date    Time    Name\n ---------  ---------- -----   ----\n         0  07-13-2021 16:46   myfolder\/\n    126340  07-13-2021 16:46   myfolder\/pic1.png\n      6232  07-13-2021 16:46   myfolder\/report.txt\n ---------                     -------\n    132572                     3 files<\/span>\n\n# we can see that it contains a folder 'myfolder' which contains the two\n# files that we archive at the example above\n\n#-----------------------------------------------\n# ALERT: keep in mind if you extract the archive file to a folder that \n# contains the same files and folders names as in the archive, \n# !!! it may OVER WRITE the file and the folders WITHOUT prompting!!!!\n#-------------------------------------\n\n# Thats why in this example we will extract the archive file \n# in a new temporary folder\n\n\n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; mkdir tmp\nbash&gt; cp myarchive.zip tmp \nbash&gt; cd tmp\nbash&gt; unzip myarchive.zip\nArchive:  myarchive.zip\n    creating: myfolder\/\n   inflating: myfolder\/pic1.png       \n   inflating: myfolder\/report.txt\n\nbash&gt; ls -ltr myfolder\/\n total 264\n -rw-r--r--  1 sfcompy  staff    6232 Jul 13 16:46 report.txt\n -rw-------  1 sfcompy  staff  126340 Jul 13 16:46 pic1.png   \n<\/span>\n# as expected the the unzip command created the folder myfolder which\n# includes both of the files\n\n# To extract the myarchive.zip to a specific folder (tmp2) we can use the\n# switch '-d' \n\n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; unzip -d tmp2 myarchive.zip \nbash&gt; ls -ltr \ntotal 248\n drwxr-xr-x  4 sfcompy  staff     128 Jul 13 16:58 myfolder\n -rw-r--r--  1 sfcompy  staff  125900 Jul 13 17:22 myarchive.zip\n drwxr-xr-x  3 sfcompy  staff      96 Jul 13 17:37 tmp2\n\nbash&gt; ls -ltr tmp2\ntotal 0\n drwxr-xr-x  4 sfcompy  staff  128 Jul 13 16:58 myfolder\n<\/span>\n# as we can see the tmp2 folder contains the extracted folder \n\n<span class=\"has-inline-color has-light-green-cyan-color\">bash&gt; ls -ltr tmp2\/myfolder\/\n total 264\n -rw-r--r--  1 sfcompy  staff    6232 Jul 13 16:46 report.txt\n -rw-------  1 sfcompy  staff  126340 Jul 13 16:46 pic1.png\n<\/span>\n\n# -------------- DONE!\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many times you like to group files and folders into a single file(archive), The motivation for that is sometimes because you like efficiently (smaller space, fast upload) to send the group of files and folder over the internet (mail , ftp..etc)as a single file, in other cases you just want to upload the group of&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"https:\/\/sfcompy.com\/blog\/zip-unzip-file-and-folders-macos-cli\/\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;Zip or UnZip files and folders &#8211; MacOS CLI&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":563,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,50,62,61,58,63],"tags":[73,79,81,82,80],"_links":{"self":[{"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/posts\/535"}],"collection":[{"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/comments?post=535"}],"version-history":[{"count":0,"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/posts\/535\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/media\/563"}],"wp:attachment":[{"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/media?parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/categories?post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sfcompy.com\/blog\/wp-json\/wp\/v2\/tags?post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}