Skip to content

SFCOMPY™ :: Computer Repair Blog :: IT Blog

  • .HOME
  • .CATEGORIES
    • Ads
    • Audio
    • Booting
    • Data Recovery
    • Drivers
    • Data Recovery
    • File System
    • Hardware
    • Network
    • OS
    • Software
    • Security
    • System
    • User Account
    • Utility
    • Video
    • Web Browser
  • .MY ACCOUNT
  • .ABOUT
    • .Privacy Policy
    • .Terms
  • .SFCOMPY™
  • Toggle search form
sfcompy-spend-hours-ad
  • Namecheap! Transfer .COM and save 16% Ads
  • InMotion! Shared Hosting Landing Page Ads
  • sfcompy-inmotion-cyber-month
    InMotion Hosting – Cyber Week/Month Ads
  • sfcompy namecheap ninja-black Friday deals
    NameCheap Black Friday: Web Ninjas unite! Ads
  • NameCheap Web Security QR SFCOMPY
    #ChooseNamecheap Web Security -Internet protection for you and your customers Ads

Zip or UnZip files and folders – MacOS CLI

Posted on July 13, 2021August 14, 2021 By SFCOMPY™ Editor No Comments on Zip or UnZip files and folders – MacOS CLI
sfcompy-zip-archive-and-comoress

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

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 its efficiency and simplicity to archives and compress files and data. Another important reason why we may benefits from using the ZIP tool, its because it is a Cross-Platform tool, means its been supported by most of the popular Operating System, you can extract the same archive Zip file in Windows, Mac’s or Linux machines without the need to install extra software – its a build-in tool

#--------------------------------------------------------------
# ZIP:
#--------------------------------------------------------------
# To demonstrate how to use the ZIP command to archive and compress 
# one or more files, we will use two types of files for that, one is a 
# simple text file ['txt' type] and the second file is an image ['png' type]

# The text file 'report.txt' size is 6232 Bytes (6.232KB)
# The image file 'mypic.png' size is 126340 Bytes (126KB)

bash> ls -ltr
-rw-------@ 1 sfcompy  staff  126340 Mar 23 13:43 pic1.png
-rw-r--r--@ 1 sfcompy  staff    6232 Jul  3 14:25 report.txt

#------------------------------------------------------------
# To archive the text file we can use the following command format:
# zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
# The simplified version for this example is: zip archivename.zip myfile
# archivename.zip: is the name of the archive file 
# myfile:  is the file you like to archive
#------------------------------------------------------------

# To archive and compress 'report.txt' file:

bash> zip myreport.zip  report.txt
  adding: report.txt (deflated 58%)

bash> ls -ltr 
-rw-r--r--  1 sfcompy  staff    2763 Jul 13 15:20 myreport.zip

# The result archive file is a compressed file with a compression ratio
# around 58%, the archive file size now is 2763 Bytes vs 6232 Bytes
# before the compressions
# To calculate the compression ratio in precent: 100 x (1 - 2763/6232))= 55.66% and as expected its around around 58% 

#-----------------------------------------------------------
# now lets compress the image file 

bash> zip myimage.zip pic1.png
  adding: pic1.png (deflated 3%)

bash> ls -ltr
 -rw-r--r--  1 sfcompy  staff  122977 Jul 13 15:50 myimage.zip

# as expected the compression ration is not great as with the text
# file (only 3%) from the reason that the 'png' file type is already
# a version of a compressed image file

#------------------------------------------------------------------
# To archive and compress more then one file we can use the following format 
# zip myarchive.zip file1 file1 ..fileN 

# now lets archive and compress both of the files

bash> zip mytwofiles.zip report.txt pic1.png 
   adding: report.txt (deflated 58%)
   adding: pic1.png (deflated 3%)

# and the result is an archive file mytwofiles.zip that includes both of the files with size of 125718 Bytes as expected around 126KB:
# (122977 + 2763 = 125740)
 
bash> ls -ltr 
-rw-r--r--  1 sfcompy  staff  125718 Jul 13 15:53 mytwofiles.zip

#----------------------------------------------------------------
# if we like to archive and compress folders and files , we need to
# use the recursive switch '-r' at the Zip command
# we can use the following format 
# zip -r  myarchive.zip  myfolder 
# to see all the Zip command switches and options used the switch '-h'
# zip -h

# To simplified this process its highly recommended to copy all the 
# files and folders that you like to archive to a new folder 

bash> mkdir myfolder
bash> cp pic1.png report.txt myfolder
bash>zip -r myarchive.zip myfolder/
bash> ls -ltr
total 512
 -rw-------@ 1 sfcompy  staff  126340 Mar 23 13:43 pic1.png
 -rw-r--r--@ 1 sfcompy  staff    6232 Jul 13 15:13 report.txt
 drwxr-xr-x  4 sfcompy  staff     128 Jul 13 16:46 myfolder
 -rw-r--r--  1 sfcompy  staff  125858 Jul 13 16:48 myarchive.zip

# as expected myarchive.zip files size is around 126KB

#--------------------------------------------------------------
# UNZIP:
#--------------------------------------------------------------
# To unzip a compressed and archive zip file the unzip command format 
# as the following:
# unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]

# To simplified this example we will create an new folder and then copy
# the archive zip file into the new folder, then we will extract the archive inside 

# First ,we can inspect the archive zip file by using the '-l' switch
# then we will get a full report about the archive file and content

bash> uzip -l myarchive.zip
Archive:  myarchive.zip
   Length      Date    Time    Name
 ---------  ---------- -----   ----
         0  07-13-2021 16:46   myfolder/
    126340  07-13-2021 16:46   myfolder/pic1.png
      6232  07-13-2021 16:46   myfolder/report.txt
 ---------                     -------
    132572                     3 files

# we can see that it contains a folder 'myfolder' which contains the two
# files that we archive at the example above

#-----------------------------------------------
# ALERT: keep in mind if you extract the archive file to a folder that 
# contains the same files and folders names as in the archive, 
# !!! it may OVER WRITE the file and the folders WITHOUT prompting!!!!
#-------------------------------------

# Thats why in this example we will extract the archive file 
# in a new temporary folder


bash> mkdir tmp
bash> cp myarchive.zip tmp 
bash> cd tmp
bash> unzip myarchive.zip
Archive:  myarchive.zip
    creating: myfolder/
   inflating: myfolder/pic1.png       
   inflating: myfolder/report.txt

bash> ls -ltr myfolder/
 total 264
 -rw-r--r--  1 sfcompy  staff    6232 Jul 13 16:46 report.txt
 -rw-------  1 sfcompy  staff  126340 Jul 13 16:46 pic1.png   

# as expected the the unzip command created the folder myfolder which
# includes both of the files

# To extract the myarchive.zip to a specific folder (tmp2) we can use the
# switch '-d' 

bash> unzip -d tmp2 myarchive.zip 
bash> ls -ltr 
total 248
 drwxr-xr-x  4 sfcompy  staff     128 Jul 13 16:58 myfolder
 -rw-r--r--  1 sfcompy  staff  125900 Jul 13 17:22 myarchive.zip
 drwxr-xr-x  3 sfcompy  staff      96 Jul 13 17:37 tmp2

bash> ls -ltr tmp2
total 0
 drwxr-xr-x  4 sfcompy  staff  128 Jul 13 16:58 myfolder

# as we can see the tmp2 folder contains the extracted folder 

bash> ls -ltr tmp2/myfolder/
 total 264
 -rw-r--r--  1 sfcompy  staff    6232 Jul 13 16:46 report.txt
 -rw-------  1 sfcompy  staff  126340 Jul 13 16:46 pic1.png


# -------------- DONE!

Related posts:

  • sfcompy_blog_412x331Show Hidden files - MacOS CLI
  • NameCheap Web Security QR SFCOMPY#ChooseNamecheap Web Security -Internet protection for you and your customers
  • sfcompy_blog_412x331Hide or Show the mac Users folder - MacOS CLI
Data Recovery, File System, Network, Security, System, Utility Tags:All Macs, Command Line (CLI), iMac, Mac Mini, MacBook

Post navigation

Previous Post: Hide or Show mac Login User – MacOS CLI
Next Post: Password Protected Zip file – MacOS CLI

Leave a Reply Cancel reply

You must be logged in to post a comment.

InMotion Hosting - Cyber Week/Month Dedicated Hosting Landing Page

Recent Posts

  • sfcompy-protected-zip-filePassword Protected Zip file – MacOS CLI
    July 16, 2021
  • sfcompy-zip-archive-and-comoressZip or UnZip files and folders – MacOS CLI
    July 13, 2021
  • sfcompy_blog_412x331Show Hidden files – MacOS CLI
    June 23, 2021

InMotion Shared Hosting Landing Page Sale

InMotion WordPress Hosting Landing Page Sale

InMotion Reseller Hosting Landing Page

Categories

  • Ads (5)
  • Data Recovery (1)
  • File System (3)
  • Network (4)
  • OS (3)
  • Security (9)
  • Software (1)
  • System (3)
  • User Account (5)
  • Utility (4)
  • Video (1)
  • Web Browser (1)

Tag

All Ads (4) All Android (2) All IOS (2) All Linux (2) All Macs (12) All Windows (4) Command Line (CLI) (8) iMac (8) InMotion (2) mac (1) MacBook (8) Mac Mini (8) namecheap (2) Pro and Advance User (7) Standard or local User (1)
June 2025
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Mar    
  • Sign a pdf file Using Microsoft Edge
  • InMotion Hosting – Cyber Week/Month
  • NameCheap Black Friday: Web Ninjas unite!
  • #ChooseNamecheap Web Security -Internet protection for you and your customers
  • InMotion! Shared Hosting Landing Page

  • Sign a pdf file Using Microsoft Edge
  • InMotion Hosting – Cyber Week/Month
  • NameCheap Black Friday: Web Ninjas unite!
  • #ChooseNamecheap Web Security -Internet protection for you and your customers
  • InMotion! Shared Hosting Landing Page
  • Sign a pdf file Using Microsoft Edge
  • sfcompy-inmotion-cyber-monthInMotion Hosting – Cyber Week/Month
  • sfcompy namecheap ninja-black Friday dealsNameCheap Black Friday: Web Ninjas unite!
  • NameCheap Web Security QR SFCOMPY#ChooseNamecheap Web Security -Internet protection for you and your customers
  • InMotion! Shared Hosting Landing Page
  • Sign a pdf file Using Microsoft Edge Software
  • NameCheap Web Security QR SFCOMPY
    #ChooseNamecheap Web Security -Internet protection for you and your customers Ads
  • sfcompy-inmotion-cyber-month
    InMotion Hosting – Cyber Week/Month Ads
  • Namecheap! Transfer .COM and save 16% Ads
  • sfcompy_macosx_users
    Get the User’s List – MacOS CLI User Account
  • sfcompy_blog_412x331
    Show Hidden files – MacOS CLI File System
  • sfcompy-vsc-color-theme-screens
    SFCOMPY™ Visual Studio Code Color Theme For Pro System
  • sfcompy-protected-zip-file
    Password Protected Zip file – MacOS CLI File System
  • About
  • Cart
  • Category
  • Checkout
  • Computer Repair & IT Service Blog
  • My account
  • Post Page
  • Privacy Policy
  • Shop
  • Terms

Copyright © 2025 SFCOMPY™ Computer Repair & IT Blog .

Powered by SFCOMPY™ WP Theme for Pro WordPress theme