public static byte[] Deflate(byte[] uncompressedStream, ulong cBlockSize) { //ulong zBlocks = (((ulong)UncompressedStream.LongLength - ((ulong)UncompressedStream.LongLength % cBlockSize)) / cBlockSize) // + ((ulong)UncompressedStream.LongLength % cBlockSize) == 0 ? 0u : 1u; ulong zBlocks = (((ulong)uncompressedStream.LongLength - ((ulong)uncompressedStream.LongLength % cBlockSize)) / cBlockSize); if ((ulong)uncompressedStream.LongLength % cBlockSize > 0) { zBlocks++; } var inStream = new MemoryStream(uncompressedStream); var outStream = new MemoryStream(); byte[] outData; var outZStream = new ZlibStream(outStream, CompressionMode.Compress, CompressionLevel.Default); try { CopyStream(inStream, outZStream, cBlockSize); outData = new byte[outStream.Length]; outStream.Read(outData, 0, (int)outStream.Length); } finally { outZStream.Close(); outStream.Close(); inStream.Close(); } return(outData); }
/// <summary> /// Compress the contents of the provided array /// </summary> /// <param name="data">An uncompressed byte array</param> /// <returns></returns> public static byte[] Compress(byte[] data) { using (MemoryStream out1 = new MemoryStream()) { ZlibStream deflaterOutputStream = new ZlibStream(out1, CompressionMode.Compress); try { //for (int i = 0; i < data.Length; i++) //deflaterOutputStream.WriteByte(data[i]); deflaterOutputStream.Write(data, 0, data.Length); //Tony Qu changed the code return(out1.ToArray()); } catch (IOException e) { throw new RecordFormatException(e.ToString()); } finally { out1.Close(); if (deflaterOutputStream != null) { deflaterOutputStream.Close(); } } } }
private byte[] InternalExtract() { _stream.Position = _manifest.Offset; var sizeBytes = new byte[4]; _stream.Read(sizeBytes, 0, 4); int uncompressedSize = BitConverter.ToInt32(sizeBytes, 0); if (uncompressedSize <= 0) { return(new byte[0]); } _stream.Position += 4; var buffer = new byte[_manifest.UncompressedSize]; var zlibStream = new ZlibStream(_stream, CompressionMode.Decompress, true); zlibStream.Read(buffer, 0, buffer.Length); zlibStream.Close(); zlibStream.Dispose(); return(buffer); }
public static byte[] Deflate(byte[] uncompressedStream, ulong cBlockSize) { ulong zBlocks = (uint)(Math.Ceiling((ulong)uncompressedStream.LongLength / (double)cBlockSize)); var inStream = new MemoryStream(uncompressedStream); var outStream = new MemoryStream(); byte[] outData; var outZStream = new ZlibStream(outStream, CompressionMode.Compress, CompressionLevel.BestCompression); try { CopyStream(inStream, outZStream, cBlockSize); outData = new byte[outStream.Length]; outStream.Read(outData, 0, (int)outStream.Length); } finally { outZStream.Close(); outStream.Close(); inStream.Close(); } return(outData); }
private static DataStream Inflate(DataStream source, EndiannessMode endianness) { DataStream dest = DataStreamFactory.FromMemory(); source.Seek(0); var reader = new DataReader(source) { Endianness = endianness, }; int size = reader.ReadInt32(); int chunkSize = reader.ReadInt32(); while (chunkSize != 0) { using var zlibStream = new ZlibStream(dest, CompressionMode.Decompress, true); source.WriteSegmentTo(source.Position, chunkSize, zlibStream); zlibStream.Close(); source.Seek(chunkSize, SeekOrigin.Current); chunkSize = reader.ReadInt32(); } if (dest.Length != size) { throw new ExtractionException("Result size doesn't match with expected size."); } return(dest); }
/// <summary> /// Decompresses the provided data, returning the inflated result. /// </summary> /// <param name="data">the deflated picture data.</param> /// <returns>the inflated picture data.</returns> private static byte[] InflatePictureData(byte[] data) { MemoryStream out1 = new MemoryStream(); ZlibStream in1 = null; try { in1 = new ZlibStream( new MemoryStream(data), CompressionMode.Decompress); byte[] buf = new byte[4096]; int ReadBytes; while ((ReadBytes = in1.Read(buf, 0, buf.Length)) > 0) { out1.Write(buf, 0, ReadBytes); } return(out1.ToArray()); } catch (IOException e) { log.Log(POILogger.INFO, "Possibly corrupt compression or non-compressed data", e); return(data); } finally { out1.Close(); if (in1 != null) { in1.Close(); } } }
public void Compress(string algorithm) { if (algorithm == CompressionAlgorithm.Deflate) { byte[] buffer = _memoryStream.ToArray(); MemoryStream ms = new MemoryStream(); DeflateStream deflateStream = new DeflateStream(ms, CompressionMode.Compress, true); deflateStream.Write(buffer, 0, buffer.Length); deflateStream.Close(); _memoryStream.Close(); _memoryStream = ms; AMFReader amfReader = new AMFReader(_memoryStream); AMFWriter amfWriter = new AMFWriter(_memoryStream); _dataOutput = new DataOutput(amfWriter); _dataInput = new DataInput(amfReader); } if (algorithm == CompressionAlgorithm.Zlib) { byte[] buffer = _memoryStream.ToArray(); MemoryStream ms = new MemoryStream(); ZlibStream zlibStream = new ZlibStream(ms, CompressionMode.Compress, true); zlibStream.Write(buffer, 0, buffer.Length); zlibStream.Flush(); zlibStream.Close(); zlibStream.Dispose(); _memoryStream.Close(); _memoryStream = ms; AMFReader amfReader = new AMFReader(_memoryStream); AMFWriter amfWriter = new AMFWriter(_memoryStream); _dataOutput = new DataOutput(amfWriter); _dataInput = new DataInput(amfReader); } }
/// <summary> /// Decompresses the specified data. /// </summary> /// <param name="data">The compressed byte array.</param> /// <param name="pos">The starting position into the byte array.</param> /// <param name="Length">The number of compressed bytes to decompress.</param> /// <returns>An uncompressed byte array</returns> public static byte[] Decompress(byte[] data, int pos, int Length) { byte[] compressedData = new byte[Length]; Array.Copy(data, pos + 50, compressedData, 0, Length); ZlibStream inflaterInputStream = new ZlibStream(new MemoryStream(compressedData), CompressionMode.Decompress); MemoryStream out1 = new MemoryStream(); int c; try { while ((c = inflaterInputStream.ReadByte()) != -1) { out1.WriteByte((byte)c); } return(out1.ToArray()); } catch (IOException e) { throw new RecordFormatException(e.ToString()); } finally { out1.Close(); if (inflaterInputStream != null) { inflaterInputStream.Close(); } } }
public void Uncompress(string algorithm) { if (algorithm == CompressionAlgorithm.Zlib) { Position = 0; ZlibStream deflateStream = new ZlibStream(_memoryStream, CompressionMode.Decompress, false); MemoryStream ms = new MemoryStream(); byte[] buffer = new byte[1024]; // Chop off the first two bytes //int b = _memoryStream.ReadByte(); //b = _memoryStream.ReadByte(); while (true) { int readCount = deflateStream.Read(buffer, 0, buffer.Length); if (readCount > 0) { ms.Write(buffer, 0, readCount); } else { break; } } deflateStream.Close(); _memoryStream.Close(); _memoryStream.Dispose(); _memoryStream = ms; _memoryStream.Position = 0; } if (algorithm == CompressionAlgorithm.Deflate) { Position = 0; DeflateStream deflateStream = new DeflateStream(_memoryStream, CompressionMode.Decompress, false); MemoryStream ms = new MemoryStream(); byte[] buffer = new byte[1024]; while (true) { int readCount = deflateStream.Read(buffer, 0, buffer.Length); if (readCount > 0) { ms.Write(buffer, 0, readCount); } else { break; } } deflateStream.Close(); _memoryStream.Close(); _memoryStream.Dispose(); _memoryStream = ms; _memoryStream.Position = 0; } AMFReader amfReader = new AMFReader(_memoryStream); AMFWriter amfWriter = new AMFWriter(_memoryStream); _dataOutput = new DataOutput(amfWriter); _dataInput = new DataInput(amfReader); }
/// <summary> /// Decompress a byte array into another byte array of the specified size /// </summary> /// <param name="toDecompress">Data to decompress</param> /// <param name="uncompressedSize">Size of the data once decompressed</param> /// <returns>Decompressed data as a byte array</returns> public static byte[] Decompress(byte[] toDecompress, int uncompressedSize) { ZlibStream stream = new ZlibStream(new MemoryStream(toDecompress, false), CompressionMode.Decompress); byte[] decompressed = new byte[uncompressedSize]; stream.Read(decompressed, 0, uncompressedSize); stream.Close(); return(decompressed); }
/// <summary> /// Decompress a byte array into another byte array of the specified size /// </summary> /// <param name="to_decompress">Data to decompress</param> /// <param name="size_uncompressed">Size of the data once decompressed</param> /// <returns>Decompressed data as a byte array</returns> public static byte[] Decompress(byte[] to_decompress, int size_uncompressed) { ZlibStream stream = new ZlibStream(new System.IO.MemoryStream(to_decompress, false), CompressionMode.Decompress); byte[] packetData_decompressed = new byte[size_uncompressed]; stream.Read(packetData_decompressed, 0, size_uncompressed); stream.Close(); return(packetData_decompressed); }
private static byte[] ZlibCompress(byte[] data, int index, int count) { using var inputMemoryStream = new MemoryStream(data, index, count); using var outputMemoryStream = new MemoryStream(); using var zlibStream = new ZlibStream(outputMemoryStream, CompressionMode.Compress, CompressionLevel.BestCompression); inputMemoryStream.CopyTo(zlibStream); zlibStream.Close(); return(outputMemoryStream.ToArray()); }
static byte[] Compress(byte[] buffer) { MemoryStream ms = new MemoryStream(); ZlibStream str = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.Level6, true); str.Write(buffer, 0x00, buffer.Length); str.Close(); ms.Seek(0x00, SeekOrigin.Begin); return(ms.ToArray()); }
/// <summary> /// Decompress a byte array into another byte array of the specified size /// </summary> /// <param name="to_decompress">Data to decompress</param> /// <param name="size_uncompressed">Size of the data once decompressed</param> /// <returns>Decompressed data as a byte array</returns> public static byte[] Decompress(byte[] to_decompress, int size_uncompressed) { using var stream = new ZlibStream(new MemoryStream(to_decompress, false), CompressionMode.Decompress, CompressionLevel.BestSpeed); byte[] packetData_decompressed = new byte[size_uncompressed]; stream.Read(packetData_decompressed, 0, size_uncompressed); stream.Close(); return(packetData_decompressed); }
public static byte[] CompressZLib(this byte[] @this) { using (var ms = new MemoryStream()) using (var zlib = new ZlibStream(ms, CompressionMode.Compress)) { zlib.Write(@this, 0, @this.Length); zlib.Close(); return(ms.ToArray()); } }
private byte[] Inflate(byte[] buffer, int sizeDecompressed, out int sizeCompressed, int compressionLevel) { MemoryStream memoryStream = new MemoryStream(); ZlibStream zlibStream = new ZlibStream(memoryStream, CompressionMode.Compress, (CompressionLevel)compressionLevel, leaveOpen: true); zlibStream.Write(buffer, 0, sizeDecompressed); zlibStream.Flush(); zlibStream.Close(); sizeCompressed = (int)memoryStream.Length; return(memoryStream.ToArray()); }
public byte[] Inflate(byte[] buffer, int sizeDecompressed, out int sizeCompressed, int compressionLevel) { MemoryStream output = new MemoryStream(); ZlibStream zs = new ZlibStream(output, CompressionMode.Compress, (CompressionLevel)compressionLevel, true); zs.Write(buffer, 0, sizeDecompressed); zs.Flush(); zs.Close(); sizeCompressed = (int)output.Length; return(output.ToArray()); }
public static MemoryStream CompressStream(MemoryStream stream) { stream.Position = 0; var compressedStream = new MemoryStream(); var zOut = new ZlibStream(compressedStream, CompressionMode.Compress, CompressionLevel.BestCompression, true); CopyStream(stream, zOut); zOut.Close(); compressedStream.Position = 0; return(compressedStream); }
public static MemoryStream DecompressStream(MemoryStream stream) { stream.Position = 0; var decompressedStream = new MemoryStream(); var zOut = new ZlibStream(decompressedStream, CompressionMode.Decompress, true); CopyStream(stream, zOut); zOut.Close(); decompressedStream.Position = 0; return(decompressedStream); }
public static byte[] Compress(byte[] buffer) { byte[] compressed; using (var input = new MemoryStream(buffer)) using (var compressStream = new MemoryStream()) using (var compressor = new ZlibStream(compressStream, CompressionMode.Compress, CompressionLevel.BestCompression, true)) { input.CopyTo(compressor); compressor.Close(); compressed = compressStream.ToArray(); } return(compressed); }
private void EndWrite() { if (zstream != null) { zstream.Close(); } if (dstream != null) { WriteChunk("IDAT", dstream.GetDataSoFar()); } WriteChunk("IEND", new byte[0]); }
private static byte[] ZlibCompress(byte[] decompressedData) { using (var inputMemoryStream = new MemoryStream(decompressedData)) using (var outputMemoryStream = new MemoryStream()) using (var zlibStream = new ZlibStream(outputMemoryStream, CompressionMode.Compress, CompressionLevel.BestCompression)) { inputMemoryStream.CopyTo(zlibStream); zlibStream.Close(); return(outputMemoryStream.ToArray()); } }
private static string decompressFile(string inputFile) { var outputFile = inputFile + ".raw"; var sr = File.OpenRead(inputFile); var s = new ZlibStream(sr, CompressionMode.Decompress); var bytes = s.ReadToEnd().ToArray(); File.WriteAllBytes(outputFile, bytes); s.Close(); sr.Close(); return(outputFile); }
private static MemoryStream Deflate(byte[] data) { var output = new MemoryStream(); using (Stream input = new ZlibStream(output, Ionic.Zlib.CompressionMode.Compress, CompressionLevel.BestCompression, true)) { input.Write(data, 0, data.Length); input.Close(); } return(output); }
public static string GetCompressedBinaryData(string p_Content) { string data; using (MemoryStream dataStream = new MemoryStream(Encoding.UTF8.GetBytes(p_Content ?? ""))) { using (MemoryStream compressStream = new MemoryStream()) { using (ZlibStream deflateStream = new ZlibStream(compressStream, CompressionMode.Compress, CompressionLevel.Level8)) { dataStream.CopyTo(deflateStream); deflateStream.Close(); data = Convert.ToBase64String(compressStream.ToArray()); } } } return(data); }
public static string GetDecompressedBinaryData(string p_Content) { string decompressedData; using (MemoryStream dataStream = new MemoryStream(Convert.FromBase64String(p_Content))) { using (MemoryStream decompressStream = new MemoryStream()) { using (ZlibStream deflateStream = new ZlibStream(decompressStream, CompressionMode.Decompress)) { dataStream.CopyTo(deflateStream); deflateStream.Close(); decompressedData = Encoding.UTF8.GetString(decompressStream.ToArray()); } } } return(decompressedData); }
public static bool SaveJaz(string data, string fileName) { try { System.IO.MemoryStream msSinkCompressed = new System.IO.MemoryStream(); ZlibStream zOut = new ZlibStream(msSinkCompressed, CompressionMode.Compress, CompressionLevel.BestCompression, true); CopyStream(StringToMemoryStream(data), zOut); zOut.Close(); FileStream file = new FileStream(fileName, FileMode.Create, System.IO.FileAccess.Write); msSinkCompressed.WriteTo(file); file.Close(); return(true); } catch { } return(false); }
private static void decompressDlm(string filepath, string outputFolder) { if (!Directory.Exists(outputFolder)) { Directory.CreateDirectory(outputFolder); } var mapId = Path.GetFileNameWithoutExtension(filepath); var or = File.OpenRead(filepath); var s = new ZlibStream(or, CompressionMode.Decompress); var mapBytes = s.ReadToEnd(); File.WriteAllBytes(Path.Combine(outputFolder, mapId + ".map"), mapBytes.ToArray()); s.Close(); or.Close(); }
private static void CompressZlib(Stream uncompressed, Stream target) { var buffer = new byte[4096]; int readBytes; var zip = new ZlibStream(target, CompressionMode.Compress) { BufferSize = 4096 }; do { readBytes = uncompressed.Read(buffer, 0, buffer.Length); zip.Write(buffer, 0, readBytes); } while (readBytes > 0); zip.Close(); }
public static byte[] CompressData(byte[] data) { using (MemoryStream compressedStream = new MemoryStream()) { var deflateStream = new ZlibStream(compressedStream, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.Default, true); deflateStream.Write(data, 0, data.Length); deflateStream.Close(); var compressedData = compressedStream.ReadAllBytes(); // Make sure compressed data is aligned with 0x10 int padding = compressedData.Length % 0x10 == 0 ? 0 : (0x10 - compressedData.Length % 0x10); Array.Resize(ref compressedData, compressedData.Length + padding); return(compressedData); } }