Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip a string?

What is the equivalent of this class in python? https://gist.github.com/2594962

In PHP, it allows you to zip a string.

I'm trying to find the equivaelnt in the following languages: Python, Ruby on Rails and ASP.

I'm hoping there are built in functions for these languages. I couldn't find one in PHP.

Update

When I say Zip, I am referring to the standard algorithm that windows uses. Not in the sense of an archive. I currently use that class to zip a string, base64 encode it and send it as a request to an internal API.

like image 346
Abs Avatar asked May 04 '12 14:05

Abs


People also ask

How do you zip a string in Python?

Python's zip() function is defined as zip(*iterables) . The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.

What is zip used for in Python?

zip() in Python Python zip() method takes iterable or containers and returns a single iterator object, having mapped values from all the containers. It is used to map the similar index of multiple containers so that they can be used just using a single entity.


2 Answers

To compress a string using the same method used in .zip archives, just use the zlib module directly (which is what Python's zipfile module does). Here's a simple example:

import zlib

teststr = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus
pretium justo eget elit eleifend, et dignissim quam eleifend. Nam vehicula nisl
posuere velit volutpat, vitae scelerisque nisl imperdiet. Phasellus dignissim,
dolor amet."""

cmpstr = zlib.compress(teststr.encode('utf-8'))
uncmpstr = zlib.decompress(cmpstr)

fmt = '{:>8}: (length {}) {!r}'
print(fmt.format('teststr', len(teststr), teststr))
print(fmt.format('cmpstr', len(cmpstr), cmpstr))
print(fmt.format('uncmpstr', len(uncmpstr), uncmpstr))

Output:

 teststr: (length 237) 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus\npretium justo eget elit eleifend, et dignissim quam eleifend. Nam vehicula nisl\nposuere velit volutpat, vitae scelerisque nisl imperdiet. Phasellus dignissim,\ndolor amet.'
  cmpstr: (length 157) 'x\x9cMO[\x0e\xc30\x08\xfb\xef)8@\xd5\x93L\xd3\xae\x10%^\xcb\x94W\x03\xf4\xfc\xa3\x9d\xb4\xed\x07\tcc\xfb\xd6\x06\nq\x17+\x94Zn\x83\x84\x95B\x81\xce\x14[\x15D\x85\xda\xa0\x90\xb8\xb3D\xae+!\xb3.\xf4\xd8\x82 g\x93\xa9\x0f(\xbb\xfce\xa2\x8d\xb0B/\x8a\x0f\xf0\x135\xcd\xe4H\xe2\xb5\xb2\x08\x17\xda-\x94\xefm\xa1\xbbo\x076\x8e\x96\x039%O\xbd\x89a\xc0\xd1\xf3\xcb\xd1\xb2i\x0f\x1e\xe7`\r \x89\xae\x1d,\xbb\xe1\xa2\x13\x97\x8e\x91\x18\xff\x99~v\xf3\xf4iu6Z\xde\xf8\xa6X\r'
uncmpstr: (length 237) 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus\npretium justo eget elit eleifend, et dignissim quam eleifend. Nam vehicula nisl\nposuere velit volutpat, vitae scelerisque nisl imperdiet. Phasellus dignissim,\ndolor amet.'
like image 198
martineau Avatar answered Sep 28 '22 00:09

martineau


Under Python, you are looking for the zipfile module - specifically ZipFile.writestr().

I'd note that in general, zlib is used a lot more for the kind of uses you are talking about.

like image 25
Gareth Latty Avatar answered Sep 28 '22 00:09

Gareth Latty