将状态(文件完整性)检查添加到cbr cbz转换bash脚本

时间:2013-02-27 11:07:26

标签: bash zip 7zip rar

第一篇文章,嗨!首先让我说我是一个关于编程的总菜鸟。我理解非常基本的东西,但是当谈到检查退出代码或适当的术语时,我很茫然。显然我的搜索结果在这个领域真的很弱,我想这是一个术语问题。

提前感谢您抽出宝贵时间阅读本文/回答我的问题!

说明:我找到了script转换/重新打包.cbr文件到.cbz文件。这些文件基本上是您的平均rarzip个文件,但是重命名为另一个扩展程序,因为它们用于(漫画)图书应用程序,例如comicrackqcomicbook 。令人惊讶的是,没有cbr - > cbz转换器在那里。 .cbz的优点是除了转义专有 rar文件格式之外,还可以使用e存储来自Comic Vine的元数据。 g comictagger

问题:有时重新打包文件并不能很好地结束,并且希望通过完整性检查来解决这个问题。另一个去。我稍微修改了所说的脚本以使用p7zip,因为它可以打包/解压缩7z,zip文件和some others,i。非常适合选择。 p7zip可以通过以下方式测试存档:

7z t comicfile.cbz tmpworkingdir

我想这是使用if& amp;否则在这里(?)检查完整性,然后再给它一次,如果有任何错误。

问题/ tl;博士:在下面的脚本中添加完整性文件检查的“最佳”/适当方法是什么?

#!/bin/bash
#Source: http://comicrack.cyolito.com/forum/13-scripts/30013-cbr3cbz-rar-to-zip-conversion-for-linux
echo "Converting CBRs to CBZs"

# Set the "field separator" to something other than spaces/newlines" so that spaces
# in the file names don't mess things up. I'm using the pipe symbol ("|") as it is very
# unlikely to appear in a file name.
IFS="|"

# Set working directory where to create the temp dir. The user you are using must have permission
# to write into this directory.
# For performance reasons I'm using ram disk (/dev/shm/) in Ubuntu server.
WORKDIR="/dev/shm/"

# Set name for the temp dir. This directory will be created under WORDDIR
TEMPDIR="cbr2cbz"

# The script should be invoked as "cbr2cbz {directory}", where "{directory}" is the
# top-level directory to be searched. Just to be paranoid, if no directory is specified,
# then default to the current working directory ("."). Let's put the name of the
# directory into a shell variable called SOURCEDIR.
# Note: "$1" = "The first command line argument"
if test -z "$1"; then
        SOURCEDIR=`pwd`
else
        SOURCEDIR="$1"
fi

echo "Working from directory $SOURCEDIR"

# We need an empty directory to work in, so we'll create a temp directory here
cd "$WORKDIR"
mkdir "$TEMPDIR"
# and step into it
cd "$TEMPDIR"

# Now, execute a loop, based on a "find" command in the specified directory. The
# "-printf "$p|" will cause the file names to be separated by the pipe symbol, rather than
# the default newline. Note the backtics ("`") (the key above the tab key on US
# keyboards).
for CBRFILE in `find "$SOURCEDIR" -name "*.cbr" -printf "%p|while read line; do

    # Now for the actual work. First, extract the base file name (without the extension)
    # using the "basename" command. Warning: more backtics.
    BASENAME=`basename $CBRFILE ".cbr"`

    # And the directory path for that file, so we know where to put the finished ".cbz"
    # file.
    DIRNAME=`dirname $CBRFILE`

    # Now, build the "new" file name,
    NEWNAME="$BASENAME.cbz"

    # We use RAR file's name to create folder for unpacked files
    echo "Processing $CBRFILE"
    mkdir "$BASENAME"
    # and unpack the rar file into it
    7z x "$CBRFILE" -O"$BASENAME"
    cd "$BASENAME"

    # Lets ensure the permissions allow us to pack everything
    sudo chmod 777 -R ./*

# Put all the extracted files into new ".cbz" file
    7z a -tzip -mx=9 "$NEWNAME" *

    # And move it to the directory where we found the original ".cbr" file
    mv "$NEWNAME" $DIRNAME/"$NEWNAME"

    # Finally, "cd" back to the original working directory, and delete the temp directory
    # created earlier.
    cd ..
    rm -r "$BASENAME"

    # Delete the RAR file also
    rm "$CBRFILE"

done

# At the end we cleanup by removing the temp folder from ram disk
cd ..
echo "Conversion Done"
rm -r "$TEMPDIR"

噢,人性化,没有在10个声誉之前发布超过两个链接而且我把OP挂掉了OP .. [编辑]啊...... mh-mmm ..我们去.. < / p>

[edit 2] 我删除了unrar作为依赖项,而是使用p7zip,因为它可以提取rar文件。

1 个答案:

答案 0 :(得分:2)

您需要进行两次检查:

  1. 7z t将测试存档
  2. 的完整性
  3. 您还应该测试存档中所有图像文件的完整性。您可以使用ImageMagick之类的工具进行此操作。

    一个简单的测试是identify file ,但这可能只读取标题。我使用convert 文件 -resize 5x5 png:- > /dev/null

    这会将图像缩小到5x5像素,将其转换为PNG,然后将结果传送到/dev/null(丢弃它)。对于缩放,必须读取整个图像。如果此命令失败并显示错误,则图像文件出现问题。