public void InflatorStreamOwnership() { var memStream = new TrackedMemoryStream(); var s = new InflaterInputStream(memStream); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); #if NET451 s.Close(); #elif NETCOREAPP1_0 s.Dispose(); #endif Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close"); Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close"); memStream = new TrackedMemoryStream(); s = new InflaterInputStream(memStream); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); s.IsStreamOwner = false; #if NET451 s.Close(); #elif NETCOREAPP1_0 s.Dispose(); #endif Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); }
protected void addDecompressedProgramChecksum( FileStream pFileStream, ref Crc32 pChecksum, ref CryptoStream pMd5CryptoStream, ref CryptoStream pSha1CryptoStream) { if (this.compressedProgramLength > 0) { InflaterInputStream inflater; int read; byte[] data = new byte[READ_CHUNK_SIZE]; pFileStream.Seek((long)(RESERVED_SECTION_OFFSET + this.reservedSectionLength), SeekOrigin.Begin); inflater = new InflaterInputStream(pFileStream); while ((read = inflater.Read(data, 0, READ_CHUNK_SIZE)) > 0) { pChecksum.Update(data, 0, read); pMd5CryptoStream.Write(data, 0, read); pSha1CryptoStream.Write(data, 0, read); } inflater.Close(); inflater.Dispose(); } }
protected override void Dispose(bool disposing) { if (disposing) { ist.Dispose(); base.Dispose(disposing); } }
/// <summary> /// 解压 /// </summary> /// <param name="inputdata"></param> /// <param name="ds"></param> /// <param name="error"></param> /// <returns></returns> public static bool Decompress(string inputdata, ref DataSet ds, ref string error) { try { //inputdata = inputdata.Replace("-", "+").Replace("_", "/").Replace(".", "="); byte[] bb = Convert.FromBase64String(inputdata); MemoryStream data = new MemoryStream(bb); //int s1 = (int)data.Length; using (MemoryStream m = new MemoryStream()) { using (InflaterInputStream mem = new InflaterInputStream(data)) { byte[] buffer = new byte[4096]; while (true) { int size = mem.Read(buffer, 0, buffer.Length); m.Write(buffer, 0, size); if (size == 0) { break; } } mem.Close(); mem.Dispose(); } m.Seek(0, SeekOrigin.Begin); BinaryFormatter bf = new BinaryFormatter(); ds = bf.Deserialize(m) as DataSet; m.Close(); m.Dispose(); } return(true); } catch (Exception ex) { error = ex.Message; return(false); } }
/// <summary> /// Decodes the specified data. /// </summary> public override byte[] Decode(byte[] data, FilterParms parms) { MemoryStream msInput = new MemoryStream(data); MemoryStream msOutput = new MemoryStream(); #if NET_ZIP // See http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=97064 // It seems to work when skipping the first two bytes. byte header; // 0x30 0x59 header = (byte)msInput.ReadByte(); //Debug.Assert(header == 48); header = (byte)msInput.ReadByte(); //Debug.Assert(header == 89); DeflateStream zip = new DeflateStream(msInput, CompressionMode.Decompress, true); int cbRead; byte[] abResult = new byte[1024]; do { cbRead = zip.Read(abResult, 0, abResult.Length); if (cbRead > 0) { msOutput.Write(abResult, 0, cbRead); } }while (cbRead > 0); zip.Close(); msOutput.Flush(); if (msOutput.Length >= 0) { msOutput.Capacity = (int)msOutput.Length; return(msOutput.GetBuffer()); } return(null); #else InflaterInputStream iis = new InflaterInputStream(msInput, new Inflater(false)); int cbRead; byte[] abResult = new byte[32768]; do { cbRead = iis.Read(abResult, 0, abResult.Length); if (cbRead > 0) { msOutput.Write(abResult, 0, cbRead); } }while (cbRead > 0); #if UWP iis.Dispose(); #else iis.Close(); #endif msOutput.Flush(); if (msOutput.Length >= 0) { #if NETFX_CORE || UWP return(msOutput.ToArray()); #else msOutput.Capacity = (int)msOutput.Length; return(msOutput.GetBuffer()); #endif } return(null); #endif }
private string ParseQpyd(string qqydFile) { var fs = new FileStream(qqydFile, FileMode.Open, FileAccess.Read); fs.Position = 0x38; var startAddressByte = new byte[4]; fs.Read(startAddressByte, 0, 4); int startAddress = BitConverter.ToInt32(startAddressByte, 0); fs.Position = 0x44; int wordCount = BinFileHelper.ReadInt32(fs); CountWord = wordCount; CurrentStatus = 0; fs.Position = startAddress; var zipStream = new InflaterInputStream(fs); int bufferSize = 2048; //缓冲区大小 int readCount = 0; //读入缓冲区的实际字节 var buffer = new byte[bufferSize]; var byteList = new List <byte>(); readCount = zipStream.Read(buffer, 0, bufferSize); while (readCount > 0) { for (int i = 0; i < readCount; i++) { byteList.Add(buffer[i]); } readCount = zipStream.Read(buffer, 0, bufferSize); } zipStream.Close(); zipStream.Dispose(); fs.Close(); byte[] byteArray = byteList.ToArray(); int unzippedDictStartAddr = -1; int idx = 0; var sb = new StringBuilder(); while (unzippedDictStartAddr == -1 || idx < unzippedDictStartAddr) { // read word int pinyinStartAddr = BitConverter.ToInt32(byteArray, idx + 0x6); int pinyinLength = BitConverter.ToInt32(byteArray, idx + 0x0) & 0xff; int wordStartAddr = pinyinStartAddr + pinyinLength; int wordLength = BitConverter.ToInt32(byteArray, idx + 0x1) & 0xff; if (unzippedDictStartAddr == -1) { unzippedDictStartAddr = pinyinStartAddr; Debug.WriteLine("词库地址(解压后):0x" + unzippedDictStartAddr.ToString("0x") + "\n"); } string pinyin = Encoding.UTF8.GetString(byteArray, pinyinStartAddr, pinyinLength); string word = Encoding.Unicode.GetString(byteArray, wordStartAddr, wordLength); sb.Append(word + "\t" + pinyin + "\n"); Debug.WriteLine(word + "\t" + pinyin); CurrentStatus++; // step up idx += 0xa; } return(sb.ToString()); }