方便的压缩文件读写装潢器
archiveIO的Python项目详细描述
这里有一些用于读写压缩档案的装饰器。
安装
easy_install -U archiveIO
用法
import archiveIO import os from six import BytesIO # Define a function that generates archive contents @archiveIO.save def save(targetPath): open(targetPath, 'wt').write('xxx') # Define a function that processes archive contents @archiveIO.load def load(sourcePath): return open(sourcePath, 'rt').read() # Save archives save('sample.txt') save('sample.txt.zip') save('sample.txt.tar.gz') save('sample.txt.tar.bz2') save('sample.txt.tar') # Load archives assert 'xxx' == load('sample.txt') assert 'xxx' == load('sample.txt.zip') assert 'xxx' == load('sample.txt.tar.gz') assert 'xxx' == load('sample.txt.tar.bz2') assert 'xxx' == load('sample.txt.tar') # Create an archive containing two files @archiveIO.save def save(targetPath): open(targetPath + '.txt', 'wt') open(targetPath + '.csv', 'wt') targetPath = 'sample.zip' save(targetPath) # Target CSV files before TXT files @archiveIO.load(extensions=['.csv', '.txt']) def load(sourcePath): return os.path.basename(sourcePath) assert 'sample.csv' == load(targetPath) # Use MyException instead of IOError class MyException(Exception): pass @archiveIO.load(CustomException=MyException) def load(sourcePath): return sourcePath try: load('xxx.tar.gz') except MyException, error: print error # Compress directly into a string buffer archive = archiveIO.Archive(BytesIO(), '.tar.gz') archive.save([ 'sample.txt', 'sample.txt.zip', ]) # Uncompress into a temporary folder with archiveIO.TemporaryFolder() as temporaryFolder: for filePath in archive.load(temporaryFolder): print filePath
0.5.0
- 修复了对python 3的支持
0.4.4
- 为未压缩文件的目标文件扩展名添加了选项
0.4.3
- 如果打开存档失败,则添加了customexception覆盖范围
0.4.2
- 向load()decorator添加了customexception选项
0.4.1
- 保存zip存档文件时排除了无关的“.”
0.4.0
- 在保存或加载存档文件时添加了对类文件对象的支持
- 在保存存档文件时添加了对文件夹路径的支持
- 测试覆盖率提高到100%
0.3.0
- 添加存档类
0.2.0
- 邮政编码
- 添加了对.tar.gz.tar.bz2.tar的支持