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?

2つの zip ファイルに含まれるファイル内容の比較

Posted at

やりたいこと

zip ファイルに圧縮されたファイルの内容が、2つの zip ファイルで同じ内容かを調べる。
ファイルの作成日時などは気にせず、ファイルの内容が同じかを確認したい。

zip ファイルの作成

検証用にディレクトリ毎ファイルを圧縮して、test1.zip と test2.zip を作成する。

ディレクトリ構成

$ echo "a" > test/test_a.txt
$ cat test/test_a.txt
a

$ echo "b" > test/test_b.txt
$ cat test/test_b.txt
b

$ tree test
test
├── test_a.txt
└── test_b.txt

zip ファイルの作成

単純に zip で圧縮して test1.zip を作成する。

$ zip -r test1.zip test
updating: test/ (stored 0%)
  adding: test/test_a.txt (stored 0%)
  adding: test/test_b.txt (stored 0%)

ファイルの内容を上書きしてタイムスタンプを変更してから test2.zip を作成する。

$ echo "a" > test/test_a.txt
$ cat test/test_a.txt
a

$ echo "b" > test/test_b.txt
$ cat test/test_b.txt
b

$ zip -r test2.zip test
  adding: test/ (stored 0%)
  adding: test/test_a.txt (stored 0%)
  adding: test/test_b.txt (stored 0%)

zip ファイルの差分

diff による差分

$ diff test1.zip test2.zip
Binary files test1.zip and test2.zip differ

zip ファイル内の各ファイルのタイムスタンプが異なるため、test1.zip と test2.zip の diff をとると2つの zip ファイルは異なるファイルと判定される。

ファイルの内容による差分

unzip -p でファイルの内容をファイルに出力し、ファイルの内容で差分を確認する。

$ unzip -p test1.zip > test1.bin
$ unzip -p test2.zip > test2.bin
$ diff test1.bin test2.bin

拡張子を bin としたが、test1.bin、test2.bin はテキストファイルのため、diff では差分が検出されない。

$ cat test1.bin
a
b

$ cat test2.bin
a
b

同様に MD5 を作成して MD5 を比較することもできる。

$ unzip -p test1.zip | md5sum
dd8c6a395b5dd36c56d23275028f526c  -

$ unzip -p test2.zip | md5sum
dd8c6a395b5dd36c56d23275028f526c  -
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?