如何解决Ubuntu环境下解压中文显示乱码问题? Ubuntu解压中文乱码的多种解决办法

梦回刚读大学时折腾KDE。Linux固有诸多不可替代的优势,然而总能在编码、IO之类的小问题上搞点无伤大雅但需要手动解决的“尴尬”。

Windows压缩文件时,默认以系统编码中文来处理。由于zip文件中没有声明编码,所以 Linux上的unzip解压中文文件名会出现乱码,有三种方式解决问题:

通过unzip命令解压,指定字符集参数

unzip -O GBK filename.zip

也可以尝试将参数GBK替换为CP936GB18030

有趣的是unzip的manual中并无这个选项的说明,unzip --help对这个参数有一行简单的说明。

配置环境变量,总以指定的字符集处理文件

在文件/etc/environment中加入两行

UNZIP="-O GBK"ZIPINFO="-O GBK"

使用The Unarchiver项目提供的lsar/unar工具。

安装:sudo apt-get install unar

使用:unar filename.zip

解压用unar工具,转码用enca。

sudo apt install unar enca

转码脚本encoding.sh

#!/bin/bash#将文件编码更改为UTF-8#用法#1. 将文件命名encoding.sh#2. chmod +x encoding.sh#3. ./set_encoding.sh#4. 输入目录名称#5. 输入是否递归更改#$1表示是否要递归修改文件编码function change_file_encoing(){	for file in $(ls -l|awk '{print $9}')	do		if [[ -d "$file" && $1 = y ]];then			cd $file			echo $file			change_file_encoing $1			cd ..		elif [[ -f "$file" ]];then			echo $file			enca -L zh_CN -x UTF-8 $file		fi;	done;	#ecna -L zh_CN file UTF-8}read -p "please enter the dir path:" path #读取目录路径if [ ! -x "$path" ]; #判断目录是否存在且是否具有执行权限then	echo "dir path not exists"else	read -p "please enter if you want to recursive?y/n:" recur #是否递归fiif [ $recur == "y" ];then	cd $path	change_file_encoing "y" #递归修改文件编码else	cd $path	change_file_encoing "n" #非递归修改fi

至此,可以批量用find,grep组合命令查找匹配中文。

使用p7zip解压

安装p7zip-full

$sudo apt-get install p7zip-full

卸载unzip

$sudo apt-get remove unzip

再下载更新的p7zip-full包把原来的程序替换掉/usr/lib/p7zip/

如何解决Ubuntu环境下解压中文显示乱码问题? Ubuntu解压中文乱码的多种解决办法

PeaZip Portable 压缩解压缩软件 V9.4.0 绿色多国语言版

  • 类型:压缩解压
  • 大小:5.54MB
  • 语言:简体中文
  • 时间:2023-08-21

查看详情

$sudo tar xzvf p7z-bin.tar.gz -C /usr/lib/p7zip

使用锁定p7zip、p7zip-rar和p7zip-full版本(防止更新后失效)

1) 安装新立得软件管理器 sudo apt-get install synaptic(如果已经安装可以略过)

2) 打开新立得软件包管理器,搜索p7zip

3) 在搜索出的结果中,选择p7zip、p7zip-rar和p7zip-full,再点击软件包 -> 设置 -> 锁定版本

文章来自互联网,不代表电脑知识网立场。发布者:芩蕾,转转请注明出处:https://www.pcxun.com/n/36701.html

(0)
上一篇 2024-01-11 06:00
下一篇 2024-01-11 06:00

相关推荐