zoukankan      html  css  js  c++  java
  • [bug]使用SharpZipLib遇到中文名称乱码问题

    写在前面

    业务逻辑是这样的,需要导出一个app的话题,并需要把该话题下帖子的附件导出,然后需求想修改成人员的名称.jpg的格式。所以就出现了中文乱码的问题。在本地没问题,但发布到服务器上面就出问题,每次打包下载下面的只有英文名称的文件,没有中文的。

    工具类

       public class ZipHelper
        {
            public static void ZipFile(string strFile, string strZip)
            {
                if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
                    strFile += Path.DirectorySeparatorChar;
                ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
                s.SetLevel(6);
                zip(strFile, s, strFile);
                s.Finish();
                s.Close();
            }
            private static void zip(string strFile, ZipOutputStream s, string staticFile)
            {
                if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
                Crc32 crc = new Crc32();
                string[] filenames = Directory.GetFileSystemEntries(strFile);
                foreach (string file in filenames)
                {
    
                    if (Directory.Exists(file))
                    {
                        zip(file, s, staticFile);
                    }
    
                    else // 否则直接压缩文件
                    {
                        //打开压缩文件
                        FileStream fs = File.OpenRead(file);
    
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string tempfile = file.Substring(staticFile.LastIndexOf("\") + 1);
                        ZipEntry entry = new ZipEntry(tempfile);
                        //设置wei unicode编码
                        entry.IsUnicodeText = true;
                        entry.DateTime = DateTime.Now;
                        entry.Size = fs.Length;
                        fs.Close();
                        crc.Reset();
                        crc.Update(buffer);
                        entry.Crc = crc.Value;
                        s.PutNextEntry(entry);
    
                        s.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }

    该类是从网上拷贝过来的,原文地址:http://blog.csdn.net/nieweiking/article/details/8663837

    解决乱码办法

    http://www.cnblogs.com/leodrain/archive/2009/05/04/fixed-sharpziplib-fastzip-not-surport-chinese-charater-file.html

    为对象ZipEntry,添加属性

     //设置wei unicode编码
                        entry.IsUnicodeText = true;
  • 相关阅读:
    js 常用方法
    Request.UrlReferrer
    批处理定时打开一个网页
    js基础知识总结:函数
    .NET DataTable转换为JSON格式的字符串
    .NET解析xml字符串,通过反射给实体类对象赋值,获取实体类数据列表
    .NET错误提示之:无法更新EntitySet“TableName”因为它有一个DefiningQuery
    python中安装web.py
    pycharm中运行flask项目安装flask
    jmeter--查看结果数中响应数据部分乱码
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/5146101.html
Copyright © 2011-2022 走看看