压缩文件

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.

How to create full compressed tar file using Python?

up vote1down votefavorite

1

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

python compression zip tarfile
link|flag
asked Jan 9 at 4:59
Tumbleweed
1,223317

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

start a bounty

3 Answers

oldestnewestvotes

up vote3down voteaccepted

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
answered Jan 9 at 5:19
Alex Martelli
141k5127374

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 vote0down vote

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

link|flag
answered Jan 9 at 5:01
jspcal
8,135417

add comment
up vote1down vote
import tarfiletar = 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".

python压缩与解压缩相关推荐

  1. Python学习笔记(一)压缩与解压缩文件

    Python压缩与解压缩文件 Python能够直接处理zip文件中的数据,例如需要将对应目录或多文件打包或压缩成zip格式,或者需要查看一个zip格式的归档文件中部分或所有的文件同时避免将这些文件展开 ...

  2. python压缩教程_如何使用Python压缩/解压缩zip文件?(代码示例)

    在批量交换大文件和多个文件时,使用zip文件是非常方便的.下面本篇文章就来带大家认识解一下zip文件,介绍使用Python压缩或解压缩zip文件的方法,希望对大家有所帮助.[视频教程推荐:Python ...

  3. Python压缩文件夹/解压缩zip文件

    #coding=utf-8 #甄码农python代码 #使用zipfile做目录压缩,解压缩功能 import os,os.path import zipfile def zip_dir(dirnam ...

  4. python中zlib库用法详解(压缩与解压缩)

    zlib主要用于压缩与解压缩 字符串:使用zlib.compress可以压缩字符串.使用zlib.decompress可以解压字符串. 数据流:压缩:compressobj,解压:decompress ...

  5. python制作解压工具_使用python制作一个解压缩软件

    python实现解压缩的重要模块就是--zipfile,其次是os 安装zipfile模块 首先得安装zipfile模块,打开cmd输入一下命令即可安装 pip install zipfile os是 ...

  6. python编写一个软件-使用python制作一个解压缩软件

    python实现解压缩的重要模块就是――zipfile,其次是os 安装zipfile模块 首先得安装zipfile模块,打开cmd输入一下命令即可安装 pip install zipfile os是 ...

  7. python压缩教程_Python压缩模块zipfile实现原理及用法解析

    一.python压缩模块简介 python直接通过内置压缩模块可以直接进行压缩文件的创建: 内置模块 zipfile/rarfile 完成压缩文件的操作. 二. zipfile模块基础使用 2.1 对 ...

  8. java压缩与解压缩

    2019独角兽企业重金招聘Python工程师标准>>> 主要用到apache的ant.jar,也可用jdk的相关类,但会有中文乱码问题.最重要的俩类是ZipFile和ZipEntry ...

  9. python如何制作一个工程软件_使用python制作一个解压缩软件

    python实现解压缩的重要模块就是--zipfile,其次是os 安装zipfile模块 首先得安装zipfile模块,打开cmd输入一下命令即可安装 pip install zipfile os是 ...

最新文章

  1. Python编程基础:第五十三节 匿名函数Lambda Function
  2. 全球与中国激光投影设备市场前景规划与发展战略建议报告2022-2028年版
  3. 超大超长图片居中显示且放大缩小无影响
  4. java单元格合并多列_ElementUI表格列相同值自动合并单元格( 多列 )
  5. seqkit根据基因id_Microwell-Seq
  6. Android开发(二十四)——数据存储SharePreference、SQLite、File、ContentProvider
  7. 基于vue2.0打造移动商城页面实践 vue实现商城购物车功能 基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果...
  8. jquery 判断是否是浮点数_jquery或者js获取到元素宽高精确到小数
  9. HTTP协议扫盲(一)HTTP协议的基本概念和通讯原理
  10. 微信停机断网可充话费;苹果正研发可折叠屏幕 iPad;Rust 1.36.0 发布 | 极客头条...
  11. word 方框内打勾√或打叉×
  12. java.lang.arr_以太坊常见问题和错误 / java.lang.ArrayIndexOutOfBoundsException? - 汇智网...
  13. flatMap,zip,Merge区别
  14. 鸿蒙子系统解读-分布式任务调度篇
  15. 浏览器指纹技术与浏览器指纹破解办法
  16. git中reset与revert的使用
  17. 中国石油大学《物理化学》第二阶段在线作业
  18. 怎么样给小孩取名字?给孩子起名字也不是一件难事
  19. 群联PS2251-03东芝MLC量产成功教程(东芝16G)
  20. ldslite是什么启动项_ldslite是什么开机启动项

热门文章

  1. cisco设备运维常用命令 总结-1
  2. 第十八 django及ORM操作
  3. package.json字段全解
  4. Flex DataGrid设置不同行高度和自动换行
  5. LINUX samba的安装使用
  6. UVA 11983 Weird Advertisement
  7. 性能提高和支持IE 9的新的ExtJS发布版:4.02和3.4
  8. Linux有问必答-如何创建和挂载XFS文件系统
  9. Linux Sed命令具体解释+怎样替换换行符quot;\nquot;(非常多面试问道)
  10. python学习笔记(7-11)