怎樣用Python解壓縮*.tgz文件
- 2020 年 1 月 10 日
- 筆記
怎樣用Python解壓縮*.tgz文件
KnightPython: 正如*大熊*所說,使用tarfile模塊即可: import tarfile tarobj = tarfile.open("my_backup_file.tgz", "r:gz") for tarinfo in tarobj: tarobj.extract(tarinfo.name, r"d:/temp/backup") tarobj.close() 十分感謝!
============
把 cygwin/msys/mingw 的 tar.exe、gzip.exe、gunzip.exe 這些一塊拷過去, 使用 os.system() 調用
=============
Windows下最常見的壓縮文件只有兩種,.zip和.rar。可是Linux就不同了,有.Z、bz2、.gz、.tar、.tar.gz等眾多的壓 縮文件名,它們分別對應了各種壓縮打包命令。要了解這些壓縮打包命令的使用,首先要弄清兩個概念:打包和壓縮。打包是指將一大堆文件或目錄什麼的變成一個 總的文件,壓縮則是將一個大的文件通過一些壓縮算法變成一個小文件。Linux中的很多壓縮命令只能針對一個文件進行壓縮,當要壓縮一大堆文件時,就得先 藉助打包命令將這一大堆文件先打成一個包,然後再用壓縮命令進行壓縮。因此打包命令在Linux的應用中具有很重要的作用。 Linux下最常用的打包命令就是tar,使用tar命令打包後,就可以用其它的命令來進行壓縮了。tar命令的使用方法如下: tar [-cxtzjvfpPN] 文件與目錄 參數說明: -c :建立一個打包文件; -x :解開一個打包文件; -t :查看 tar包裏面的文件; (特別注意,在選擇參數時,c/x/t僅能存在一個,不可同時存在,因為不可能同時壓縮與解壓縮。) -z :打包後用gzip壓縮,生成.tar.gz文件; -j :打包後用zip2壓縮,生成.tar.bz2文件; -v :壓縮的過程中顯示文件; -f :使用文件名,請留意,在f之後要立即接文件名,不要再加其它參數; -p :保持原文件的屬性; -P :使用絕對路徑來壓縮; -N :設定日期(yyyy/mm/dd),比後面接的日期還要新的文件才會被打包進新建的文件中; –exclude FILE:在打包的過程中,不要將FILE打包。 舉幾個例子: 例一:將整個/etc目錄下的文件全部打包成為/tmp/etc.tar tar -cvf /tmp/etc.tar /etc #僅打包,不壓縮 tar -zcvf /tmp/etc.tar.gz /etc #打包後,以gzip壓縮 tar -jcvf /tmp/etc.tar.bz2 /etc #打包後,以bzip2壓縮 例二:查閱上述/tmp/etc.tar.gz文件內有哪些文件 tar -ztvf /tmp/etc.tar.gz 例三:將/tmp/etc.tar.gz文件解壓縮到/usr/local/src下 cd /usr/local/src #先將工作目錄變換到/usr/local/src下 tar -zxvf /tmp/etc.tar.gz 例四:只將/tmp/etc.tar.gz內的etc/passwd解壓到/tmp下 cd /tmp tar -zxvf /tmp/etc.tar.gz etc/passwd 例五:將/etc內的所有文件備份下來,並且保存其權限! tar -zxvpf /tmp/etc.tar.gz /etc 例六:在/home當中,比2005/06/01新的文件才備份 tar -N '2005/06/01' -zcvf home.tar.gz /home 例七:備份/home、/etc,但不要/home/dmtsai tar –exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc 例八:將/etc打包後直接解開在/tmp底下,而不產生文件! cd /tmp tar -cvf – /etc | tar -xvf – 現在有一個需求,不知道該如何才能實現 壓縮: tar czvf /data/backup/test.tar.gz /data/a/b/directory 解壓: cd /data/test tar xzvf /data/backup/test.tar.gz 問題是,解壓後的文件,在/data/test/data/a/b/directory裏面 能否壓縮時只保留directory以下的所有目錄,以directory作為/,而不是/data/a/b/directory? 問題已經解決,找到了GNU tar的官方資料 http://www.delorie.com/gnu/docs/tar/tar_98.html 這樣寫就可以解決了 tar czvf /data/backup/test.tar.gz /data/a/b/directory 改成 tar czvf /data/backup/test.tar.gz -C /data/a/b(空格)directory -C是臨時切換工作目錄,-P是絕對路徑,在這裡只用到-C參數就行了
================
之前寫了一個自動解壓壓縮文件到壓縮文件所在文件夾的腳本 後根據自己需要,寫了另外兩個。原理一樣 都是使用winrar的命令 第一個腳本沒考慮周到,只能解壓rar文件 改進後可以支持winrar支持的各種文件 把指定文件夾下的文件保存到指定文件夾 #rardir.py import os import sys src=sys.argv[1] dst=sys.argv[2] format=['rar','zip','7z','ace','arj','bz2','cab','gz','iso','jar','lzh','tar','uue','z'] os.chdir(sys.argv[1]) for file in os.listdir('.'): if os.path.isfile(file) and (os.path.splitext(file)[1][1:].lower() in format)==True: #cmd='winrar x -ibck "'+file+'" "'+dst+'//'+os.path.splitext(file)[0]+'//"' cmd='winrar x -ibck "'+file+'" "'+dst+'//"' os.system(cmd) os.remove(file) print('done '+file) 第一個版本的改進 #rardecmp.py #decompress with winrar #arguments :filename directory opt # opt='mkdir' to create directory with the correspond filename # opt='direct' to decompress rar files in current directory # opt='mk&del' to mkdir and delete rar file import os import sys if len(sys.argv)!=3: print ('wrong arguments/n') print ('rar.py directory opt/n') print ('opt=/'mkdir/' to create directory with the correspond filename/n') print ('opt=/'direct/' to decompress rar files in current directory/n') print ('opt=/'diredel/' to decompress rar files in current directory and delete files/n') print ('opt=/'mkdel/' to mkdir and delete rar file/n') exit(0) #-ibck ,minimized when running opt=sys.argv[2] os.chdir(sys.argv[1]) format=['rar','zip','7z','ace','arj','bz2','cab','gz','iso','jar','lzh','tar','uue','z'] for file in os.listdir('.'): if os.path.isfile(file) and (os.path.splitext(file)[1][1:].lower() in format)==True: if opt=='mkdir': cmd='winrar x -ibck "'+file+'"'+' "'+os.path.splitext(file)[0]+'"//' os.system(cmd) elif opt=='direct': cmd='winrar x -ibck "'+file+'"' os.system(cmd) ……