不同批次的 zip 目录

Zip directory in different batches

我正在尝试压缩一个包含图像的大型目录,这些图像将被输入深度学习系统。这非常耗时,所以我想提前停止压缩过程 Ctrl + C 并在不同的“批次”中压缩目录。

目前我正在使用 zip -r9v folder.zip folder,并且我看到选项 -u 允许更新更改的文件和添加新文件。

我担心如果我用 Ctrl + C 终止进程,某些文件或 zip 本身会损坏。来自 this answer I understand that the cp can be terminated safely, and this other answer 表明 gzip 也是安全的。

综上所述:提前结束 zip 命令是否安全? -u 选项是否可用于不同批次的压缩?

Is it safe to end prematurely the zip command?

在我的测试中,使用 CtrlC[=28 取消 zip(Info-ZIP,2008 年 6 月 16 日(v3.0)) =] 根本没有创建 zip 存档,即使已经压缩的数据是 2.5GB。因此,我会说 CtrlC 是“安全的”(你不会以损坏的文件结束,但也毫无意义(你做了所有不劳而获)。

Is the -u option viable for zipping in different batches?

是的。 Zip 存档单独压缩每个文件,因此您稍后通过添加文件获得的存档与在单个 运行 中添加所有文件一样好。请记住,开始 zip 也需要时间。因此,将批量大小设置得尽可能高以节省时间。

这是一个脚本,可将您的所有文件添加到 zip 存档中,但有机会在每第 100 个文件时停止压缩。

#! /bin/bash
batchsize=100
shopt -s globstar
files=(folder/**)
echo "Press enter to stop compression after this batch."
for ((startfile=0; startfile<"${#files[@]}"; startfile+=batchsize)); do
  ((startfile==0)) && u= || u=u
  zip "-r9v$u" folder.zip "${files[@]:startfile:batchsize}" 
  u=u
  if read -t 0; then
    echo "Compression stopped before file $startfile."
    echo "Re-run this script with startfile=$startfile to continue".
    exit
  fi
done

为了提高速度,您可能需要研究其他 zip 实施方式。