如题,对图形格式的文件没有问题
但是如果是文本文件的话就会出现解压缩后保存的文件内容是乱码?实在不知道如何解决,在网上找了问题也没有。
以下是解压缩代码:      private static byte[] DecompressByteData(byte[] dataValue)
       {
            byte[] resultValue = null;
            try
            {
                //解压缩数据
                Inflater decompressFile = new Inflater();
                decompressFile.SetInput(dataValue);
                //内存文件流对象
                MemoryStream smsStream = new MemoryStream();
                byte[] bufData = new byte[1024];
                while (!decompressFile.IsFinished) 
                {
                    int bufLength = decompressFile.Inflate(bufData);
                    smsStream.Write(bufData, 0, bufLength);
                }
                resultValue = smsStream.ToArray();
                smsStream.Close();
            }
            catch(Exception ex)
            {
                System.Console.WriteLine("解压缩数据失败!"+ex.Message);
            }            //返回数据
            return resultValue;
        }    或者怎样在压缩前就指定文件格式并且一并压缩呢?

解决方案 »

  1.   

    文本文件通过StreamWriter指定编码再写入。
      

  2.   

    看错了,压缩的代码怎么写的,请确定压缩的时候文本文件编码没有问题。
    dataValue是压缩以后的数据吧?
      

  3.   

    http://msdn2.microsoft.com/zh-cn/library/system.io.compression.gzipstream(VS.80).aspx
      

  4.   

           private static byte[] CompressByteData(Byte[] dataValue)
            {
                byte[] resultValue = null;
                try
                {
                    long m = dataValue.Length;
                    //压缩数据
                    Deflater compressFile = new Deflater(Deflater.BEST_COMPRESSION);
                    compressFile.SetInput(dataValue);
                    compressFile.Finish();
                    //内存文件流对象
                    MemoryStream smsStream = new MemoryStream();
                    byte[] bufData = new byte[1024];
                    while (!compressFile.IsFinished)
                    {
                        int bufLength = compressFile.Deflate(bufData);
                        smsStream.Write(bufData, 0, bufLength);
                    }
                    resultValue = smsStream.ToArray();
                    smsStream.Close();            }
                catch (Exception e)
                {
                    System.Console.WriteLine("压缩数据失败!" + e.Message);
                }            //返回压缩后的数据
                long n = resultValue.Length;
                return resultValue;}这是压缩的方法
    如果我要加入编码命令如何解决呢?
    真抱歉打扰大家
      

  5.   

    dataValue怎么得来的那部分代码贴一下,压缩这部分没有问题,看看是不是文本文件读入到byte数组的时候有问题。
      

  6.   

    换个思路也可以因为我的二进制数组是通过httppostfile文件转换过来的能否在转换为二进制数组前把httppostfile文件转化为utf8格式的然后再转换过来因为我下面是这样用的oFiles[1]是httppostfile对象
    我想在这个前面把httppostfile文件转化成utf8的然后再变成httppostfile对象能否实现byte[] b = new byte[oFiles[1].ContentLength];
                        
                        System.IO.Stream fs = (System.IO.Stream)oFiles[1].InputStream;
                        fs.Read(b, 0, oFiles[1].ContentLength);
      

  7.   

    上面的代码也没有问题,一直是二进制流,没有涉及到文本到字节的转换,
    你能否测试oFiles[1]直接保存以后的结果,应该和上传文件一致。
      

  8.   

    你能举例一下你用的文件么?程序是asp.net的吧?
      

  9.   


    Encoding gb2312 = Encoding.GetEncoding("gb2312");
    FileStream fs = new FileStream("g:\\temp\\gb2312.txt", FileMode.Open);
    StreamReader sr = new StreamReader(fs, gb2312);
    MemoryStream ms = new MemoryStream();
    string line = null;
    byte[] buf;
    while ((line = sr.ReadLine()) != null)
    {
        buf = gb2312.GetBytes(line + "\r\n");
        ms.Write(buf, 0, buf.Length);
    }
    ms.Close();
    sr.Close();
    fs.Close();int bytes;
    Deflater deflater = new Deflater();
    deflater.SetInput(ms.ToArray());
    buf = new byte[1024];
    ms = new MemoryStream();
    while (!deflater.IsNeedingInput)
    {
        bytes = deflater.Deflate(buf);
        if (bytes <= 0)
            break;
        ms.Write(buf, 0, bytes);
    }
    deflater.Finish();
    while (!deflater.IsFinished)
    {
        bytes = deflater.Deflate(buf);
        if (bytes <= 0)
            break;
        ms.Write(buf, 0, bytes);
    }
    ms.Close();Inflater inflater = new Inflater();
    inflater.SetInput(ms.ToArray());
    ms = new MemoryStream();
    while (!inflater.IsFinished)
    {
        bytes = inflater.Inflate(buf);
        if (bytes <= 0)
            break;
        ms.Write(buf, 0, bytes);
    }
    ms.Close();buf = ms.ToArray();
    fs = new FileStream("g:\\temp\\gb2312.txt.bak", FileMode.Create);
    fs.Write(buf, 0, buf.Length);
    fs.Close();gb2312.txt内容
    好好学习天天向上。
    锄禾日当午。
    火星。程序运行结果gb2312.txt和gb2312.txt.bak内容一致。
      

  10.   

    这是我原来的上传代码System.Web.HttpFileCollection oFiles = System.Web.HttpContext.Current.Request.Files;
    string FilePath = oFiles[1].FileName;
    System.IO.Stream fs = (System.IO.Stream)oFiles[1].InputStream;
    fs.Read(b, 0, oFiles[1].ContentLength);
    byte[] bb = CompressByteData(b);            //对二进制数组进行压缩upfile(bb); 方法调用进行保存
      

  11.   


    FileStream fs = new FileStream("g:\\temp\\gb2312.txt", FileMode.Open);
    MemoryStream ms = new MemoryStream();
    string line = null;
    byte[] buf = byte[1024];
    int bytes;
    while ((bytes = fs.Read(buf, 0, buf.Length)) != 0)
    {
        ms.Write(buf, 0, bytes);
    }
    ms.Close();
    fs.Close();
    这段代码跟文件编码无关直接读二进制流。
    你的程序现在是前台页面上传文件,后台程序压缩然后保存,图片压缩保存正常,文本文件出现乱码。
    你可以把上传以后的内容直接保存来验证一下是否上传的数据没有问题,我看你压缩解压那里的代码
    都没有问题呀。
      

  12.   

    我的msn,[email protected]