Linux解压乱码

1.向系统添加windows下的字符编码:
sudo vim /var/lib/locales/supported.d/local
添加一下编码:
zh_CN.GBK GBK
zh_CN.GB2312 GB2312
 
2.向系统中文字符添加相应编码
sudo vim /var/lib/locales/supported.d/zh-hans
添加以下编码:
zh_CN.GBK GBK
zh_CN.GB2312 GB2312
 
3.更新系统编码:
sudo locale-gen

4.使用unzip命令解压:
unzip -O CP936 xxxx.zip
解决办法一,利用pyton来处理
1.vi uzip文件
2.复制一下内容(Python)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# uzip.py
 
import os
import sys
import zipfile
 
print "Processing File " + sys.argv[1]
 
file=zipfile.ZipFile(sys.argv[1],"r");
for name in file.namelist():
    utf8name=name.decode('gbk')
    print "Extracting " + utf8name
    pathname = os.path.dirname(utf8name)
    if not os.path.exists(pathname) and pathname!= "":
        os.makedirs(pathname)
    data = file.read(name)
    if not os.path.exists(utf8name):
        fo = open(utf8name, "w")
        fo.write(data)
        fo.close
file.close()
3.chmod +x uzip
4../uzip xxxx.zip

方法2,通过unzip行命令解压,指定字符集 
unzip -O CP936 xxx.zip (用GBK, GB18030也可以) 
有趣的是unzip的manual中并无这个选项的说明,unzip –help对这个参数有一行简单的说明。

方法3,在环境变量中,指定unzip参数,总是以指定的字符集显示和解压文件
/etc/environment中加入2行 
UNZIP=”-O CP936″
ZIPINFO=”-O CP936″

方法4,采用java的jar命令解压zip包 JAR 解压
jar xvf file.name
7za x your-zip-file.zip
convmv -f GBK -t utf8 --notest -r .
convmv -f gb2312 -t utf8 -r --notest *




1:再压缩前,要设置linux模式, 需要使用第三方ant-1.6.5.jar
  如果是文件目录,则
ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator"));
zipEntry.setUnixMode(755);//解决linux乱码
 
如果是文件,则 
ZipEntry zipEntry=new ZipEntry(base);
zipEntry.setUnixMode(644);//解决linux乱码
 
然后在输出时强制设置编码: 
 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                zipFileName));
        out.setEncoding("GBK");//解决linux乱码
 
全部代码如下: 
 
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
 
import org.apache.tools.ant.Project;  
 
import org.apache.tools.ant.taskdefs.Expand;
 
/**
 * 文件夹压缩,支持win和linux
 * @author wlzhang
 *
 */
public class ZipUtil
{
    /**
     * @param inputFileName
     *            输入一个文件夹
     * @param zipFileName
     *            输出一个压缩文件夹,打包后文件名字
     * @throws Exception
     */
    public static OutputStream zip(String inputFileName, String zipFileName) throws Exception
    {
        return zip(zipFileName, new File(inputFileName));
    }
 
    private static OutputStream zip(String zipFileName, File inputFile) throws Exception
    {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                zipFileName));
        out.setEncoding("UTF-8");//解决linux乱码  根据linux系统的实际编码设置,可以使用命令 vi/etc/sysconfig/i18n 查看linux的系统编码
        zip(out, inputFile, "");
        out.close();
        return out;
    }
 
    private static void zip(ZipOutputStream out, File f, String base) throws Exception
    {
        if (f.isDirectory())
        { // 判断是否为目录
            File[] fl = f.listFiles();
            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
//            out.putNextEntry(new ZipEntry(base + "/"));
            ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator"));
            zipEntry.setUnixMode(755);//解决linux乱码
            out.putNextEntry(zipEntry);
//            base = base.length() == 0 ? "" : base + "/";
            base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator");
            for (int i = 0; i < fl.length; i++)
            {
                zip(out, fl[i], base + fl[i].getName());
            }
        }
        else
        { // 压缩目录中的所有文件
            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
            ZipEntry zipEntry=new ZipEntry(base);
            zipEntry.setUnixMode(644);//解决linux乱码
            out.putNextEntry(zipEntry);
            FileInputStream in = new FileInputStream(f);
            int b;
            while ((b = in.read()) != -1)
            {
                out.write(b);
            }
            in.close();
        }
    }
 
    private static void unzip(String sourceZip,String destDir) throws Exception{  
 
        try{  
 
            Project p = new Project();  
 
            Expand e = new Expand();  
 
            e.setProject(p);  
 
            e.setSrc(new File(sourceZip));  
 
            e.setOverwrite(false);  
 
            e.setDest(new File(destDir));  
 
            /* 
 
            ant下的zip工具默认压缩编码为UTF-8编码, 
 
            而winRAR软件压缩是用的windows默认的GBK或者GB2312编码 
            所以解压缩时要制定编码格式 
            */ 
            e.setEncoding("UTF-8");  //根据linux系统的实际编码设置
            e.execute();  
        }catch(Exception e){  
            throw e;  
        }  
    }  

}
原文地址:https://www.cnblogs.com/timssd/p/4734612.html