private static DataRecord GetData(byte[] input, string filename, CompressType compress) { if (compress == CompressType.Nothing) { return new DataRecord() { Data = input }; } if (compress == CompressType.ByExtension && _noCompressExt.Contains(System.IO.Path.GetExtension(filename))) { return new DataRecord() { Data = input }; } var cdata = new System.IO.MemoryStream(); //Lzs.Encode(new System.IO.MemoryStream(input), cdata); byte[] lprops; using (var lzma = new SharpCompress.Compressors.LZMA.LzmaStream(new SharpCompress.Compressors.LZMA.LzmaEncoderProperties(), false, cdata)) { lzma.Write(input, 0, input.Length); lprops = lzma.Properties; } if (/*compress == CompressType.ByContent &&*/ (cdata.Length + lprops.Length + 8) > (input.Length * 10 / 8)) { return new DataRecord() { Data = input }; } byte[] data = new byte[cdata.Length + lprops.Length + 8]; Array.Copy(BitConverter.GetBytes(input.Length), data, 4); Array.Copy(BitConverter.GetBytes(lprops.Length), 0, data, 4, 4); Array.Copy(lprops, 0, data, 8, lprops.Length); cdata.Position = 0; cdata.Read(data, lprops.Length + 8, (int)cdata.Length); return new DataRecord() { Data = data, Compressed = true }; }
/// <summary> /// Decompresses the specified data. /// </summary> /// <param name="data">The data.</param> /// <param name="index">The index.</param> /// <param name="originalDataLength">Length of the original data.</param> protected byte[] Decompress(byte[] data, int index, int originalDataLength) { // PayBySquare limit is 550 utf-8 characters // longest utf-8 character is 4 bytes var buffer = new byte[originalDataLength]; // decompress data using (var input = new MemoryStream(data, index, data.Length - index)) using (var decompressor = new SharpCompress.Compressors.LZMA.LzmaStream(this.LzmaDecoderProperties, input, input.Length, originalDataLength)) { decompressor.Read(buffer, 0, originalDataLength); } // return the compressed data return(buffer); }
/// <summary> /// Compresses the specified data. /// </summary> /// <param name="data">The data.</param> protected byte[] Compress(byte[] data) { using var output = new MemoryStream(); // compress data var encoderProperties = new SharpCompress.Compressors.LZMA.LzmaEncoderProperties(false, this.LzmaEncoderDictionarySize); using (var compressor = new SharpCompress.Compressors.LZMA.LzmaStream(encoderProperties, false, output)) { // compressor flushes to output stream at dispose compressor.Write(data, 0, data.Length); } // return the compressed data return(output.ToArray()); }
//private int _cacheCounter = 0; private CacheEntry GetCache(DirectoryEntry e) { CacheEntry ce; if (!_cache.TryGetValue(e.Offset, out ce)) { ce = new CacheEntry() { File = e.Filename }; byte[] data; lock (_data) { switch (e.Flags & FileFlags.COMPRESSION_FLAGS) { case FileFlags.CompressLZS: data = new byte[e.Length]; _data.Position = e.Offset; _data.Read(data, 0, e.Length); var ms = new System.IO.MemoryStream(data); var output = new System.IO.MemoryStream(); Lzs.Decode(ms, output); data = new byte[output.Length]; output.Position = 0; output.Read(data, 0, data.Length); ce.Data = data; break; case FileFlags.CompressLZMA: _data.Position = e.Offset; int decSize = _data.ReadInt(), propSize = _data.ReadInt(); byte[] props = new byte[propSize]; _data.Read(props, 0, props.Length); byte[] cdata = new byte[e.Length - propSize - 8]; _data.Read(cdata, 0, cdata.Length); data = new byte[decSize]; var lzma = new SharpCompress.Compressors.LZMA.LzmaStream(props, new System.IO.MemoryStream(cdata)); lzma.Read(data, 0, data.Length); /*int srcSize = cdata.Length; * switch (LzmaUncompress(data, ref decSize, cdata, ref srcSize, props, props.Length)) { * case SZ_OK: * //Woohoo! * break; * default: * throw new IrosArcException("Error decompressing " + e.Filename); * }*/ ce.Data = data; break; default: throw new IrosArcException("Bad compression flags " + e.Flags.ToString()); } } _cache.AddOrUpdate(e.Offset, ce, (_, __) => ce); } ce.LastAccess = DateTime.Now; CleanCache(); /* * if ((_cacheCounter++ % 100) == 0) * System.Diagnostics.Debug.WriteLine("IRO cache contents; " + String.Join(",", _cache.Values.Select(e => e.File))); */ return(ce); }
public override void SetBaseStream(Stream stream) { BaseStream = new SharpCompress.Compressors.LZMA.LzmaStream(new[] { _dictionarySize }, stream); }