protected byte[] CompressMap() { int uncompressedSize = DynaBuffer.GetBytesUsed(); MemoryStream ms = new MemoryStream(); Ionic.Zlib.ZlibStream ws = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestCompression, true); ws.Write(DynaBuffer.GetRawBuffer(), 0, uncompressedSize); ws.Flush(); ws.Close(); int len = (int)ms.Length; byte[] compressedData = ms.GetBuffer(); // ms.Length(); Array.Resize(ref compressedData, (int)ms.Length); DynamicOutputBuffer header = DynamicOutputBuffer.GetTempBuffer(64); header.WriteUInt16(Constants.WorldCodeHeaderSize); header.WriteUInt16(Constants.WorldCodeHeader); header.WriteUInt16(1); header.WriteUInt32(uncompressedSize); header.WriteUInt32(compressedData.Length); header.WriteBytes(compressedData); header.WriteUInt16(Constants.WorldCodeEndSize); header.WriteUInt16(Constants.WorldCodeEnd); return(header.GetFinalBuffer()); }
public static byte[] Comp(byte[] input) { using (var sourceStream = new MemoryStream(input)) { using (var compressed = new MemoryStream()) { using (var zipSteam = new ZlibStream(compressed, CompressionMode.Compress, CompressionLevel.Level9, true)) { zipSteam.FlushMode = FlushType.Full; //var buffer = new byte[1024]; //int len = sourceStream.Read(buffer, 0, buffer.Length); //while (len > 0) //{ // zipSteam.Write(buffer, 0, len); // len = sourceStream.Read(buffer, 0, buffer.Length); //} sourceStream.CopyTo(zipSteam); zipSteam.Flush(); return compressed.ToArray(); } } } }
private void load(BinaryReader binReader) { if (binReader != null && binReader.BaseStream != null && binReader.BaseStream.Length > 0 && binReader.BaseStream.CanSeek == true) { try { binReader.BaseStream.Seek(0, SeekOrigin.Begin); if (binReader.BaseStream.Length - binReader.BaseStream.Position >= 13) { string magic = System.Text.Encoding.UTF8.GetString(binReader.ReadBytes(3)); int version = (int)binReader.ReadInt16(); // usually version == 40 if (magic == "C3D" && version >= 40 && version < 50) { int totalLength = (int)binReader.ReadUInt32(); // useless int totalResLength = (int)binReader.ReadUInt32(); int len = (int)(binReader.BaseStream.Length - binReader.BaseStream.Position); var rawData = binReader.ReadBytes(len); // create a seekable memory stream of the file bytes using (MemoryStream filestream = new MemoryStream()) { // create a BinaryReader used to read the uf3d file using (Ionic.Zlib.ZlibStream gzipStream = new Ionic.Zlib.ZlibStream(filestream, Ionic.Zlib.CompressionMode.Decompress, Ionic.Zlib.CompressionLevel.Default, true)) { gzipStream.Write(rawData, 0, rawData.Length); gzipStream.Flush(); if (filestream != null && filestream.Length > 0 && filestream.CanSeek == true) { // create a BinaryReader used to read the uf3d file using (binReader = new BinaryReader(filestream)) { this.loadDataChunk(binReader); } ParticleSystemAssembler.Assemble(this); } } } } } } catch (Exception ex) { // clear all this.ClearAll(); throw ex; } } else { this.ClearAll(); throw new Exception(@"Error loading file, could not read file from disk."); } }
/// <summary> /// Compress to CompressedData /// </summary> public static MemoryStream Compress(byte[] buffer) { var output = new MemoryStream(); using (ZlibStream zout = new ZlibStream(output, CompressionMode.Compress)) { zout.Write(buffer, 0, buffer.Length); zout.Flush(); } return output; }
public byte[] CompressChunk() { //File and body headers ar emanaged outside //All we do here is to write the compressed tag using (MemoryStream ms = new MemoryStream()) { using (ZlibStream compressed = new ZlibStream(ms, CompressionMode.Compress, true)) { var w = new EndianBinaryWriter(EndianBitConverter.Big, compressed); Tag.Write(w); compressed.Flush(); } return ms.ToArray(); } }
public static byte[] ZibDecompress(byte[] data) { var ms = new MemoryStream(data); using (var msDecompressed = new System.IO.MemoryStream()) { using (var zs = new ZlibStream(msDecompressed, CompressionMode.Decompress, true)) { int readByte = ms.ReadByte(); while (readByte != -1) { zs.WriteByte((byte)readByte); readByte = ms.ReadByte(); } zs.Flush(); } return msDecompressed.ToArray(); } }
public void Save(Stream stream) { long dataStart = CalculateDataStartOffset(); stream.Seek(dataStart, SeekOrigin.Begin); long fileStart = 0; long compressedSize = 0; long uncompressedSize = 0; // Output file data ZlibStream dataStream = null; if (IsCompressed && IsCondensed) { dataStream = new ZlibStream(stream, CompressionMode.Compress, CompressionLevel.BestCompression, true); dataStream.FlushMode = FlushType.Sync; } long compressedStart = 0; for (int i = 0; i < Files.Count; i++) { IPackfileEntry entry = Files[i]; Stream fs = m_Streams[entry.Name]; bool isLast = (i == (Files.Count - 1)); PackfileEntry pfE = (PackfileEntry)entry; var data = pfE.Data; data.Start = (uint)fileStart; data.Size = (uint)fs.Length; data.Alignment = (IsCondensed) ? (ushort)16 : (ushort)1; if (this.IsCompressed) data.Flags = PackfileEntryFlags.Compressed; if (IsCompressed && IsCondensed) { fs.CopyTo(dataStream); dataStream.Flush(); long afterData = dataStream.TotalOut; data.CompressedSize = (uint)(afterData - compressedStart); compressedStart = afterData; if (IsStr2) { fileStart += data.Size.Align(16); uncompressedSize += data.Size.Align(16); compressedSize += data.CompressedSize; } else { fileStart += data.Size; //.Align(16); if (!isLast) { uncompressedSize += data.Size; //.Align(16); //uint toSkip = data.Size.Align(16) - data.Size; // for (int j = 0; j < toSkip; j++) //dataStream.WriteByte(0); } else { uncompressedSize += data.Size; } compressedSize += data.CompressedSize; } } else if (IsCondensed) { fs.CopyTo(stream); data.CompressedSize = 0xFFFFFFFF; fileStart += data.Size.Align(16); if (isLast) uncompressedSize += data.Size; else { uint toSkip = data.Size.Align(16) - data.Size; uncompressedSize += data.Size.Align(16); stream.Seek(toSkip, SeekOrigin.Current); } } else if (IsCompressed) { long beforeData = stream.Position; using (dataStream = new ZlibStream(stream, CompressionMode.Compress, CompressionLevel.BestCompression, true)) { dataStream.FlushMode = FlushType.Sync; fs.CopyTo(dataStream); dataStream.Flush(); } long afterData = stream.Position; data.CompressedSize = (uint)(afterData - beforeData); fileStart += data.CompressedSize; uncompressedSize += data.Size; compressedSize += data.CompressedSize; } else { fs.CopyTo(stream); data.CompressedSize = 0xFFFFFFFF; fileStart += data.Size; uncompressedSize += data.Size; } fs.Close(); pfE.Data = data; } if (IsCompressed && IsCondensed) { dataStream.Close(); } // Output file names stream.Seek(CalculateEntryNamesOffset(), SeekOrigin.Begin); long startOffset = CalculateEntryNamesOffset(); foreach (IPackfileEntry entry in Files) { PackfileEntry pfE = (PackfileEntry)entry; stream.Align(2); pfE.Data.FilenameOffset = (UInt32)(stream.Position - startOffset); stream.WriteAsciiNullTerminatedString(entry.Name); stream.Seek(1, SeekOrigin.Current); stream.Align(2); } long nameSize = stream.Position - CalculateEntryNamesOffset(); // Output file info stream.Seek(GetEntryDataOffset(), SeekOrigin.Begin); foreach (IPackfileEntry entry in Files) { PackfileEntry pfE = (PackfileEntry)entry; stream.WriteStruct(pfE.Data); } long dirSize = stream.Position - GetEntryDataOffset(); // Output header stream.Seek(0, SeekOrigin.Begin); FileData.Descriptor = 0x51890ACE; FileData.Version = 0x0A; FileData.HeaderChecksum = 0; FileData.FileSize = (uint)stream.Length; FileData.NumFiles = (uint)Files.Count; FileData.DirSize = (uint)dirSize; FileData.FilenameSize = (uint)nameSize; FileData.DataSize = (uint)uncompressedSize; if (IsCompressed) FileData.CompressedDataSize = (uint)compressedSize; else FileData.CompressedDataSize = 0xFFFFFFFF; stream.WriteStruct(FileData); uint checksum = 0; byte[] checksumBuffer = new byte[0x1C]; stream.Seek(0x0C, SeekOrigin.Begin); stream.Read(checksumBuffer, 0, checksumBuffer.Length); checksum = Hashes.CrcVolition(checksumBuffer); stream.Seek(0x08, SeekOrigin.Begin); stream.WriteUInt32(checksum); stream.Flush(); }
private byte[] Compress(byte[] revlogEntryData, int offset, int count) { using(var memoryStream = new MemoryStream()) { using(var zlibStream = new ZlibStream(memoryStream, CompressionMode.Compress)) { zlibStream.Write(revlogEntryData, offset, count); zlibStream.Flush(); } // using return memoryStream.ToArray(); } // using }
private void writeToStreamOld(Stream s, bool le, bool readOnly) { bool compress = true; bool base64 = true; using (var tw = new StreamWriter(s, System.Text.Encoding.ASCII)) { tw.NewLine = "\n"; tw.WriteLine("Type: Floppy"); if (readOnly) tw.WriteLine("Access: Read-Only"); else tw.WriteLine("Access: Read-Write"); if (le) tw.WriteLine("Byte-Order: Little-Endian"); else tw.WriteLine("Byte-Order: Big-Endian"); int lb = 0, hb = 1; if (!le) { lb = 1; hb = 0; } byte[] bytes, cbytes; bytes = new byte[1440 * 1024]; for (int i = 0; i < _words.Length; ++i) { var j = 2 * i; bytes[j + lb] = (byte)_words[i]; bytes[j + hb] = (byte)(_words[i] >> 8); } if (compress) { tw.WriteLine("Compression: Zlib"); using (var ms = new MemoryStream()) { using (var cmpr = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression, leaveOpen: true)) { cmpr.Write(bytes, 0, bytes.Length); cmpr.Flush(); } cbytes = ms.ToArray(); } } else { tw.WriteLine("Compression: None"); cbytes = bytes; } if (base64) { var encoded = Convert.ToBase64String(cbytes, Base64FormattingOptions.InsertLineBreaks); tw.WriteLine("Encoding: Base64"); tw.WriteLine("Content-Length: {0}", encoded.Length); tw.WriteLine(); tw.WriteLine(encoded); } else { tw.WriteLine("Encoding: None"); tw.WriteLine("Content-Length: {0}", cbytes.Length); tw.WriteLine(); tw.Flush(); tw.BaseStream.Write(bytes, 0, cbytes.Length); } } }