美文网首页
linux 解压zip中文乱码问题

linux 解压zip中文乱码问题

作者: Qiuty | 来源:发表于2018-06-21 13:39 被阅读0次

    本文用程序解决的的,有兴趣可以看一看,limux有解决中文乱码问题,,请去其他博客。
    方法一:使用java

    importjava.io.File;importjava.io.FileOutputStream;
    importjava.io.IOException;
    importjava.io.InputStream;
    importjava.util.Enumeration;importorg.apache.tools.zip.ZipEntry;
    importorg.apache.tools.zip.ZipFile; 
    classUnZip{ public static void main(String[] args) throws IOException {
            //1.找到目录下需要解压的文件        String fileAddress = "××××××";//压缩文件的源位置          String unZipAddress = "××××××";//解压文件的目的位置        File file = new File(fileAddress);
            ZipFile zipFile = null;
            try {
                zipFile = new ZipFile(file,"GBK");//设置编码格式        } catch (IOException exception) {
                exception.printStackTrace();
                System.out.println("解压文件不存在!");
            }//2.对文件处理        Iterator e = (Iterator)zipFile.getEntries();//迭代处理压缩文件里面的文件        while(e.hasNext()) {
                ZipEntry zipEntry = (ZipEntry)e.next();
                if(zipEntry.isDirectory()) {//如果文件存在,按照压缩文件名字,创建压缩文件                String name = zipEntry.getName();
                    name = name.substring(0,name.length()-1);
                    File f = new File(unZipAddress + name);
                    f.mkdirs();
                } else {//如果文件不存在,创建新的文件                File f = new File(unZipAddress + zipEntry.getName());
                    f.getParentFile().mkdirs();
                    f.createNewFile();
                    InputStream is = zipFile.getInputStream(zipEntry);
                    FileOutputStream fos = new FileOutputStream(f);
                    int length = 0;
                    byte[] b = new byte[1024];
                    int i=0;//解压输出到文件中,并在界面上打印进度                while((length=is.read(b, 0, 1024))!=-1) {
                        fos.write(b, 0, length);
                        i++;
                        System.err.print("#");//用错误输出流打印#符                }
                    is.close();//关闭Io流                fos.close();
                }
            }
            //关闭文件和删除压缩包        if (zipFile != null) {
                zipFile.close();
            }
            file.deleteOnExit();//解压完以后将压缩包删除        System.out.println("\n This file has unziped ,congraduation !!!");
        }
    }
    

    方法二:python代码

    import sys
    import zipfileprint "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.closefile.close()
    

    相关文章

      网友评论

          本文标题:linux 解压zip中文乱码问题

          本文链接:https://www.haomeiwen.com/subject/mbvqyftx.html