文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>检查ZIP包文件类型的shell脚本

检查ZIP包文件类型的shell脚本

时间:2008-05-30  来源:crook

今天整理出很久以前的一个shell脚本。当时为什么要写个脚本呢?主要是因为工作上的原因,东西做完之后必须得打个ZIP包传到服务器上。刚开始的时候,经常出现ZIP包里面的文件不对,包进去了不该加进去的文件。后来写了这个脚本,从根本上解决了问题。所以这个脚本的主要功能就是检查一个ZIP包里面的文件格式,是否是有效的文件格式。如果发现不对格式的文件,马上删除。最后生成一个新的ZIP包。     下面是程序代码: 在Cygwin下面写的,处理的是Windows下的文件,所以要注意文件名的大小写。  

#!/bin/sh
# Check whether all the files in ZIP package are valid file type.

# If not, removed the invalid files. Finally, ZIP left files to ZIP package.
#
# Usage : checkfile.sh FILE_NAME DIR
#
# Author : Ray Chen
# Date : 2007-09-27

echo
echo ===================================
echo = Usage : checkfile.sh FILE_NAME DIR
echo =
echo ===================================
echo

usage()
{
    echo "Usage : checkfile.sh FILE_NAME DIR"
    exit;
}

if [ $# -ne 2 ]; then

    usage;
fi

FILE_NAME=$1
DIR=$2
# define a file type list. When added new support file

# type, don't forget to update type_num.

file_type_list=(ver rpt dsp cul gpr bat ada xml txt)
type_num=9

if [ "$FILE_NAME" = "" ]; then
    echo "File name can't be null"
    usage;
fi

if [ -d "$DIR" ]
then
    echo "The checked directory is $DIR"
else
    echo "$DIR doesn't exist"
    usage;
fi

echo
echo ===================================
echo = Step 1 : Unzip $FILE_NAME.zip
echo =
echo ===================================
echo
cd "$DIR"
#ZIP=`echo *`

#ctp_zip=`echo $ZIP | tr A-Z a-z`

# unzip

unzip -o $FILE_NAME.zip

if [ $? -eq 0 ]
then
    echo "Unzip completely"
    # Remove unnecessary files

    rm -f $FILE_NAME.zip $FILE_NAME.ZIP
    rm -f $ctp_zip.ZIP $ctp_zip.zip
else
    echo "Unzip error"
    exit;
fi

echo
echo ====================================
echo = Step 2 : Check $FILE_NAME.zip
echo =
echo ====================================
echo
# Check file type

check_type()
{
    local_name=$1
    local_type=$2
    # change upcase to lowcase

    check_type=`echo "$local_type" | tr A-Z a-z`
    count=0
    while [ $check_type != ${file_type_list[$count]} ]
    do    
        let "count += 1"
        if [ $count -eq $type_num ]; then
            rm -f $local_name
            echo "Removed $local_name file"
            break;
        fi
    done
}

# loop to check

for file in *
do
    file_name=`basename $file`
    # get the file type(3 char)

    file_type=${file_name: -3}
    check_type $file_name $file_type
done

if [ $? -eq 0 ]
then
    echo "Check completely"
else
    echo "Check error"
    exit;
fi

echo
echo ====================================
echo = Step 3 : zip $FILE_NAME.zip
echo =
echo ====================================
echo
zip $FILE_NAME.zip *
if [ $? -eq 0 ]
then
    echo "Zip completely"
else
    echo "Zip error"
fi

 

下面是程序的运行界面:

包里面有个test_package.jar文件, 不符合设定的文件类型,就删除之。最后在/tmp/check目录下生成正确的ZIP包。 运行的时候,确保系统里有ZIP命令。  

$./checkfile.sh /tmp/check/test_package /tmp/check/

===================================
= Usage : checkfile.sh FILE_NAME DIR
=
===================================

The checked directory is /tmp/check/

===================================
= Step 1 : Unzip /tmp/check/test_package.zip
=
===================================

Archive: /tmp/check/test_package.zip
  inflating: test_package_gpr.gpr
  inflating: test_package.jar
  inflating: test_package.ada
  inflating: test_package.bat
  inflating: test_package.cul
  inflating: test_package.DSP
  inflating: test_package.RPT
  inflating: test_package.VER
  inflating: test_package.XML
Unzip completely

====================================
= Step 2 : Check /tmp/check/test_package.zip
=
====================================

Removed test_package.jar file
Check completely

====================================
= Step 3 : zip /tmp/check/test_package.zip
=
====================================

  adding: test_package.DSP (deflated 85%)
  adding: test_package.RPT (deflated 85%)
  adding: test_package.VER (deflated 94%)
  adding: test_package.XML (deflated 55%)
  adding: test_package.ada (deflated 52%)
  adding: test_package.bat (deflated 83%)
  adding: test_package.cul (deflated 77%)
  adding: test_package_gpr.gpr (deflated 65%)
Zip completely

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载