How to create, split, join and extract zip archives in Linux

I create zip archives when I need to send them to the Windows people. And I usually have to split the archives because of the attachment size limit on email.

1. How to compress files with zip:

Syntax: zip name.zip file1 file2 file3

$ zip mike.zip file1 file2 file3
adding: file1 (stored 0%)
adding: file2 (stored 0%)
adding: file3 (stored 0%)

Compressing a single file only:

$ zip john.zip john01.jpg
adding: john01.jpg (stored 0%)

Zipping a directory:

Syntax: zip -r name.zip /path/to/dir

$ zip -r pictures ~/holidaypics
adding: home/razvan/holidaypics/ (stored 0%)
adding: home/razvan/holidaypics/pic1.jpg (stored 0%)
adding: home/razvan/holidaypics/pic3.jpg (stored 0%)
adding: home/razvan/holidaypics/pic2.jpg (stored 0%)

Creating a password protected archives (with the -e argument):

Sometimes you need to encrypt archives to protect the data inside. The receiver can only extract the files if he knows your password.

$ zip -e the_stevens.zip mike.doc sarah.doc
Enter password:
Verify password:
adding: mike.doc (stored 0%)
adding: sarah.doc (stored 0%)

Extra shell/zip tricks here.

Browsing zip archives:

There are a lot of tools for viewing the files inside an archive without extracting, but I recommand you to use ViM (yes, ViM!):

You can browse a password protected archive without knowing its password.

$ vim the_stevens
" zip.vim version v23
" Browsing zipfile /home/razvan/abc/the_stevens.zip
" Select a file with cursor and press ENTER
mike.doc
sarah.doc
[:q]

How to split files

I will split archives to 5 M each:
The tool for splitting zip archives is zipsplit, but I will use split because it splits any file type.
$ split -b 5M file1 --verbose
creating file `xaa'
creating file `xab'
creating file `xac'
creating file `xad'
creating file `xae'

The –verbose argument is not necesary. I put it only to show you what happens.
The splited files are named xaa, xab, … .

How to join files:

I will join the splited files with cat:

$ cat x* > ~/hugefile
$ ls ~/hugefile
/home/razvan/hugefile

Testing if an archive is OK or broken can be done with zip -T:

$ zip -T mike.zip
test of mike.zip OK

And finally:

Extracting files from a zip archive with unzip:

$ unzip mike.zip -d ~/output
Archive: mike.zip
extracting: /home/razvan/output/file1
extracting: /home/razvan/output/file2
extracting: /home/razvan/output/file3

You can also view the files inside an archive with unzip -l:
$ unzip -l mike.zip

Archive: mike.zip
Length Date Time Name
--------- ---------- ----- ----
0 2012-05-27 09:22 file1
0 2012-05-27 09:22 file2
0 2012-05-27 09:22 file3
--------- -------
0 3 files

If you need to extract only file2 from the mike.zip archive, do like this:
$ unzip mike.zip file2
Archive: mike.zip
extracting: file2

To unzip a password protected zip, you use the same unzip command, but you will have to type in the password:

$ unzip the_stevens.zip
Archive: the_stevens.zip
extracting: mike.doc
[the_stevens.zip] sarah.doc password:
extracting: sarah.doc

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!