public static BinaryReader Decompress(BinaryReader br, int size, int outsize, out int compressLevel) { if (input.Length < size) { input = new byte[size]; } if (output.Length < outsize) { output = new byte[outsize]; } int n = br.Read(input, 0, size); ms.Position = 0; ms.Write(input, 0, n); ms.SetLength(n); ms.Position = 0; using (var inf = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Decompress, Ionic.Zlib.CompressionLevel.Default, true)) { n = inf.Read(output, 0, output.Length); if (n != outsize) Trace.Write("Compression Output does not match expected size"); compressLevel = (int)Ionic.Zlib.CompressionLevel.Default; } ms.Position = 0; ms.Write(output, 0, outsize); ms.SetLength(outsize); ms.Position = 0; return compReader; }
public static byte[] CompressZlib(byte[] bytes) { using (MemoryStream ms = new MemoryStream()) { using (Ionic.Zlib.ZlibStream zip = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, true)) { zip.Write(bytes, 0, bytes.Length); } return ms.ToArray(); } }
public static BinaryReader Decompress(BinaryReader br, int size, int outsize, out int compressLevel) { if (input.Length < size) { input = new byte[size]; } if (output.Length < outsize) { output = new byte[outsize]; } int n = br.Read(input, 0, size); ms.Position = 0; ms.Write(input, 0, n); ms.SetLength(n); ms.Position = 0; using (var inf = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Decompress, Ionic.Zlib.CompressionLevel.Default, true)) { n = inf.Read(output, 0, output.Length); if (n != outsize) { Trace.Write("Compression Output does not match expected size"); } compressLevel = (int)Ionic.Zlib.CompressionLevel.Default; } ms.Position = 0; ms.Write(output, 0, outsize); ms.SetLength(outsize); ms.Position = 0; return(compReader); }
private static byte[] ZlibDecompress(Stream data, int expectedLength) { using (MemoryStream output = new MemoryStream(expectedLength)) { #if WITH_DOTNETZIP using (var stream = new Ionic.Zlib.ZlibStream(data, Ionic.Zlib.CompressionMode.Decompress)) { stream.CopyTo(output); } #elif WITH_SHARPCOMPRESS /* * using (var stream = new SharpCompress.Compressors.Deflate.ZlibStream(data, SharpCompress.Compressors.CompressionMode.Decompress)) * { * stream.CopyTo(output); * } */ #elif WITH_SHARPZIPLIB using (var stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(data)) { stream.CopyTo(output); } #else throw new NotImplementedException("Please define which compression library you want to use"); #endif return(output.ToArray()); } }
public override byte[] Encode(byte[] bytes) { using (var inputstr = new MemoryStream(bytes)) using (var outputstr = new MemoryStream()) { // http://stackoverflow.com/questions/9050260/what-does-a-zlib-header-look-like // http://stackoverflow.com/questions/18450297/is-it-possible-to-use-the-net-deflatestream-for-pdf-creation // https://web.archive.org/web/20130905215303/http://connect.microsoft.com/VisualStudio/feedback/details/97064/deflatestream-throws-exception-when-inflating-pdf-streams //#if IONIC using (var dstream = new Ionic.Zlib.ZlibStream(outputstr, Ionic.Zlib.CompressionMode.Compress)) { dstream.FlushMode = Ionic.Zlib.FlushType.Finish; //#elif NET40 // using (var dstream = new System.IO.Compression.DeflateStream(outputstr, System.IO.Compression.CompressionMode.Compress)) // { // // Zlib magic header (Default Compression): // outputstr.WriteByte(0x78); // outputstr.WriteByte(0x9C); //#else //NET45+ // using (var dstream = new System.IO.Compression.DeflateStream(outputstr, System.IO.Compression.CompressionLevel.Optimal)) // { // // Zlib magic header (Best Compression): // outputstr.WriteByte(0x78); // outputstr.WriteByte(0xDA); //#endif inputstr.CopyTo(dstream); inputstr.Flush(); } return(outputstr.ToArray()); } }
private void ReadAsBase64(XmlNode node, XmlNode dataNode) { Stream uncompressedData = new MemoryStream(Convert.FromBase64String(node.InnerText), false); if (dataNode.Attributes[AttributeNames.TileLayerAttributes.Compression] != null) { string compression = dataNode.Attributes[AttributeNames.TileLayerAttributes.Compression].Value; if (compression == "gzip") uncompressedData = new GZipStream(uncompressedData, CompressionMode.Decompress, false); else if (compression == "zlib") uncompressedData = new Ionic.Zlib.ZlibStream(uncompressedData, Ionic.Zlib.CompressionMode.Decompress, false); else throw new InvalidOperationException(String.Format("Unsupported compression type: {0}", compression)); } using (uncompressedData) { using (BinaryReader reader = new BinaryReader(uncompressedData)) { for (int i = 0; i < data.Length; i++) data[i] = reader.ReadUInt32(); } } }
public static string ZipString(string inString) { if (string.IsNullOrEmpty(inString)) return ""; try { if (Settings.Default.DebugServer) Functions.WriteLineToLogFile("Zipping up string of length " + inString.Length + ". Begins:" + inString.Substring(0, 5) + "|Ends:" + inString.Substring(inString.Length - 5)); byte[] buffer = Encoding.UTF8.GetBytes(inString); MemoryStream ms = new MemoryStream(); // Write from buffer => memorystream using (Ionic.Zlib.ZlibStream Zip = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestCompression, true)) { Zip.Write(buffer, 0, buffer.Length); } // Read from memorystream into byte array compressed[] ms.Position = 0; MemoryStream outStream = new MemoryStream(); byte[] compressed = new byte[ms.Length - 1]; ms.Read(compressed, 0, compressed.Length); ms.Close(); return Convert.ToBase64String(compressed); } catch (Exception ex) { Functions.WriteLineToLogFile("ZipHelper: Error zipping string:"); Functions.WriteExceptionToLogFile(ex); } return ""; }
public static void Encode(Stream src, Stream dst) { using (var zlib = new Ionic.Zlib.ZlibStream(dst, Ionic.Zlib.CompressionMode.Compress, true)) { src.CopyTo(zlib); } }
public static void Decode(Stream src, Stream dst) { using (var zlib = new Ionic.Zlib.ZlibStream(src, Ionic.Zlib.CompressionMode.Decompress, true)) { zlib.CopyTo(dst); } }
public override byte[] Encode(byte[] bytes) { using (var inputstr = new MemoryStream(bytes)) using (var outputstr = new MemoryStream()) { // http://stackoverflow.com/questions/9050260/what-does-a-zlib-header-look-like // http://stackoverflow.com/questions/18450297/is-it-possible-to-use-the-net-deflatestream-for-pdf-creation // https://web.archive.org/web/20130905215303/http://connect.microsoft.com/VisualStudio/feedback/details/97064/deflatestream-throws-exception-when-inflating-pdf-streams //#if IONIC using (var dstream = new Ionic.Zlib.ZlibStream(outputstr, Ionic.Zlib.CompressionMode.Compress)) { dstream.FlushMode = Ionic.Zlib.FlushType.Finish; //#elif NET40 // using (var dstream = new System.IO.Compression.DeflateStream(outputstr, System.IO.Compression.CompressionMode.Compress)) // { // // Zlib magic header (Default Compression): // outputstr.WriteByte(0x78); // outputstr.WriteByte(0x9C); //#else //NET45+ // using (var dstream = new System.IO.Compression.DeflateStream(outputstr, System.IO.Compression.CompressionLevel.Optimal)) // { // // Zlib magic header (Best Compression): // outputstr.WriteByte(0x78); // outputstr.WriteByte(0xDA); //#endif inputstr.CopyTo(dstream); inputstr.Flush(); } return outputstr.ToArray(); } }
public static string ZipString(string inString) { if (string.IsNullOrEmpty(inString)) return ""; try { if (Settings.Default.DebugServer) Functions.WriteLineToLogFile("Zipping up string of length " + inString.Length + ". Begins:" + inString.Substring(0, 5) + "|Ends:" + inString.Substring(inString.Length - 5)); byte[] buffer = Encoding.UTF8.GetBytes(inString); MemoryStream ms = new MemoryStream(); // Write from buffer => memorystream using (Ionic.Zlib.ZlibStream Zip = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestCompression, true)) { Zip.Write(buffer, 0, buffer.Length); } // Read from memorystream into byte array compressed[] ms.Position = 0; MemoryStream outStream = new MemoryStream(); byte[] compressed = new byte[ms.Length - 1]; ms.Read(compressed, 0, compressed.Length); ms.Close(); return Convert.ToBase64String(compressed); } catch (Exception ex) { Functions.WriteLineToLogFile("ZipHelper: Error zipping string:"); Functions.WriteExceptionToLogFile(ex); } return ""; }
public static byte[] Decode(ArraySegment <byte> buf) { using (var mem = new MemoryStream(buf.Array, buf.Offset, buf.Count, false)) using (var zlib = new Ionic.Zlib.ZlibStream(mem, Ionic.Zlib.CompressionMode.Decompress)) { return(zlib.ReadAllBytes()); } }
private static byte[] Decompress(byte[] compData, int uncompressedSize = 0) { using var ds = new Ionic.Zlib.ZlibStream(new MemoryStream(compData), Ionic.Zlib.CompressionMode.Decompress); using var decompressed = new MemoryStream(uncompressedSize == 0 ? compData.Length * 2 : uncompressedSize); ds.CopyTo(decompressed); return(decompressed.ToArray()); }
public static byte[] CompressData(byte[] data) { MemoryStream ms = new MemoryStream(); Ionic.Zlib.ZlibStream s = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.Level9); s.Write(data, 0, data.Length); s.Close(); return ms.ToArray(); }
private static byte[] Deflate(byte[] buffer, int sizeDecompressed, out int sizeCompressed, int compressionLevel) { MemoryStream output = new MemoryStream(); using (Ionic.Zlib.ZlibStream zs = new Ionic.Zlib.ZlibStream(output, Ionic.Zlib.CompressionMode.Compress, (Ionic.Zlib.CompressionLevel)compressionLevel, true)) zs.Write(buffer, 0, sizeDecompressed); sizeCompressed = (int)output.Length; return(output.ToArray()); }
public static byte[] Encode(ArraySegment <byte> buf) { using (var mem = new MemoryStream()) { using (var zlib = new Ionic.Zlib.ZlibStream(mem, Ionic.Zlib.CompressionMode.Compress)) { zlib.Write(buf.Array, buf.Offset, buf.Count); } return(mem.ToArray()); } }
private byte[] Inflate(byte[] buffer, int sizeDecompressed, out int sizeCompressed, int compressionLevel) { MemoryStream output = new MemoryStream(); Ionic.Zlib.ZlibStream zs = new Ionic.Zlib.ZlibStream(output, Ionic.Zlib.CompressionMode.Compress, (Ionic.Zlib.CompressionLevel)compressionLevel, true); zs.Write(buffer, 0, sizeDecompressed); zs.Flush(); zs.Close(); sizeCompressed = (int)output.Length; return(output.ToArray()); }
public static byte[] CompressData(byte[] data) { MemoryStream ms = new MemoryStream(); Ionic.Zlib.ZlibStream s = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.Level9); s.Write(data, 0, data.Length); s.Close(); return(ms.ToArray()); }
private static byte[] ZlibDecompress(Stream data, int expectedLength) { using (MemoryStream output = new MemoryStream(expectedLength)) { using (var stream = new Ionic.Zlib.ZlibStream(data, Ionic.Zlib.CompressionMode.Decompress)) { stream.CopyTo(output); } return(output.ToArray()); } }
private static GitObject ReadObject(FileInfo fi, ref Stream s) { s = new Ionic.Zlib.ZlibStream(s, Ionic.Zlib.CompressionMode.Decompress); var go = new GitObject(); var header = ReadHeader(s); go.ObjectType = header.Type; ReadBody(s, go, header); go.Id = fi.Directory.Name + fi.Name; return(go); }
public static byte[] CompressZlib(string ResponseData, Encoding e) { Byte[] bytes = e.GetBytes(ResponseData); using (MemoryStream ms = new MemoryStream()) { using (Ionic.Zlib.ZlibStream zip = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, true)) { zip.Write(bytes, 0, bytes.Length); } return(ms.ToArray()); } }
public static byte[] CompressZlib(string ResponseData, Encoding e) { Byte[] bytes = e.GetBytes(ResponseData); using (MemoryStream ms = new MemoryStream()) { using (Ionic.Zlib.ZlibStream zip = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, true)) { zip.Write(bytes, 0, bytes.Length); } return ms.ToArray(); } }
public static string DecompressZlib(Stream input, Encoding e) { using (Ionic.Zlib.ZlibStream decompressor = new Ionic.Zlib.ZlibStream(input, Ionic.Zlib.CompressionMode.Decompress)) { int read = 0; var buffer = new byte[BUFFER_SIZE]; using (MemoryStream output = new MemoryStream()) { while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); } return(e.GetString(output.ToArray())); } } }
public static byte[] Compress(byte[] data, int compressLevel) { var level = (Ionic.Zlib.CompressionLevel)compressLevel; if (ms == null) { Init(); } ms.Position = 0; ms.SetLength(0); using (var gstream = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, level, true)) { gstream.Write(data, 0, data.Length); gstream.Flush(); } return(ms.ToArray()); }
public static async Task<byte[]> CompressionByteAsync(byte[] str, CompressionType compressionType) { if (str == null) { return null; } using (var output = new MemoryStream()) { switch (compressionType) { case CompressionType.Deflate: using ( var compressor = new Ionic.Zlib.DeflateStream( output, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestSpeed)) { await compressor.WriteAsync(str, 0, str.Length); //compressor.Write(str, 0, str.Length); } break; case CompressionType.GZip: using ( var compressor = new Ionic.Zlib.GZipStream( output, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestSpeed)) { await compressor.WriteAsync(str, 0, str.Length); //compressor.Write(str, 0, str.Length); } break; case CompressionType.Zlib: using ( var compressor = new Ionic.Zlib.ZlibStream( output, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestSpeed)) { await compressor.WriteAsync(str, 0, str.Length); //compressor.Write(str, 0, str.Length); } break; } return output.ToArray(); } }
public static async Task <string> GZipUncompress([NotNull] this byte[] buffer) { if (buffer == null || buffer.LongLength == 0) { throw new ArgumentNullException("buffer"); } using (var ms = new MemoryStream(buffer)) using (var gzs = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Decompress, true)) using (var output = new MemoryStream()) { await gzs.CopyToAsync(output); var array = output.ToArray(); var str = Encoding.UTF8.GetString(array); return(str); } }
private Stream GetStream(byte[] data, Layer layer) { Stream stream; switch (compression) { case "gzip": stream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress); break; case "uncompressed": stream = new MemoryStream(data); break; case "zlib": stream = new Ionic.Zlib.ZlibStream(new MemoryStream(data), Ionic.Zlib.CompressionMode.Decompress); break; default: throw new UnsupportedCompressionException("Layer \"" + layer + "\" has unsupported compression."); } return stream; }
/// <summary> /// Un-compresses a ZLIB-compressed (Unix-style GZIP compression) byte array to the original source text /// </summary> /// <param name="buffer">The byte array that represents the compressed data to un-compress</param> /// <returns>The original UTF-8 string that was compressed</returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="buffer"/> is null or an empty string</exception> /// <exception cref="DecoderFallbackException">Thrown when characters are provided in the <param name="buffer"> that cannot be represented by UTF-8</param></exception> /// <exception cref="ArgumentException">Thrown when a byte array is passed in for the <paramref name="buffer"/> contains invalid data for the decompression routine.</exception> /// <exception cref="ObjectDisposedException">Thrown when the source or destination stream are disposed at the time they are internally copied for compression. Should never occur.</exception> /// <exception cref="NotSupportedException">Thrown when the source stream does not support reading or the destination stream does not support writing. Should never occur.</exception> public static async Task <string> GZipUncompressAsync(this byte[] buffer, CancellationToken cancellationToken = default(CancellationToken)) { if (buffer == null || buffer.LongLength == 0) { throw new ArgumentNullException("buffer"); } using (var ms = new MemoryStream(buffer)) using (var gzs = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Decompress, true)) using (var output = new MemoryStream()) { // Buffer size is default for underlying Stream.CopyToAsync() await gzs.CopyToAsync(output, 81920, cancellationToken); var array = output.ToArray(); var str = Encoding.UTF8.GetString(array); return(str); } }
public static async Task <byte[]> GZipCompress([NotNull] this string text) { if (string.IsNullOrWhiteSpace(text)) { throw new ArgumentNullException("text"); } var buffer = Encoding.UTF8.GetBytes(text); using (var ms = new MemoryStream(buffer)) using (var gzs = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, true)) using (var output = new MemoryStream()) { await gzs.CopyToAsync(output); var array = output.ToArray(); return(array); } }
public Stream ReadChunk(ChunkCoord coord) { lock (_file) { int offset = GetOffset(coord.X, coord.Z); if (offset == 0) { return(null); } int sectorNum = offset >> 8; int numSectors = offset & 255; if (sectorNum + numSectors > _freeSectors.Count) { return(null); } _stream.Seek((sectorNum * SectorBytes), SeekOrigin.Begin); int len = _stream.ReadInt32Helper(); if (len > SectorBytes * numSectors || len <= 0) { return(null); } sbyte ver = _stream.ReadByteHelper().ConvertToSbyte(); byte[] dat = new byte[len - 1]; _stream.Read(dat, 0, dat.Length); if (ver == VerGZip) { Stream ret = new GZipStream(new MemoryStream(dat), CompressionMode.Decompress); return(ret); } else if (ver == VerDeflate) { Stream ret = new Ionic.Zlib.ZlibStream(new MemoryStream(dat), Ionic.Zlib.CompressionMode.Decompress); return(ret); } } return(null); }
/// <summary> /// Compresses a string using ZLIB compression (Unix-style GZIP compression) /// </summary> /// <param name="text">The text to compress</param> /// <returns>A ZLIB compressed byte array representation of <paramref name="text"/></returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="text"/> is null or an empty string</exception> /// <exception cref="EncoderFallbackException">Thrown when characters are provided in the <param name="text"> that cannot be represented by UTF-8</param></exception> /// <exception cref="ObjectDisposedException">Thrown when the source or destination stream are disposed at the time they are internally copied for compression. Should never occur.</exception> /// <exception cref="NotSupportedException">Thrown when the source stream does not support reading or the destination stream does not support writing. Should never occur.</exception> public static async Task <byte[]> GZipCompressAsync(this string text, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrWhiteSpace(text)) { throw new ArgumentNullException("text"); } var buffer = Encoding.UTF8.GetBytes(text); using (var ms = new MemoryStream(buffer)) using (var gzs = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, true)) using (var output = new MemoryStream()) { // Buffer size is default for underlying Stream.CopyToAsync() await gzs.CopyToAsync(output, 81920, cancellationToken); var array = output.ToArray(); return(array); } }
private static byte[] zlibCompressData(string body) { using (var stream = new MemoryStream()) { using (var inflater = new Ionic.Zlib.ZlibStream(stream, Ionic.Zlib.CompressionMode.Compress)) using (var streamWriter = new StreamWriter(inflater)) { streamWriter.Write(body); } var res = stream.ToArray(); /*var ret = new byte[res.Length + 6]; * ret[0] = 120; * ret[1] = 156; * res.CopyTo(ret, 2); * var adl = new Adler32Computer(); * adl.Update(res, 0, res.Length); * BitConverter.GetBytes(adl.Checksum).CopyTo(ret, 2 + res.Length);*/ return(res); } }
private void ReadAsBase64(XmlNode node, XmlNode dataNode) { if (String.IsNullOrEmpty(node.InnerText)) { return; } Stream uncompressedData = new MemoryStream(Convert.FromBase64String(node.InnerText), false); // compression is optional, so we check if the attribute is available if (dataNode.Attributes[AttributeNames.TileLayerAttributes.Compression] != null) { string compression = dataNode.Attributes[AttributeNames.TileLayerAttributes.Compression].Value; if (compression == "gzip") { uncompressedData = new GZipStream(uncompressedData, CompressionMode.Decompress, false); } else if (compression == "zlib") { uncompressedData = new Ionic.Zlib.ZlibStream(uncompressedData, Ionic.Zlib.CompressionMode.Decompress, false); } else { throw new InvalidOperationException(String.Format("Unsupported compression type: {0}. Only 'gzip' and 'zlib' are supported at this time.", compression)); } } using (uncompressedData) { using (BinaryReader reader = new BinaryReader(uncompressedData)) { for (int i = 0; i < data.Length; i++) { data[i] = reader.ReadUInt32(); } } } }
public byte[] GetContents() { BinaryReader reader = new BinaryReader(File.Open(Parent.Path, FileMode.Open, FileAccess.Read)); reader.BaseStream.Seek(Offset, SeekOrigin.Begin); byte[] buffer = reader.ReadBytes((Int32)Size); reader.Close(); if (IsCompressed) { using (MemoryStream compressedStream = new MemoryStream(buffer)) { using (Ionic.Zlib.ZlibStream zlibStream = new Ionic.Zlib.ZlibStream(compressedStream, Ionic.Zlib.CompressionMode.Decompress)) { byte[] decompressed = new byte[SizeUncompressed]; zlibStream.Read(decompressed, 0, (Int32)SizeUncompressed); return(decompressed); } } } return(buffer); }
public byte[] Compress(byte[] buffer) { MemoryStream result = new MemoryStream(); BinaryWriter bw = new BinaryWriter(result); bw.Write(new byte[] { 0x54, 0x4C, 0x5A, 0x43 }); bw.Write((byte)0x01); bw.Write((byte)0x02); bw.Write((byte)0x00); bw.Write((byte)0x00); bw.Write((int)0); // compressed size - we'll fill this in once we know it bw.Write((int)buffer.Length); // decompressed size bw.Write((int)0); // unknown, 0 bw.Write((int)0); // unknown, 0 bool assume_zlib = true; if (assume_zlib) { using (Stream compressionStream = new Ionic.Zlib.ZlibStream(result, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestCompression)) { compressionStream.Write(buffer); } } else { using (DeflateStream compressionStream = new DeflateStream(result, CompressionLevel.Optimal)) { compressionStream.Write(buffer); } } byte[] retval = result.ToArray(); // fill in compressed size Util.CopyByteArrayPart(BitConverter.GetBytes((int)retval.Length), 0, retval, 8, 4); return(retval); }
private void ReadAsBase64(NanoXMLNode dataNode) { // get a stream to the decoded Base64 text Stream data = new MemoryStream(Convert.FromBase64String(dataNode.Value), false); // figure out what, if any, compression we're using. the compression determines // if we need to wrap our data stream in a decompression stream if (dataNode.GetAttribute("compression") != null) { string compression = dataNode.GetAttribute("compression").Value; if (compression == "gzip") { data = new Ionic.Zlib.GZipStream(data, Ionic.Zlib.CompressionMode.Decompress, false); } else if (compression == "zlib") { data = new Ionic.Zlib.ZlibStream(data, Ionic.Zlib.CompressionMode.Decompress, false); } else { throw new InvalidOperationException("Unknown compression: " + compression); } } // simply read in all the integers using (data) { using (BinaryReader reader = new BinaryReader(data)) { for (int i = 0; i < Data.Length; i++) { Data[i] = reader.ReadUInt32(); } } } }
private void ReadAsBase64(XmlNode node, XmlNode dataNode) { // get a stream to the decoded Base64 text Stream data = new MemoryStream(Convert.FromBase64String(node.InnerText), false); // figure out what, if any, compression we're using. the compression determines // if we need to wrap our data stream in a decompression stream if (dataNode.Attributes["compression"] != null) { string compression = dataNode.Attributes["compression"].Value; if (compression == "gzip") { data = new GZipStream(data, CompressionMode.Decompress, false); } else if (compression == "zlib") { data = new Ionic.Zlib.ZlibStream(data, Ionic.Zlib.CompressionMode.Decompress, false); } else { throw new InvalidOperationException("Unknown compression: " + compression); } } // simply read in all the integers using (data) { using (BinaryReader reader = new BinaryReader(data)) { for (int i = 0; i < Data.Length; i++) { Data[i] = reader.ReadUInt32(); } } } }
public byte[] Decompress(byte[] buffer) { MemoryStream result = new MemoryStream(); int inSize = BitConverter.ToInt32(buffer, 8); int outSize = BitConverter.ToInt32(buffer, 12); int offset = 0x18; bool assume_zlib = true; if (assume_zlib) { using (Stream decompressionStream = new Ionic.Zlib.ZlibStream(new MemoryStream(buffer, offset, inSize - offset), Ionic.Zlib.CompressionMode.Decompress)) { Util.CopyStream(decompressionStream, result, outSize); } } else { using (DeflateStream decompressionStream = new DeflateStream(new MemoryStream(buffer, offset, inSize - offset), CompressionMode.Decompress)) { Util.CopyStream(decompressionStream, result, outSize); } } return(result.ToArray()); }
public void TestReadHeader() { var file = new System.IO.FileInfo(Path.Combine(Path.GetTempPath(), "objects", Path.GetRandomFileName())); if (!file.Directory.Exists) { file.Directory.Create(); } File.WriteAllBytes(file.FullName, TestResources.singlefiletree); try { using (var stream = new Ionic.Zlib.ZlibStream(file.OpenRead(), Ionic.Zlib.CompressionMode.Decompress)) { ObjectHeader result = ObjectFileParser.ReadHeader(stream); Assert.AreEqual(ObjectType.Tree, result.Type); Assert.AreEqual(36, result.Size); } } finally { file.Delete(); } }
//static readonly char[] padding = { '=' };.TrimEnd(padding) private string Compress(string s) { Encoding enc = Encoding.GetEncoding("us-ascii"); byte[] bytes = enc.GetBytes(s); MemoryStream output = new MemoryStream(); /*using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal)) * { * dstream.Write(bytes, 0, bytes.Length); * } obviously not the same Deflate as in zlib!*/ // ZLib Deflate using (Ionic.Zlib.ZlibStream dstream = new Ionic.Zlib.ZlibStream(output, Ionic.Zlib.CompressionMode.Compress)) { dstream.Write(bytes, 0, bytes.Length); dstream.Close(); } string base64string = Convert.ToBase64String(output.ToArray()).Replace('+', '-').Replace('/', '_'); return(base64string); }
public void TestReadHeader() { var file = new System.IO.FileInfo(Path.Combine(Path.GetTempPath(), "objects", Path.GetRandomFileName())); if (!file.Directory.Exists) { file.Directory.Create(); } File.WriteAllBytes(file.FullName, TestResources.singlefiletree); try { using (var stream = new Ionic.Zlib.ZlibStream(file.OpenRead(), Ionic.Zlib.CompressionMode.Decompress)) { ObjectHeader result = ObjectFileParser.ReadHeader(stream); Assert.AreEqual(ObjectType.Tree, result.Type); Assert.AreEqual(36, result.Size); } } finally { file.Delete(); } }
public static byte[] Compress(byte[] data, int compressLevel) { var level = (Ionic.Zlib.CompressionLevel)compressLevel; if (ms == null) Init(); ms.Position = 0; ms.SetLength(0); using (var gstream = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, level, true)) { gstream.Write(data, 0, data.Length); gstream.Flush(); } return ms.ToArray(); }
public static string DecompressZlib(Stream input, Encoding e) { using (Ionic.Zlib.ZlibStream decompressor = new Ionic.Zlib.ZlibStream(input, Ionic.Zlib.CompressionMode.Decompress)) { int read = 0; var buffer = new byte[BUFFER_SIZE]; using (MemoryStream output = new MemoryStream()) { while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); } return e.GetString(output.ToArray()); } } }
private static int[,] DecompressLayerZlib(Stream Input, int width, int height) { Ionic.Zlib.ZlibStream ZlibDecompressed = new Ionic.Zlib.ZlibStream(Input, Ionic.Zlib.CompressionMode.Decompress); return DecompressedLayer(ZlibDecompressed, width, height); }
private static GitObject ReadObject(FileInfo fi, ref Stream s) { s = new Ionic.Zlib.ZlibStream(s, Ionic.Zlib.CompressionMode.Decompress); var go = new GitObject(); var header = ReadHeader(s); go.ObjectType = header.Type; ReadBody(s, go, header); go.Id = fi.Directory.Name + fi.Name; return go; }
public void GetSwf() { try { // check if we can retrieve if (string.IsNullOrEmpty(swfUrl)) return; Uri swfUri = new Uri(swfUrl); if (swfCache.ContainsKey(swfUrl)) { SWFHash = swfCache[swfUrl].Hash; SWFSize = swfCache[swfUrl].Size; } else { // get the swf file from the web HttpWebRequest request = WebRequest.Create(swfUri) as HttpWebRequest; if (request == null) return; request.Accept = "*/*"; request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); request.Timeout = 5000; // don't wait longer than 5 seconds HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.Stream responseStream; if (response.ContentEncoding.ToLower().Contains("gzip")) responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress); else if (response.ContentEncoding.ToLower().Contains("deflate")) responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress); else responseStream = response.GetResponseStream(); byte[] tempBuff = new byte[1024 * 1024 * 10]; int bytesRead = 0; int totalBytesRead = 0; while ((bytesRead = responseStream.Read(tempBuff, totalBytesRead, tempBuff.Length - totalBytesRead)) > 0) { totalBytesRead += bytesRead; } byte[] buff = new byte[totalBytesRead]; Array.Copy(tempBuff, buff, totalBytesRead); MemoryStream ms = new MemoryStream(buff); BinaryReader br = new BinaryReader(ms); // compressed swf? if (br.PeekChar() == 'C') { // read size br.BaseStream.Position = 4; // skip signature SWFSize = Convert.ToInt32(br.ReadUInt32()); // read swf head byte[] uncompressed = new byte[SWFSize]; br.BaseStream.Position = 0; br.Read(uncompressed, 0, 8); // header data is not compressed uncompressed[0] = System.Text.Encoding.ASCII.GetBytes(new char[] { 'F' })[0]; // un-zip byte[] compressed = br.ReadBytes(SWFSize); Ionic.Zlib.ZlibStream dStream = new Ionic.Zlib.ZlibStream(new MemoryStream(compressed), Ionic.Zlib.CompressionMode.Decompress); int read = dStream.Read(uncompressed, 8, SWFSize - 8); byte[] finalUncompressed = new byte[8 + read]; Array.Copy(uncompressed, finalUncompressed, 8 + read); buff = finalUncompressed; } System.Security.Cryptography.HMACSHA256 sha256Hmac = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.ASCII.GetBytes("Genuine Adobe Flash Player 001")); SWFHash = sha256Hmac.ComputeHash(buff); Logger.Log(string.Format("Size of decompressed SWF: {0}, Hash:", SWFSize)); Logger.LogHex(SWFHash, 0, SWFHash.Length); swfCache.Add(swfUrl, new SwFInfo() { Hash = SWFHash, Size = SWFSize, Time = DateTime.Now }); } } catch (Exception ex) { Logger.Log(string.Format("Error while getting swf ({0}): {1}", swfUrl, ex.Message)); } }
public unsafe void CreateTexture(BinaryReader reader, int[] palette) { if (Width == 0 || Height == 0) return; Image = new Bitmap(Width, Height); MaskImage = new Bitmap(1, 1); BitmapData data = Image.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); byte[] bytes = new byte[0]; byte[] maskbytes = new byte[0]; MemoryStream output; switch (nType) { case 0://wemade wil file uncompressed if (palette.Length > 256) { bo16bit = true; nSize = nSize * 2; } bytes = reader.ReadBytes(nSize); break; case 1://shanda wzl file compressed case 4://shanda miz file compressed output = new MemoryStream(); Ionic.Zlib.ZlibStream deflateStream = new Ionic.Zlib.ZlibStream(output,Ionic.Zlib.CompressionMode.Decompress); deflateStream.Write(reader.ReadBytes(nSize), 0, nSize); bytes = output.ToArray(); deflateStream.Close(); output.Close(); break; case 2: byte Compressed = reader.ReadByte(); reader.ReadBytes(5); if (Compressed != 8) { bytes = reader.ReadBytes(nSize - 6); break; } MemoryStream input = new MemoryStream(reader.ReadBytes(nSize-6)); output = new MemoryStream(); byte[] buffer = new byte[10]; System.IO.Compression.DeflateStream decompress = new System.IO.Compression.DeflateStream(input, System.IO.Compression.CompressionMode.Decompress); int len; while ((len = decompress.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, len); } bytes = output.ToArray(); decompress.Close(); output.Close(); input.Close(); break; case 3: MaskImage = new Bitmap(Width, Height); byte[][] DecodedPixels = DecompressWemadeMir3(reader,Width,Height,nSize); if (DecodedPixels != null) { bytes = DecodedPixels[0]; if (HasMask) maskbytes = DecodedPixels[1]; } else { HasMask = false; bytes = new byte[Width * Height * 2]; } break; } int index = 0; int* scan0 = (int*) data.Scan0; { for (int y = Height - 1; y >= 0; y--) { for (int x = 0; x < Width; x++) { if (bo16bit) scan0[y * Width + x] = convert16bitTo32bit(bytes[index++] + (bytes[index++] << 8)); else scan0[y*Width + x] = palette[bytes[index++]]; } if (((nType == 1) || (nType == 4)) & (Width % 4 > 0)) index += WidthBytes(bo16bit ? 16 : 8, Width) - (Width * (bo16bit ? 2 : 1)); } } Image.UnlockBits(data); index = 0; if (HasMask) { BitmapData Maskdata = MaskImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); int* maskscan0 = (int*)Maskdata.Scan0; { for (int y = Height - 1; y >= 0; y--) { for (int x = 0; x < Width; x++) maskscan0[y * Width + x] = convert16bitTo32bit(maskbytes[index++] + (maskbytes[index++] << 8)); } } MaskImage.UnlockBits(Maskdata); } }