Showing posts with label zip(). Show all posts
Showing posts with label zip(). Show all posts

Friday, July 3, 2009

Archive and Compress Files Using zip

Zip and unzip are two free command line programs that comes with Linux. For Windows, they can be downloaded from the Info-ZIP Website. This article provides the basic of archiving and compressing files using the command line zip program.

Zip one file

zip filename.zip file_to_compress

Zip multiple files

zip filename.zip file_to_compress1 file_to_compress2 file_to_compress3

Compression options


The compression level ranges from 0 to 9. Level 0 means no compression, 1 compress faster, and 9 compress better. The default is 6.
zip -9 filename.zip file_to_compress

Password protect zip archive

zip -P the_password filename.zip file_to_compress

To be prompted for password instead of specifying the password in the command line, use -e instead of -P.
zip -e filename.zip file_to_compress
Enter password:
Verify password:

Unzip files

unzip filename.zip

Unzip files with informations (verbose mode)

unzip -v filename.zip

List files that will be unzipped

unzip -l filename.zip

Test Files That Will Be Unzipped

unzip -t filename.zip

Saturday, March 7, 2009

Uncompress files online

WobZIP is an online service for uncompressing compressed file online, from a url without first download the compressed file to your computer. The compressed files will be scanned using an anti-virus scanner, and any infected files will be removed. WobZip works with .rar, .7z, .tar, iso and many other compressed file types.

Link: http://www.wobzip.org

WobZIP interface

Tuesday, February 24, 2009

How to sort Python dictionary

To sort a Python dictionary, and append the keys into a list.
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> sorted(x, key=x.get)
['c', 'a', 'b']

To sort a Python dictionary, and append tuples of the key and value into a list.
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> [(the_key, x[the_key]) for the_key in sorted(x, key=x.get)]
[('c', 1), ('a', 2), ('b', 3)]

To sort a Python dictionary, and append the keys into a tuple, and the values into a tuple.
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> [(the_key, x[the_key]) for the_key in sorted(x, key=x.get)]
[('c', 1), ('a', 2), ('b', 3)]
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> the_keys, the_vals = zip(*[(the_key, x[the_key])
for the_key in sorted(x, key=x.get)])
>>> the_keys
('c', 'a', 'b')
>>> the_vals
(1, 2, 3)

The results from the above examples are in ascending order. To sort in descending order, attach reverse=True at the end of the sorted() function.
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> sorted(x, key=x.get, reverse=True)
['b', 'a', 'c']

Unzip a Python list

Suppose that you have a list with tuples as the member.
>>> test = [('x',1), ('y',2), ('z',3)]
We can unzip the list by using zip() in conjunction with the * operator.
>>> a, b = zip(*test)
>>> a
('x', 'y', 'z')
>>> b
(1, 2, 3)

Thursday, February 19, 2009

How to convert two Python lists into a dictionary

To convert two Python lists into a dictionary. First, pair the two list with the zip() function, as key-value pairs. The last step is to use the dict() constructor to build a dictionary directly from the key-value pairs.
>>> x = ['a', 'b', 'c']
>>> y = [1 ,2 ,3]
>>> dict(zip(x,y))
{'a': 1, 'c': 3, 'b': 2}

We can also zip the two lists together, as list of tuples, and use the dict() constructor for the iterator of that list.
>>> x = ['a', 'b', 'c']
>>> y = [1, 2, 3]
>>> dict(iter(list(zip(x, y))))
{'a': 1, 'c': 3, 'b': 2}