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 file1 file2 file3

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

Compressing a single file only:

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

Zipping a directory:

Syntax: zip -r /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 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/
" Select a file with cursor and press ENTER

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

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

$ zip -T
test of OK

And finally:

Extracting files from a zip archive with unzip:

$ unzip -d ~/output
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

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 archive, do like this:
$ unzip file2
extracting: file2

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

$ unzip
extracting: mike.doc
[] 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 to get the latest Linux news and how to guides directly on your e-mail!