//------------------------------------------------------------------------- public static string DeCompressGZIP(byte[] bufCompress) { if (null == bufCompress || 0 == bufCompress.Length) { Console.WriteLine("DeCompressGZIP bufCompress is nil"); return(string.Empty); } MemoryStream msDe = null; GZipInputStream inStream = null; MemoryStream msCom = null; long currentIndex = 0; try { msDe = new MemoryStream(); msDe.Write(bufCompress, 0, bufCompress.Length); //msDe.Flush(); msDe.Seek(0, SeekOrigin.Begin); inStream = new GZipInputStream(msDe); // 一次读取 8KB int nSize = 1024 * 8; byte[] bufCom = new byte[nSize]; msCom = new MemoryStream(); while (true) { int numRead = inStream.Read(bufCom, 0, nSize); if (numRead <= 0) { break; } // 写进去 msCom.Write(bufCom, 0, numRead); currentIndex += numRead; if (numRead < nSize) { break; } } byte[] bufDeCompress = msCom.GetBuffer(); return(Encoding.UTF8.GetString(bufDeCompress)); } catch (Exception ex) { Console.WriteLine("DeCompressGZIP 异常(1/2),可能ZIP出错 可能ZIP文件解压后大小刚好是缓冲区的N倍:" + ex.Message); Console.WriteLine("DeCompressGZIP 异常(2/2),bufCompress.Length:" + bufCompress.Length + ",currentIndex:" + currentIndex); if (null != msCom) { byte[] bufDeCompress = msCom.GetBuffer(); return(Encoding.UTF8.GetString(bufDeCompress)); } } finally { if (null != inStream) { inStream.Close(); } if (null != msDe) { msDe.Close(); } if (null != msCom) { msCom.Close(); } } return(string.Empty); }
static void LoadResToCache() { string loon_default_font = "lfcache"; IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication(); if (!isoStorage.FileExists(loon_default_font)) { IsolatedStorageFileStream outStream = isoStorage.CreateFile(loon_default_font); Stream gzip = null; try { gzip = new GZipInputStream(XNAConfig.LoadStream(LSystem.FRAMEWORK_IMG_NAME + "font.zip")); byte[] buffer = new byte[4096]; int charsRead; while ((charsRead = gzip.Read(buffer, 0, buffer.Length)) != 0) { outStream.Write(buffer, 0, charsRead); } outStream.Flush(); if (gzip != null) { gzip.Close(); gzip.Dispose(); gzip = null; } buffer = null; } catch (Exception) { if (outStream != null) { outStream.Close(); outStream.Dispose(); outStream = null; } if (gzip != null) { gzip.Close(); gzip.Dispose(); gzip = null; } IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(loon_default_font); } finally { if (outStream != null) { outStream.Close(); outStream = null; } } } Stream ins = null; try { ins = isoStorage.OpenFile(loon_default_font, FileMode.Open); DataInputStream resStream = new DataInputStream(ins); realsize = resStream.ReadUnsignedByte(); offy = resStream.ReadByte(); fontSpace = (realsize + offy) * 2; if (realsize > 0x10) { fontSpace *= 2; } int num = resStream.ReadInt(); int num2 = resStream.ReadByte(); byte[] bufferData = new byte[resStream.Available()]; resStream.Read(bufferData); fontData = bufferData; if (resStream != null) { resStream.Close(); resStream = null; } } catch (Exception) { fontData = null; if (ins != null) { ins.Close(); ins.Dispose(); ins = null; } LoadResToMemory(); } finally { if (ins != null) { ins.Close(); ins.Dispose(); ins = null; } } }
//------------------------------------------------------------------------- public static string DeCompressGZIPBase64(string strValue) { if (true == string.IsNullOrEmpty(strValue)) { Console.WriteLine("DeCompressGZIPBase64 strValue is nil"); return(string.Empty); } string str = strValue; MemoryStream msDe = null; GZipInputStream inStream = null; MemoryStream msCom = null; try { byte[] bufCompress = Convert.FromBase64String(strValue); msDe = new MemoryStream(); msDe.Write(bufCompress, 0, bufCompress.Length); //msDe.Flush(); msDe.Seek(0, SeekOrigin.Begin); inStream = new GZipInputStream(msDe); // 一次读取 8KB int nSize = 1024 * 8; byte[] bufCom = new byte[nSize]; msCom = new MemoryStream(); long currentIndex = 0; while (true) { int numRead = inStream.Read(bufCom, 0, nSize); if (numRead <= 0) { break; } // 写进去 msCom.Write(bufCom, 0, numRead); currentIndex += numRead; if (numRead < nSize) { break; } } byte[] bufDeCompress = msCom.GetBuffer(); str = Encoding.UTF8.GetString(bufDeCompress); } catch (Exception ex) { Console.WriteLine("DeCompressGZIPBase64 异常,可能ZIP出错 可能ZIP文件解压后大小刚好是缓冲区的N倍:" + ex.Message); if (null != msCom) { byte[] bufDeCompress = msCom.GetBuffer(); str = Encoding.UTF8.GetString(bufDeCompress); } } finally { if (null != inStream) { inStream.Close(); } if (null != msDe) { msDe.Close(); } if (null != msCom) { msCom.Close(); } } return(str); }