方便的压缩文件读写装潢器

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的支持

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
我可以用C++代码使用java代码吗?   java使用JSR303在派生类中提供更具体的约束   java在这个查找唯一路径数算法中我做错了什么?   java如何为2个不同的服务提供商使用2个不同的SSL证书?   java在Gridview上绘制文本   java使用连接for循环构建字符串名   java StringBuilder拆分无法处理某些文件   java事件关注EditText   Java Web Start“找不到URL的缓存资源”   java程序从命令行运行的速度比在Eclipse中慢   java为什么HttpServletRequest会截断#字符上的url输入?   java自定义折叠工具栏平滑标题大小调整   使用Mockito对安卓 java中调用另一个静态函数的函数进行单元测试   http在java客户机中使用cachecontrol头   java如何使用。是否使用Delimiter从输入文件中排除标点符号和数字?   使用上下文作为参数/参数的java   java更有效地从Jar中提取文件   java为多个JButton提供相同的actionListener