void LoadAndCompress() { compressedmem = new System.IO.MemoryStream(); System.IO.FileInfo fi = new System.IO.FileInfo(filename); System.IO.FileStream filestream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read); ComponentAce.Compression.Libs.zlib.ZOutputStream zstream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(compressedmem, ComponentAce.Compression.Libs.zlib.zlibConst.Z_DEFAULT_COMPRESSION); int totallen = (int)Size; int len = buffer.Length; while (totallen > 0) { if (len > totallen) { len = totallen; } filestream.Read(buffer, 0, len); zstream.Write(buffer, 0, len); totallen -= len; } zstream.finish(); filestream.Close(); compressedsize = (uint)compressedmem.Length; adler32 = AdlerCheckSum.GetAdler32(compressedmem, 0, (int)compressedmem.Length); }
public Stream ExtractFile(FileEntry fileEntry, Stream streamOut = null) { var buffer = new byte[header.maxBlockSize]; int index = fileEntry.blockIndex; long size = (long)fileEntry.fileSize.Value; long total = 0; streamIn.Seek((long)fileEntry.dataOffset.Value, SeekOrigin.Begin); streamOut = streamOut ?? new MemoryStream((int)(ulong)fileEntry.fileSize); long startPosition = streamOut.Position; // loop until all blocks have been read while (total < size) { uint blockSize = blockSizes[index]; streamIn.Read(buffer, 0, (int)blockSize); var zOut = new ComponentAce.Compression.Libs.zlib.ZOutputStream(streamOut); long currentPos = streamOut.Position; //Detect zlib blocks if (buffer[0] == 0x78) { zOut.Write(buffer, 0, (int)blockSize); zOut.Flush(); total += zOut.TotalOut; } else { //Make sure that no data has been written to the output stream streamOut.Seek(currentPos, SeekOrigin.Begin); streamOut.Write(buffer, 0, (int)blockSize); total += blockSize; } index++; } streamOut.Flush(); streamOut.Position = startPosition; return(streamOut); }
public override void WriteDecompressed(System.IO.Stream writestream) { System.IO.FileStream filestream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.Stream readstream = filestream; readstream.Position = offset; if (compressedmem != null && compressedmem.Length == compressedsize) { readstream = compressedmem; readstream.Position = 0; } ComponentAce.Compression.Libs.zlib.ZOutputStream zstream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(writestream); int encryptlen = (int)compressedsize; byte[] data = new byte[(int)compressedsize]; readstream.Read(data, 0, (int)compressedsize); if (algorithm == AlgorithmType.infilateandblowfish && encryptlen >= BlowfishNET.BlowfishECB.BLOCK_SIZE) { if (ecb == null) { throw new Exception("Encrypted File"); } if (encryptlen % BlowfishNET.BlowfishECB.BLOCK_SIZE != 0) { encryptlen -= encryptlen % BlowfishNET.BlowfishECB.BLOCK_SIZE; } byte[] tmp = (byte [])data.Clone(); ecb.Decrypt(tmp, 0, data, 0, encryptlen); } zstream.Write(data, 0, (int)compressedsize); zstream.finish(); filestream.Close(); writestream.Flush(); }
public void DecodeStream() { // Reset position to use same buffer zlibMemoryStream.Position = 0; // Get compressed stream length to read byte[] buff = new byte[4]; if (this.BaseStream.Read(buff, 0, 4) != 4) { throw new Exception("ZRLE decoder: Invalid compressed stream size"); } // BigEndian to LittleEndian conversion int compressedBufferSize = (int)(buff[3] | buff[2] << 8 | buff[1] << 16 | buff[0] << 24); if (compressedBufferSize > 64 * 1024 * 1024) { throw new Exception("ZRLE decoder: Invalid compressed data size"); } #region Decode stream // Decode stream // int pos = 0; // while (pos++ < compressedBufferSize) // zlibDecompressedStream.WriteByte(this.BaseStream.ReadByte()); #endregion #region Decode stream in blocks // Decode stream in blocks int bytesToRead; int bytesNeeded = compressedBufferSize; int maxBufferSize = 64 * 1024; // 64k buffer byte[] receiveBuffer = new byte[maxBufferSize]; NetworkStream netStream = (NetworkStream)this.BaseStream; netStream.ReadTimeout = 15000; // Set timeout to 15s do { if (netStream.DataAvailable) { bytesToRead = bytesNeeded; // the byteToRead should never exceed the maxBufferSize if (bytesToRead > maxBufferSize) { bytesToRead = maxBufferSize; } // try reading bytes (read in 1024 byte chunks) - improvement for slow connections int toRead = (bytesToRead > 1024) ? 1024 : bytesToRead; int bytesRead = 0; try { bytesRead = netStream.Read(receiveBuffer, bytesRead, toRead); } catch { } // lower the bytesNeeded with the bytesRead. bytesNeeded -= bytesRead; // write the readed bytes to the decompression stream. zlibDecompressedStream.Write(receiveBuffer, 0, bytesRead); } else { // there isn't any data atm. let's give the processor some time. Thread.Sleep(100); // increased to 100ms for slow connections } } while (bytesNeeded > 0); #endregion zlibMemoryStream.Position = 0; }