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

本文介绍了一个用于解决RAR文件解压时遇到的中文路径问题的Shell脚本。该脚本可以预先创建中文目录,避免解压过程中的乱码问题,并在解压完成后清理错误创建的目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我的机子上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` <Archive> [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
}</tmp/rarx.$$

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
}</tmp/rarx.Sdir.$$

rm /tmp/rarx.$$
rm /tmp/rarx.Adir.$$
rm /tmp/rarx.Sdir.$$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值