Archiving and Compression
It's possible to adjust the level of compression that zip uses when it does its job. The zip command uses a scale from 0 to 9, in which 0 means "no compression at all" (which is like tar, as you'll see later), 1 means "do the job quickly, but don't bother compressing very much," and 9 means "compress the heck out of the files, and I don't mind waiting a bit longer to get the job done." The default is 6, but modern computers are fast enough that it's probably just fine to use 9 all the time.
Say you're interested in researching Herman Melville's Moby-Dick, so you want to collect key texts to help you understand the book: Moby-Dick itself, Milton's Paradise Lost, and the Bible's book of Job. Let's compare the results of different compression rates.
$ ls -l -rw-r--r-- scott scott 102519 job.txt -rw-r--r-- scott scott 1236574 moby-dick.txt -rw-r--r-- scott scott 508925 paradise_lost.txt $ zip -0 moby.zip *.txt adding: job.txt (stored 0%) adding: moby-dick.txt (stored 0%) adding: paradise_lost.txt (stored 0%) $ ls -l -rw-r--r-- scott scott 102519 job.txt -rw-r--r-- scott scott 1236574 moby-dick.txt -rw-r--r-- scott scott 1848444 moby.zip -rw-r--r-- scott scott 508925 paradise_lost.txt $ zip -1 moby.zip *txt updating: job.txt (deflated 58%) updating: moby-dick.txt (deflated 54%) updating: paradise_lost.txt (deflated 50%) $ ls -l -rw-r--r-- scott scott 102519 job.txt -rw-r--r-- scott scott 1236574 moby-dick.txt -rw-r--r-- scott scott 869946 moby.zip -rw-r--r-- scott scott 508925 paradise_lost.txt $ zip -9 moby.zip *txt updating: job.txt (deflated 65%) updating: moby-dick.txt (deflated 61%) updating: paradise_lost.txt (deflated 56%) $ ls -l -rw-r--r-- scott scott 102519 job.txt -rw-r--r-- scott scott 1236574 moby-dick.txt -rw-r--r-- scott scott 747730 moby.zip -rw-r--r-- scott scott 508925 paradise_lost.txt
In tabular format, the results look like this:
|Book||zip -0||zip -1||zip -9|
|Total (in bytes)||1848444||869946||747730|
The results you see here would vary depending on the file types (text files typically compress well) and the sizes of the original files, but this gives you a good idea of what you can expect. Unless you have a really slow machine or you're just naturally impatient, you should just use -9 all the time to get the maximum compression.
Note - If you want to be clever, define an alias in your .bashrc file that looks like this:
alias zip='zip -9'
That way you'll always use -9 and won't have to think about it.
The Zip program allows you to password-protect your Zip archives using the -P option. You shouldn't use this option. It's completely insecure, as you can see in the following example (the actual password is 12345678):
$ zip -P 12345678 moby.zip *.txt
Because you had to specify the password on the command line, anyone viewing your shell's history (and you might be surprised how easy it is for other users to do so) can see your password in all its glory. Don't use the -P option!
Instead, just use the -e option, which encrypts the contents of your Zip file and also uses a password. The difference, however, is that you're prompted to type the password in, so it won't be saved in the history of your shell events.
$ zip -e moby.zip *.txt Enter password: Verify password: adding: job.txt (deflated 65%) adding: moby-dick.txt (deflated 61%) adding: paradise_lost.txt (deflated 56%)
The only part of this that's saved in the shell is zip -e moby.zip *.txt. The actual password you type disappears into the ether, unavailable to anyone viewing your shell history.
Caution - The security offered by the Zip program's password protection isn't that great. In fact, it's pretty easy to find a multitude of tools floating around the Internet that can quickly crack a password-protected Zip archive. Think of password-protecting a Zip file as the difference between writing a message on a postcard and sealing it in an envelope: It's good enough for ordinary folks, but it won't stop a determined attacker.
Also, the version of zip included with some Linux distros may not support encryption, in which case you'll see a zip error: "encryption not supported." The only solution: recompile zip from source. Ugh.
|Dynamic DNS—an Object Lesson in Problem Solving||May 21, 2013|
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Tech Tip: Really Simple HTTP Server with Python
- BASH script to log IPs on public web server
3 hours 19 min ago
6 hours 55 min ago
- Reply to comment | Linux Journal
7 hours 28 min ago
- All the articles you talked
9 hours 51 min ago
- All the articles you talked
9 hours 54 min ago
- All the articles you talked
9 hours 56 min ago
14 hours 20 min ago
- Keeping track of IP address
16 hours 11 min ago
- Roll your own dynamic dns
21 hours 25 min ago
- Please correct the URL for Salt Stack's web site
1 day 36 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?