2015-04-05 68 views
3

我注意到,tar文件沒有AW:XZ選項或類似的東西,有沒有什麼辦法來創建一個XZ文件我有蟒蛇tar文件創建XZ文件

dir=tkFileDialog.askdirectory(initialdir="/home/david") 
     if x.get()=="gz": 
      tar = tarfile.open(dir+".tar.gz", "w:gz") 
      tar 
      for i in range(lbox.size()): 
       tar.add(e1.get()+"/"+lbox.get(i),arcname=lbox.get(i)) 
      tar.close() 
     if x.get()=="bz": 
      tar = tarfile.open(dir+".tar.gz", "w:bz2") 
      tar 
      for i in range(lbox.size()): 
       tar.add(e1.get()+"/"+lbox.get(i),arcname=lbox.get(i)) 
      tar.close() 
     if x.get()=="xz": 
      tar = tarfile.open(dir+".tar.gz", "w:gz") 
      tar 
      for i in range(lbox.size()): 
       tar.add(e1.get()+"/"+lbox.get(i),arcname=lbox.get(i)) 
      tar.close() 

回答

3

Python版本3.3的代碼和以上有你正在尋找的選項。

'w:xz' - 打開lzma壓縮文字。

https://docs.python.org/3.3/library/tarfile.html

對於低於3.3版本,你可以試試以下

  • 假設你在前面的代碼值分配給查找inputfilename和outputFilename。
  • 注意使用關鍵字自動關閉執行縮進代碼後的文件

示例代碼:

import lzma 

# open input file as binary and read input data 
with open(inputFilename, 'rb') as iFile: 
    iData = iFile.read() 

# compress data 
oData = lzma.compress(iData) 

# open output file as binary and write compressed data 
with open(outputFilename, 'wb') as oFile: 
    oFile.write(oData) 

我搜索了其他答案,我發現,上述問題中的條目導入lzma到python 2.7。在此條目中介紹了一種可以遵循的解決方法。

這裏是鏈接 - Python 2.7: Compressing data with the XZ format using the "lzma" module

+0

我已經嘗試了LZMA模塊,但是Python instatly關閉,任何想法,爲什麼還?W:XZ不工作我想我的Python版本,應該是問題。 ... – Ulrok 2015-04-05 02:36:29

+0

完美的作品,謝謝 – Ulrok 2015-04-05 02:48:03

+0

你不寫作並關閉oFile – 2015-04-05 05:39:03