How to pin a string?

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

In PHP, it allows you to pin a string.

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

I hope there are built-in functions for these languages. I could not find it in PHP.

Update

When I say Zip, I mean the standard algorithm that Windows uses. Not in the sense of an archive. I am currently using this class for a zip string, base64 encodes it and sends it as a request to the internal API.

+3
source share
4 answers

In Python, you are looking for a modulezipfile - in particular ZipFile.writestr().

, zlib , .

+4

, , .zip, zlib ( Python zipfile ). :

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))

:

 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.'
+12

Python zipfile, / zip-.

zipfile.ZipFile writestr(), "" .

, .

, , PHP - PK-Zip. Python zipfile. , zip - , .

import zipfile
from cStringIO import StringIO

f = StringIO()
z = zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED)
z.writestr('name', 'some_bytes_to_compress')
z.close()

output_string = f.getvalue()

output_string PK-Zip.

If you control the sending and receiving side, and you do not need to send multiple compressed files into one piece of data, using PK-Zip is redundant. Instead, you can simply use a module zlibthat implements compression for PK-Zip.

import zlib

output_string = zlib.compress('some_bytes_to_compress')

And then you can unzip it (assuming Python):

decompressed_string = zlib.decompress(output_string)
+3
source

I don’t know what exactly you need, but you may find it interesting. I am using a zipfile module with a file of type string here

import zipfile
import StringIO

s = StringIO.StringIO()  # s is a file like object
z = zipfile.ZipFile(s, 'w')  # this is a zip archive
z.writestr('file1.txt', 'Hello, world') # create a "file" inside the archive called 'file1.txt' and write 'hello world' into it
z.close() # close the archive

print s.getvalue()  # this is the content of the string
s.close()  # close the string file-like object

Watch the PK launch inside the line

+2
source

All Articles