0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

zstd ノススメ(後編): zstddiff について

0
Posted at
  • 前回の最後では、zstd には zstddiff コマンドが用意されていない
    という致命的な欠点について触れた所で終わりました。
    • 今回のテーマは zstddiff をどうするかについてです。

その前に

どうする zstddiff

  • さて本題ですが、そもそもこの内容は AI さんが教えてくれたので、
    記事にするほどのことでも無いですがご容赦ください。
    • まあそれは Qiita の記事全般に言えることかもしれませんが

TL;DR

  • diff コマンドは stdin- で扱えるので、
    zstdcat を使うことで以下のように差分を比較できます。

     $ FILE=hello.txt; zstdcat ${FILE}.zst | diff -NEwur - $FILE
    
     --- -	2025-12-21 16:48:45.198007003 +0900
     +++ hello.txt	2025-12-21 16:48:35.008024901 +0900
     @@ -0,0 +1 @@
     +Hello
    
    • めでたし x2 :smiley:

もうちっとだけ続くんじゃ

パイプを使うのは何か落ち着かない

  • 以下のように <() を用いることで同じ動作になります

     FILE=hello.txt; diff -NEwur <(zstdcat ${FILE}.zst) $FILE
    

やっぱり zstddiff が良い

  • file が *.zst の時だけ diff に zstdcat を使うことを意識するのも
    何か微妙なので、zstddiff を script で作りたいと思います。

     $ cat ~/bin/zstddiff
    
     #!/bin/bash
    
     if [ $# -lt 2 ]
     then
     	echo "[USAGE] ${0##*/} [zst path] [txt path]"
     	exit 1
     fi
    
     diff -NEwur <(zstdcat "$1") "$2"
    

*.zst の方が新しい場合は?

  • zstdcat*.txt も表示できるので以下で OK です。

     diff -NEwur <(zstdcat "$1") <(zstdcat "$2")
    

*.zst じゃないのが来たら?

  • チッうっせーな

  • こんな感じでどうでしょうか。

     #!/bin/bash
    
     show_usage ()
     {
     	echo "[USAGE] ${0##*/} [zst|txt path 1] [zst|txt path 2]"
     	return 1
     }
    
     zstddiff ()
     {
     	if [[ ! "$1" =~ \.zst$ ]] && [[ ! "$2" =~ \.zst$ ]]
     	then
     		show_usage
     		exit $?
     	fi
    
     	diff -NEwur <(zstdcat "$1") <(zstdcat "$2")
     }
    
     if [ $# -lt 2 ]
     then
     	show_usage
     	exit $?
     fi
    
     zstddiff $@
    

せっかく *.gz も扱えるのに *.gz*.txt でできないんだけど?

  • もうええわ!
    • ありがとうございましたー :microphone2:

Epilogue

  • 他にも何か不具合がありましたらご容赦ください。

  • なお zdiffxzdiff も script なので、ご興味があればどうぞ。

     $ file $(which zdiff)
     /usr/bin/zdiff: POSIX shell script, ASCII text executable
    
     $ cat $(which zdiff) | less
    
     $ file $(which xzdiff)
     /usr/bin/xzdiff: POSIX shell script, ASCII text executable
    
     $ cat $(which xzdiff) | less
    
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?