python压缩与解压缩

压缩文件

import os
import zipfile
import time
# 压缩目录
source_dir  = r'F:\web'
# 按时间生成文件名称
target_file = time.strftime('%Y%m%d%H%M%S') + '.zip'

myZipFile = zipfile.ZipFile(target_file, 'w' )# 压缩所有文件,包含子目录
for root,dirs,files in os.walk(source_dir):
    for vfileName in files:
        fileName = os.path.join(root,vfileName)
        myZipFile.write( fileName, fileName, zipfile.ZIP_DEFLATED )
 # 压缩完成
myZipFile.close()

 

 

 

Gzip File Handling. The gzip module provides function and class definitions that make it easy to handle simple Gzip files. These allow you to open a compressed file and read it as if it were already decompressed. They also allow you to open a file and write to it, having the data automatically compressed as you write.

gzip.open( filename mode level fileobj ) → gzip.GzipFile

Open the file named filename with the given mode ('r''w' or 'a'). The compression level is an integer that provides a preference for speed versus size. As an alternative, you can open a file or socket separately and provide a fileobj , for example, f= open('somefile','r'); zf= gzip.open( fileobj=f ).

Once this file is open, it can perform ordinary readreadlinereadlineswritewriteline, and writelines operations. If you open the file with 'rb'mode, the various read functions will decompress the file's contents as the file is read. If you open the file with 'wb' or 'ab' modes, the various write functions will compress the data as they write to the file.

 

 

 

 

up vote 1 down vote favorite
1

how can I create a .tar.gz file which compress the data as much tar can...

link | flag

75% accept rate
2
 
tar doesn't compress data, it just packs the files together. It's gzip that does the actual compression. –  Ignacio Vazquez-Abrams  Jan 9 at 5:19
add comment

3 Answers

up vote 3 down vote accepted

You call tarfile.open with mode='w:gz', meaning "Open for gzip compressed writing."

You'll probably want to end the filename (the name argument to open) with .tar.gz, but that doesn't affect compression abilities.

BTW, you usually get better compression with a mode of 'w:bz2', just like tar can usually compress even better with bzip2 than it can compress with gzip.

link | flag
1
 
Just a quick note that the filename for bzip2-compressed tarballs should end with ".tar.bz2". –  Ignacio Vazquez-Abrams  Jan 9 at 5:23
add comment

up vote 0 down vote

tarfile module is cool: http://docs.python.org/library/tarfile.html

link | flag
add comment
up vote 1 down vote
import tarfile
tar
= tarfile.open("sample.tar.gz", "w:gz")
for name in ["file1", "file2", "file3"]:
    tar
.add(name)
tar
.close()

If you want to create a tar.bz2 compressed file, just replace file extension name with ".tar.bz2" and "w:gz" with "w:bz2".

 


posted on 2010-10-11 18:41  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2010/10/11/1848141.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值