首页 新闻 赞助 找找看

C# 压缩 ICSharpCode.SharpZipLib byte[]压缩

0
悬赏园豆:40 [已关闭问题] 关闭于 2013-07-23 08:57

我想实现一个功能,就是压缩byt[]字节,在U3d的web player中貌似无法操作文件流,不能取得文件的实际路径。我就想针对程序中直接得到的byte数组进行压缩,这个数组大概20多万长度,我现在保存成文本有3mb的样子,我想在得到这些byte的时候先压缩,然后再传到服务器,下次读取时,到达客户端后再解压byte[],

string a = "1";
            if (a == "1")//为1时操作正常
            {  
                //tt.txt大约3.1MB
                //参数1,是待压缩文本路径,参数二是压缩后文件存放位置
                socket_Test.Gzip.ZipAndUnzipFile.GZipFile(@"E:\kankan\1111111111111\tt.txt", @"E:\kankan\1111111111111\ss.txt");
                //参数1,压缩后文件路径,参数2,解压后文件存放位置
                socket_Test.Gzip.ZipAndUnzipFile.UnGzipFile(@"E:\kankan\1111111111111\ss.txt", @"E:\kankan\1111111111111\rr.txt");

            }else 
            if (a == "2")
            {
               // socket_Test.Gzip.ZipAndUnzipFile.UnGzipFile(@"E:\kankan\1111111111111\201305161033314884asdfasdf.txt", @"E:\kankan\1111111111111\201305161033314884jieyasuohou.txt");
                byte[] temp = System.IO.File.ReadAllBytes(@"E:\kankan\1111111111111\tt.txt"); 
                FileStream ffffs = new FileStream(@"E:\kankan\1111111111111\rr.txt", FileMode.OpenOrCreate); 
                Stream s = new MemoryStream(temp);
                StreamReader rader = new StreamReader(s, System.Text.Encoding.UTF8);
                string T_Data = rader.ReadToEnd(); 
                string tData = string.Empty;
                string rData = socket_Test.Gzip.ZipAndUnzipFile.GZipFile(T_Data); 
                 byte[] rByte= Encoding.UTF8.GetBytes(rData); 
                 ffffs.Write(rByte, 0, rByte.Length);
                 ffffs.Close();
            }

上面的直接操作文件流就是正常的,但是下面操作byte[]就错误,求帮助啊!!

下面贴出操作方法代码:

 /// <summary>
       /// 使用GZIP压缩文件的方法
       /// </summary>
        /// <param name="sourcefilename">源文件路径</param>
        /// <param name="zipfilename">压缩文件路径</param>
        /// <returns>返回bool操作结果,成功true,失败 flase</returns>
        public static bool GZipFile(string sourcefilename, string zipfilename)
        {
            bool blResult;//表示压缩是否成功的返回结果
            //为源文件创建读取文件的流实例
            FileStream srcFile = File.OpenRead(sourcefilename);
            //为压缩文件创建写入文件的流实例,
            GZipOutputStream zipFile = new GZipOutputStream(File.Open(zipfilename, FileMode.Create));
            try
            {
                byte[] FileData = new byte[srcFile.Length];//创建缓冲数据
                srcFile.Read(FileData, 0, (int)srcFile.Length);//读取源文件
                zipFile.Write(FileData, 0, FileData.Length);//写入压缩文件
                blResult = true;
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                blResult = false;
            }
            srcFile.Close();//关闭源文件
            zipFile.Close();//关闭压缩文件
            return blResult;
        }
        /// <summary>
        /// 使用GZIP解压文件的方法
        /// </summary>
        /// <param name="zipfilename">源文件路径</param>
        /// <param name="unzipfilename">解压缩文件路径</param>
        /// <returns>返回bool操作结果,成功true,失败 flase</returns>
        public static bool UnGzipFile(string zipfilename, string unzipfilename)
        {
            bool blResult;//表示解压是否成功的返回结果
            //创建压缩文件的输入流实例
            GZipInputStream zipFile = new GZipInputStream(File.OpenRead(zipfilename));
            //创建目标文件的流
            FileStream destFile = File.Open(unzipfilename, FileMode.Create);
            try
            {
                int buffersize = 2048;//缓冲区的尺寸,一般是2048的倍数
                byte[] FileData = new byte[buffersize];//创建缓冲数据
                while (buffersize > 0)//一直读取到文件末尾
                {
                    buffersize = zipFile.Read(FileData, 0, buffersize);//读取压缩文件数据
                    destFile.Write(FileData, 0, buffersize);//写入目标文件
                }
                blResult = true;
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                blResult = false;
            }
            destFile.Close();//关闭目标文件
            zipFile.Close();//关闭压缩文件
            return blResult;
        }
        
         
        public static string GZipFile(string param)
        {
            string rStr = string.Empty; 
            byte[] srcFile = Encoding.UTF8.GetBytes(param);

            MemoryStream ms = new MemoryStream();  
             ICSharpCode.SharpZipLib.Zip.ZipOutputStream zit = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(ms);
             try
             {
                 zit.Write(srcFile, 0, srcFile.Length);//写入压缩文件 
                 rStr = Convert.ToBase64String(ms.ToArray());
             }
             catch (Exception ee)
             {
                 Console.WriteLine(ee.Message); 
             }
             finally {
                 ms.Close(); 
             } 
            return rStr;
        }


        public static string UnGzipFile(string param)
        {
            string rStr = string.Empty;
            byte[] buffer = Convert.FromBase64String(param); 
             
            MemoryStream ms = new MemoryStream(buffer);

            ICSharpCode.SharpZipLib.Zip.ZipInputStream zit = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(ms);

            StreamReader reader = new StreamReader(zit, System.Text.Encoding.UTF8);
            try
            {
                rStr = reader.ReadToEnd();
            }
            catch (Exception e)
            {
                string a = e.Message;
            } 

            return rStr;
        }
View Code
留下的枇杷的主页 留下的枇杷 | 初学一级 | 园豆:6
提问于:2013-07-22 23:30
< >
分享
所有回答(2)
0

其实我是要在u3d中的web player中使用压缩,但是最后这些在普通vs中正常运行的压缩到u3d中就报错,找不到dll。其实压缩很简单,网上也很多可用代码:

 /// <summary>
    /// 压缩字符串
    /// </summary>
    /// <param name="toCompress">需要被压缩的字符串</param>
    /// <returns>返回压缩后的字符串</returns>
    public static string CompressStringToString(string toCompress)
    {
        if (!string.IsNullOrEmpty(toCompress))
        {
            Debug.Log("得到的待解压到字符串是:"+toCompress);
            byte[] inBuffer = Encoding.UTF8.GetBytes(toCompress);
            using (MemoryStream compressedStream = new MemoryStream())
            {
                using (GZipStream gzip = new GZipStream(compressedStream, CompressionMode.Compress,true))
                {
                    gzip.Write(inBuffer, 0, inBuffer.Length);
                }

                compressedStream.Position = 0;

                // store the length of the uncompressed array (inBuffer) at the first 4 bytes in outBuffer
                byte[] outBuffer = new byte[compressedStream.Length + 4];
                System.Buffer.BlockCopy(compressedStream.ToArray(), 0, outBuffer, 4, Convert.ToInt32(compressedStream.Length));
                System.Buffer.BlockCopy(BitConverter.GetBytes(inBuffer.Length), 0, outBuffer, 0, 4);

                return Convert.ToBase64String(outBuffer);
            }
        }
        else
        {
            return string.Empty;
        }
    }

    /// <summary>
    /// 解压字符串
    /// </summary>
    /// <param name="toDecompress">需要被解压到字符串</param>
    /// <returns>返回解压缩后的字符串</returns>
    public static string DecompressStringToString(string toDecompress)
    {
        if (!string.IsNullOrEmpty(toDecompress))
        {
            byte[] toDecompressBuffer = Convert.FromBase64String(toDecompress);

            using (MemoryStream decompressedStream = new MemoryStream())
            {
                int len = BitConverter.ToInt32(toDecompressBuffer, 0);
                decompressedStream.Write(toDecompressBuffer, 4, toDecompressBuffer.Length - 4);

                byte[] outBuffer = new byte[len];

                decompressedStream.Position = 0;
                using (GZipStream gzip = new GZipStream(decompressedStream, CompressionMode.Decompress))
                {
                    gzip.Read(outBuffer, 0, outBuffer.Length);
                }

                return Encoding.UTF8.GetString(outBuffer);
            }
        }
        else
        {
            return string.Empty;
        }
    }
View Code

但是在u3d中,System.dll貌似不是完整的dll,不能使用io的功能?这个还真没有搞定,u3d中的socket也还在研究,

留下的枇杷 | 园豆:6 (初学一级) | 2013-07-23 08:57
0

you can find the help in

http://stackoverflow.com/questions/10012178/creating-zip-files-from-memory-stream-c-sharp

 

Supper_litt | 园豆:827 (小虾三级) | 2016-06-22 17:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册