Pythontarで作成されたtar.xzファイルがmacOStarの15分の1であるのはなぜですか

翻訳。:これは別の記事に基づいていないため、通常の翻訳ではありませんが、今月リソースの主なヒットとなったStackExchangeの最近の事例です。その作者は質問をしますが、その答えは一部のサイト訪問者にとって本当の啓示であることが判明しました。





ディレクトリを約1.3GB圧縮し、それぞれに1440のJSONファイルを含めると、tar



macOSまたはRaspbian 10(Buster)を使用して圧縮されたアーカイブと、Pythonに組み込まれtarfileライブラリを使用して取得されたアーカイブのサイズに15倍の違いが見つかりました





最小限の作業例

このスクリプトは、両方の方法を比較します。





#!/usr/bin/env python3

from pathlib import Path 
from subprocess import call 
import tarfile

fullpath = Path("/Users/user/Desktop/temp/tar/2021-03-11") 
zsh_out = Path(fullpath.parent, "zsh-archive.tar.xz") 
py_out = Path(fullpath.parent, "py-archive.tar.xz")

# tar using terminal 
# tar cJf zsh-archive.tar.xz folderpath
call(["tar", "cJf", zsh_out, fullpath])

# tar using tarfile library 
with tarfile.open(py_out, "w:xz") as tar:
    tar.add(fullpath, arcname=fullpath.stem)

# Print filesizes 
print(f"zsh tar filesize: {round(Path(zsh_out).stat().st_size/(1024*1024), 2)} MB") 
print(f"py tar filesize: {round(Path(py_out).stat().st_size/(1024*1024), 2)} MB")
      
      



結果は次のとおりです。





zsh tar filesize: 23.7 MB
py tar filesize: 1.49 MB
      
      



次のバージョンが使用されました。





  • tar



    MacOSの場合:bsdtar 3.3.2 - libarchive 3.3.2 zlib/1.2.11 liblzma/5.0.5 bz2lib/1.0.6



    ;





  • tar



    10時のラズビアン:xz (XZ Utils) 5.2.4 liblzma 5.2.4



    ;





  • tarfile



    Python: 0.9.0



    .





:





diff -r py-archive-expanded zsh-archive-expanded
      
      



.





« » ( ) :





➜ diff zsh-archive.tar.xz py-archive.tar.xz
Binary files zsh-archive.tar.xz and py-archive.tar.xz differ
      
      



Quicklook ( Betterzip) , -:





左側はzsh-archive.tar.xz、右側はpy-archive.tar.xzです。
— zsh-archive.tar.xz, — py-archive.tar.xz.

zsh



, Python — . , .





? ? , Python- ? 15- - Python-?





: , tarlib



Python ; BSD- tar



.





:

, , BSD- GNU- tar



.





GNU tar



--sort



:





ORDER



, none



, name



inode



.





--sort=none



— , .





GNU tar

GNU tar



Mac:





brew install gnu-tar
      
      



'tar' , --sort



:





gtar --sort='name' -cJf zsh-archive-sorted.tar.xz /Users/user/Desktop/temp/tar/2021-03-11
      
      



zsh-archive-sorted.tar.xz



1,5 — , , Python-.





, , JSON-, ( — unixtime), BSD tar



:





cat *.json > all.txt
tar cJf zsh-cat-archive.tar.xz all.txt
      
      



zsh-cat-archive.tar.xz



1,5 .





Python- tarfile

, TarFile.add Python , tarfile



Python :





. , recursive False. .





, , , :





JSON- . , .





, . , .





P.S.

UPD: — XZ/LZMA — , @iliazeus!





:





  • «Git happens! 6 Git »;





  • « »;





  • « ».








All Articles