命令

字符串:使用zlib.compress可以压缩字符串。使用zlib.decompress可以解压字符串。

数据流:压缩:compressobj,解压:decompressobj

案例

>>> import zlib
>>> s = 'slfsjdalfkasflkkdkaleeeeeeeeeeeeeeeeeeeeeeeeeeeelaaalkllfksaklfasdll kkkkkk123'
>>> zlib_s = zlib.compress(s)
>>> zlib_s
'x\x9c}\xca\xb1\r\xc0 \x10\x04\xc1Vh\xc1\xb8\xa2\x93\x9e\x0f|\x9b]\xff\x92\x11\x050\xf1\x84\xceW\xa2\xad4vY\xac\x0b$a\xf6\x8fL+\x05c\xf8x\xe6\xfb\x03\xf7\x97\x1e\xd1' >>> print tlen(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tlen' is not defined
>>> print len(s)
79
>>> print len(zlib_s)
55
>>> ss = zlib.decompress(zlib_s)
>>> ss
'slfsjdalfkasflkkdkaleeeeeeeeeeeeeeeeeeeeeeeeeeeelaaalkllfksaklfasdll kkkkkk123'

压缩与解压缩文件

import zlib
def compress(infile, dst, level=9):
infile = open(infile, 'rb')
dst = open(dst, 'wb')
compress = zlib.compressobj(level)
data = infile.read(1024)
while data:
dst.write(compress.compress(data))
data = infile.read(1024)
dst.write(compress.flush())
def decompress(infile, dst):
infile = open(infile, 'rb')
dst = open(dst, 'wb')
decompress = zlib.decompressobj()
data = infile.read(1024)
while data:
dst.write(decompress.decompress(data))
data = infile.read(1024)
dst.write(decompress.flush()) if __name__ == "__main__":
infile = "1.txt"
dst = "1.zlib.txt"
compress(infile, dst) infile = "1.zlib.txt"
dst = "2.txt"
decompress(infile, dst)
print "done~"

注:compressobj返回一个压缩对象,用来压缩不能一下子读入内存的数据流。 level 从9到-1表示压缩等级,其中1最快但压缩度最小,9最慢但压缩度最大,0不压缩,默认是-1大约相当于与等级6,是一个压缩速度和压缩度适中的level。

python使用zlib实现压缩与解压字符串的更多相关文章

  1. python用模块zlib压缩与解压字符串和文件的方法

    摘自:http://www.jb51.net/article/100218.htm Python标准模块中,有多个模块用于数据的压缩与解压缩,如zipfile,gzip, bz2等等. python中 ...

  2. python tar.gz格式压缩、解压

    一.压缩 需求描述 现在有一个目录,需要将此目录打包成tar.gz文件.因为有一个Django项目,需要用到此功能! tar.gz 目录结构如下: ./ ├── folder │   ├── .doc ...

  3. C# 压缩与解压字符串(面试题)

    /* * 题目:压缩字符串.如“abbcccddddeef”,压缩成“a1b2c3d4e2f1” * 解题: 这个题目也是面试常见的题目.看似很简单,其实暗藏杀机.一般的想法就是,一边遍历,一边计数, ...

  4. zlib的压缩与解压

    http://zlibnet.codeplex.com/releases/view/629717 using ZLibNet; string str = "ccc"; byte [ ...

  5. python 模块zlib 压缩与解压

    例子1:压缩与解压字符串 import zlib message = 'abcd1234' compressed = zlib.compress(message) decompressed = zli ...

  6. PAT 乙级 1078 字符串压缩与解压 (20)

    文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表示.如果字符没有重复,就原样输出.例如 ...

  7. PAT 1078 字符串压缩与解压(20)(代码+思路)

    1078 字符串压缩与解压(20 分) 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表 ...

  8. PAT 1078 字符串压缩与解压

    https://pintia.cn/problem-sets/994805260223102976/problems/994805262018265088 文本压缩有很多种方法,这里我们只考虑最简单的 ...

  9. PAT(B) 1078 字符串压缩与解压(Java)

    题目链接:1078 字符串压缩与解压 (20 point(s)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...

随机推荐

  1. SAE saestorage.class.php文件的封装代码

    Source for file saestorage.class.php Documentation is available at saestorage.class.php <?php /** ...

  2. Redis Sentinel哨兵集群

    Redis Sentinel(哨兵集群)是一种高可用的redis部署方案.在集群中的redis-master服务挂掉时,无需人为干预,即可通过哨兵集群的自我调整,实现redis服务的持续可用. 哨兵集 ...

  3. poj 3253 初涉二叉堆 模板题

    这道题很久以前就做过了 当时是百度学习了优先队列 后来发现其实还有个用sort的办法 就是默认sort排序后 a[i]+=a[i-1] 然后sort(a+i,a+i+n) (大概可以这样...答案忘了 ...

  4. NV SDK 9.5, 10 and 11

    NVIDIA SDK 10   Overview This all-new collection of DirectX 10 and OpenGL code samples teaches devel ...

  5. Linux 计划任务 Crontab 笔记与总结(3)Crontab 配置文件

    [全局(系统)配置文件] /etc/crontab Linux 主要的配置文件都在 etc 目录下. cd /etc ls cron* vim crontab 进入 cron.d(服务) cd cro ...

  6. mysql线程缓存和表缓存

    一.线程缓存1.thread_cache_size定义了线程缓冲中的数量.每个缓存中的线程通常消耗256kb内存2.Threads_cached,可以看到已经建立的线程二.表缓存(table_cach ...

  7. Flink - Generating Timestamps / Watermarks

    https://ci.apache.org/projects/flink/flink-docs-release-1.0/apis/streaming/event_timestamps_watermar ...

  8. oracle管理控制台不能打开,提示此网站的安全证书有问题?

    在命令行里直接键入:certutil -setreg chain\minRSAPubKeyBitLength 128 然后再用IE打开.

  9. Nginx 禁用IP IP段

    最近公司网站被竞争对手用爬虫频繁访问,所以我们这边要禁止这些爬虫访问,我们通过nginx 指令就可以实现了 方法一:直接在LB机器上封IP 1.在 blocksip.conf 文件中加入要屏蔽的ip或 ...

  10. ubuntu cpus 共享打印

    下载工具 axel 打印机 hp-setup http://blog.x1986.com/t/18.think lsusb wkhtmltopdf/0.12.2.1 ubuntu 14.01 x64下 ...