红联Linux门户
Linux帮助

Linux下rar解压中文文档会出错的脚本

发布时间:2007-07-02 15:41:24来源:红联作者:Stickalso
在我的机子上rar解压中文文档总是出错 估计是字符编码的问题。因为我后来看到总是rar在建立中文目录的时候犯错,所以写了这个脚本 先把目录都建立起来 然后再解压,然后把解压过程中建立的错误中文目录删掉 就是酱紫的,想想估计应该可能对别人会有用就放上来了 呵呵。

这个脚本假设你的rar文档里面根目录只有一个,两个的话 对于错误目录的删除会只删第一个目录。介个懒得改了 呵呵

x@XEROO:~$ cat rarx.sh

#!/bin/bash
#
# Author: JayXie
# Date: 2006-03-24
# Email: oxwjo@163.com
#

E_BADARGS=65
E_NOFILE=66
E_NODIR=67

help()
{
echo -e "Usage: `basename $0` [PathTo]\n"
}

if [ $# -eq 0 ]
then
help
exit $E_BADARGS
fi

while [ -n "$1" ]; do
case $1 in
-h) help;exit 0;;
-*) echo -e "error: no such option $1. -h for help\n";exit $E_BADARGS;;
*) break;;
esac
done

if [ ! -f "$1" ]
then
echo -e "File \"$1\" does not exist.\n"
exit $E_NOFILE
fi

# Get file list from archive
rar vt $1 | tac | sed -e '1,/--------/d' > /tmp/rarx.$$

# Make directorys
current_dir=`pwd`

if [ -n "$2" ]
then
if [ ! -d "$2" ]
then
echo -e "Directory \"$2\" dose not exist.\n"
exit $E_NODIR
fi
cd "$2"
fi

{
while read line
do
case $line in
*.D....*) read line;echo "$line" >> /tmp/rarx.Adir.$$;mkdir -p "$line";;
------*) break;;
*);;
esac
done
}
cd "$current_dir"

# Unrar archive
rar x $1 $2

# Remove wrong directorys
read first_dir < /tmp/rarx.Adir.$$
if [ -n "$2" ]
then
cd $2
fi
ls -A -c1 > /tmp/rarx.Sdir.$$
ls $first_dir -ARF -c1 | sed -n /:/p | sed s/://g >> /tmp/rarx.Sdir.$$
{
while read line
do
if [ -d "$line" ]
then
grep -q "$line" /tmp/rarx.Adir.$$
if [ $? -eq 0 ]
then
grep -q "$line$" /tmp/rarx.Adir.$$
if [ $? -eq 1 ]
then
rmdir "$line"
fi
fi
fi
done
}
rm /tmp/rarx.$$
rm /tmp/rarx.Adir.$$
rm /tmp/rarx.Sdir.$$
文章评论

共有 0 条评论