zip文件中的文件而不提取它

Gum*_* Bi 11 linux diff zip unzip

有没有办法在两个拉链中的两个文件上执行diff operetion而不提取它们?如果没有 - 任何其他解决方法来比较它们而不提取?

谢谢.

sys*_*ral 8

unzip -l将列出zip文件的内容.然后你可以diff按照这里提到的正常方式传递它:https://askubuntu.com/questions/229447/how-do-i-diff-the-output-of-two-commands

例如,如果您有两个zip文件:

foo.zip
bar.zip
Run Code Online (Sandbox Code Playgroud)

您可以运行diff -y <(unzip -l foo.zip) <(unzip -l bar.zip)以对两个文件的内容进行并排差异.

希望有所帮助!

  • 添加`--suppress-common-lines`标志只显示不同的行对我来说非常好:`diff -y <(unzip -l foo.zip)<(unzip -l bar.zip) - 抑制-共lines` (5认同)
  • 我最终得到了`function zipdiff(){diff -y <(unzip -l $ 1)<(unzip -l $ 2)--suppress-common-lines; 对于我想做的事情,这完美无瑕. (3认同)

小智 8

结合到目前为止的响应,以下bash函数将比较zip文件中的文件列表.列表包括详细输出(unzip -v),因此可以比较校验和.输出按filename(sort -k8)排序,以允许并排比较,diff输出expand(W200),因此文件名在并排视图中可见.

function zipdiff() { diff -W200 -y <(unzip -vql $1 | sort -k8) <(unzip -vql $2 | sort -k8); }
Run Code Online (Sandbox Code Playgroud)

这可以添加到您的~/.bashrc文件中,以便从任何控制台使用.它可以用于zipdiff a.zip b.zip.将输出管道设置为较小或重定向到文件对于大型zip文件很有用.


Ian*_*Ian 7

仅压缩文件内容

长话短说

a.zip比较 2 个 zip 文件(和)的命令b.zip

diff \
  <(unzip -vqq a.zip  | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3 -f) \
  <(unzip -vqq b.zip  | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3 -f)
Run Code Online (Sandbox Code Playgroud)

解释

我正在寻找一种方法来比较zip 文件中存储的文件的内容,而不是其他元数据。考虑以下:

$ echo foo > foo.txt
$ zip now.zip foo.txt
  adding: foo.txt (stored 0%)
$ zip later.zip foo.txt
  adding: foo.txt (stored 0%)
$ diff now.zip later.zip 
Binary files now.zip and later.zip differ
Run Code Online (Sandbox Code Playgroud)

从概念上讲,这是没有意义的;我在相同的输入上运行相同的命令并得到 2 个不同的输出!区别在于元数据,它存储文件添加的日期!

$ unzip -v now.zip 
Archive:  now.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       4  Stored        4   0% 04-08-2020 23:27 7e3265a8  foo.txt
--------          -------  ---                            -------
       4                4   0%                            1 file
$ unzip -v later.zip
Archive:  later.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       4  Stored        4   0% 04-08-2020 23:28 7e3265a8  foo.txt
--------          -------  ---                            -------
       4                4   0%                            1 file
Run Code Online (Sandbox Code Playgroud)

注意:为了清楚起见,我在这里手动编辑了第二个文件的时间,从23:2723:28。文件本身中的字段存储秒值(在我的情况下,这是不同的——二进制差异仍然会失败),即使它们没有在命令的输出中表示。

因此,为了仅比较文件,我们必须忽略日期字段。 unzip -vqq将为我们提供更好的总结:

$ unzip -vqq now.zip
       4  Stored        4   0% 04-08-2020 23:27 7e3265a8  foo.txt
Run Code Online (Sandbox Code Playgroud)

因此,让我们屏蔽掉字段(我们不关心日期或压缩指标)并对文件进行排序:

$ unzip -vqq now.zip  | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3 -f
4      7e3265a8 foo.txt
Run Code Online (Sandbox Code Playgroud)


vou*_*rus 6

我希望以可读的格式显示 zip 中文件之间的实际差异。这是我为此目的编写的一个 bash 函数,它使用了 git。如果您已经使用 git 作为正常工作流程的一部分并且可以读取 git diff,那么这将具有良好的用户体验。

# usage: zipdiff before.zip after.zip
function zipdiff {
  current=$(pwd)
  before="$current/$1"
  after="$current/$2"
  tempdir=$(mktemp -d)
  cd "$tempdir"
  git init &> /dev/null
  unzip -qq "$before" *
  git add . &> /dev/null
  git commit -m "before" &> /dev/null
  rm -rf "$tempdir/*"  
  yes | unzip -qq "$after" * &> /dev/null
  git add .
  git diff --cached
  cd "$current"
  rm -rf "$tempdir"
}

Run Code Online (Sandbox Code Playgroud)


gol*_*lum 5

如果您想要diff两个文件(如查看差异),您必须提取它们 - 即使只是内存!

为了查看两个 zip 文件中的两个文件的差异,您可以执行以下操作(没有错误检查或任何其他内容):

# define a little bash function
function zipdiff () { diff -u <(unzip -p $1 $2) <(unzip -p $3 $4); }

# test it: create a.zip and b.zip, each with a different file.txt
echo hello >file.txt; zip a.zip file.txt
echo world >file.txt; zip b.zip file.txt

zipdiff a.zip file.txt b.zip file.txt
--- /dev/fd/63  2016-02-23 18:18:09.000000000 +0100
+++ /dev/fd/62  2016-02-23 18:18:09.000000000 +0100
@@ -1 +1 @@
-hello
+world
Run Code Online (Sandbox Code Playgroud)

注:unzip -p提取文件到p IPE(标准输出)。

如果您只想知道文件是否不同,您可以使用检查它们的校验和

unzip -v -l zipfile [file_to_inspect]
Run Code Online (Sandbox Code Playgroud)

注:-v表示详细和-l列表内容)

unzip -v -l a.zip 
Archive:  a.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       6  Stored        6   0% 2016-02-23 18:23 363a3020  file.txt
--------          -------  ---                            -------
       6                6   0%                            1 file

unzip -v -l b.zip 
Archive:  b.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       6  Stored        6   0% 2016-02-23 18:23 dd3861a8  file.txt
--------          -------  ---                            -------
       6                6   0%                            1 file 
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,您可以看到校验和 (CRC-32) 不同。

你可能也对这个项目感兴趣:https : //github.com/nhnb/zipdiff