internal static byte[] Compress(byte[] Input, UInt32 Offset, UInt32 InputSize) { SevenZip.Compression.LZMA.Encoder Coder = new SevenZip.Compression.LZMA.Encoder(); MemoryStream InStream = new MemoryStream(Input, (int)Offset, (int)InputSize); MemoryStream OutStream = new MemoryStream(); // Write the encoder properties Coder.WriteCoderProperties(OutStream); // Write the decompressed file size OutStream.Write(BitConverter.GetBytes(InStream.Length), 0, 8); // Encode the file Coder.Code(InStream, OutStream, (Int64)InputSize, -1, null); byte[] Output = new byte[OutStream.Length]; Buffer.BlockCopy(OutStream.GetBuffer(), 0, Output, 0, (int)OutStream.Length); OutStream.Flush(); OutStream.Close(); InStream.Close(); return(Output); }
public static byte[] Compress(byte[] inbuf) { SevenZip.CoderPropID[] propIDs = { SevenZip.CoderPropID.DictionarySize, SevenZip.CoderPropID.PosStateBits, SevenZip.CoderPropID.LitContextBits, SevenZip.CoderPropID.LitPosBits, SevenZip.CoderPropID.Algorithm, SevenZip.CoderPropID.NumFastBytes, SevenZip.CoderPropID.MatchFinder, SevenZip.CoderPropID.EndMarker }; object[] properties = { (Int32)(23), (Int32)(2), (Int32)(3), (Int32)(2), (Int32)(1), (Int32)(128), (string)("bt4"), (bool)(true) }; SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); coder.SetCoderProperties(propIDs, properties); MemoryStream msInp = new MemoryStream(inbuf); MemoryStream msOut = new MemoryStream(); coder.WriteCoderProperties(msOut); coder.Code(msInp, msOut, -1, -1, null); return(msOut.ToArray()); }
private void CompressArchive() { if (archiveFileName2 == null) { archiveFileName2 = Path.Combine(Path.GetTempPath(), "log-" + DateTime.UtcNow.ToString("yyyyMMdd-HHmmss") + ".tar.lzma"); SharedData.Instance.ArchiveFileName = archiveFileName2; SharedData.Instance.TempFiles.Add(archiveFileName2); } // Compress the tar file with LZMA // Source: http://stackoverflow.com/a/8605828/143684 SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); using (lzmaInput = new FileStream(archiveFileName1, FileMode.Open)) using (lzmaOutput = new FileStream(archiveFileName2, FileMode.Create)) // Creates new or truncates existing { SharedData.Instance.OpenDisposables.Add(lzmaInput); SharedData.Instance.OpenDisposables.Add(lzmaOutput); coder.WriteCoderProperties(lzmaOutput); // Write the decompressed file size lzmaOutput.Write(BitConverter.GetBytes(lzmaInput.Length), 0, 8); coder.Code(lzmaInput, lzmaOutput, lzmaInput.Length, -1, this); } SharedData.Instance.OpenDisposables.Remove(lzmaInput); SharedData.Instance.OpenDisposables.Remove(lzmaOutput); // Clean up the uncompressed tar file, we only keep the name of the compressed lzma file // from now on to delete it later. File.Delete(archiveFileName1); SharedData.Instance.TempFiles.Remove(archiveFileName1); archiveFileName1 = null; }
//compresses the given byte array public static byte[] Compress(byte[] uncbytes) { if (uncbytes == null) { return(null); } //create the encoder object SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); //create the memory streams MemoryStream input = new MemoryStream(uncbytes); MemoryStream output = new MemoryStream(); //set the properties of the coder coder.WriteCoderProperties(output); //write the size of the array output.Write(System.BitConverter.GetBytes(input.Length), 0, 8); //encode the byte array coder.Code(input, output, input.Length, -1, null); //return the compressed array return(output.GetBuffer()); }
/// <summary> /// 压缩一段字节 /// </summary> /// <param name="_in">源数据</param> /// <param name="_out">压缩后的数据,注意这里的数据长度通常大于压缩资源本身</param> /// <param name="_length">压缩后的实际字节大小, 通常来说 _length 小于 _out.Length </param> private static Boolean _compress(Byte[] _in, out Byte[] _out, out Int64 _length) { try { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); using (MemoryStream out_stream = new MemoryStream()) { using (MemoryStream in_stream = new MemoryStream(_in)) { encoder.WriteCoderProperties(out_stream); // 写入源文件长度 // out_stream.Write( System.BitConverter.GetBytes( _in.Length ), 0, 8 ); encoder.Code(in_stream, out_stream, in_stream.Length, -1, null); } _out = out_stream.GetBuffer(); _length = out_stream.Length; } } catch (Exception ex) { _out = null; _length = 0; Debug.Log(ex); return(false); } return(true); }
/** 同步压缩一个文件 **/ private static void Compress(object obj) { FileChangeInfo info = (FileChangeInfo)obj; string inpath = info.inpath; string outpath = info.outpath; CodeProgress codeProgress = null; if (info.progressDelegate != null) { codeProgress = new CodeProgress(info.progressDelegate); } try { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); FileStream inputFS = new FileStream(inpath, FileMode.Open); FileStream outputFS = new FileStream(outpath, FileMode.Create); encoder.WriteCoderProperties(outputFS); outputFS.Write(System.BitConverter.GetBytes(inputFS.Length), 0, 8); encoder.Code(inputFS, outputFS, inputFS.Length, -1, codeProgress); outputFS.Flush(); outputFS.Close(); inputFS.Close(); Debug.Log("压缩完毕"); } catch (Exception ex) { Debug.Log(ex); } }
public static byte[] ToBytes(string srcfile) { var src = System.IO.File.OpenRead(srcfile); var dest = new System.IO.MemoryStream(); var encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(new SevenZip.CoderPropID[] { SevenZip.CoderPropID.NumFastBytes }, new object[] { (int)5 }); encoder.WriteCoderProperties(dest); byte[] buf = BitConverter.GetBytes(src.Length); dest.Write(buf, 0, 4); var poslen = dest.Position; dest.Write(buf, 0, 4); var start = dest.Position; encoder.Code(src, dest, src.Length, -1, null); var len = dest.Position - start; buf = BitConverter.GetBytes(len); dest.Position = poslen; dest.Write(buf, 0, 4); var array = dest.ToArray(); dest.Close(); src.Close(); return(array); }
public static byte[] Encode(string s) { byte[] result; byte[] str = new byte[s.Length]; int v = 0; foreach (var item in s) { str[v] = (byte)item; v++; } MemoryStream ins = new MemoryStream(str); MemoryStream outs = new MemoryStream(); SevenZip.Compression.LZMA.Encoder lzma = new SevenZip.Compression.LZMA.Encoder(); lzma.WriteCoderProperties(outs); outs.Write(BitConverter.GetBytes(ins.Length), 0, 8); lzma.Code(ins, outs, -1, -1, null); result = outs.ToArray(); //outs.Flush(); outs.Close(); outs.Dispose(); ins.Close(); ins.Dispose(); return(result); }
public static byte[] Compress(byte[] src) { if (src == null) { return(null); } SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); MemoryStream input = new MemoryStream(src); MemoryStream output = new MemoryStream(); coder.WriteCoderProperties(output); output.Write(BitConverter.GetBytes(input.Length), 0, 8); coder.Code(input, output, input.Length, -1, null); output.Flush(); input.Flush(); var bytes = output.ToArray(); output.Close(); input.Close(); output.Dispose(); input.Dispose(); return(bytes); }
public static void CompressFile(string inFile, string outFile, CompressionFormat format) { using (FileStream input = new FileStream(inFile, FileMode.Open, FileAccess.Read)) using (FileStream output = new FileStream(outFile, FileMode.Create)) { if (format == CompressionFormat.LZMA) { // Credit: http://stackoverflow.com/questions/7646328/how-to-use-the-7z-sdk-to-compress-and-decompress-a-file Compression.LZMA.Encoder coder = new Compression.LZMA.Encoder(); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); } else if (format == CompressionFormat.GZIP) { using (GZipStream compressionStream = new GZipStream(output, CompressionMode.Compress)) { input.CopyTo(compressionStream); } } else { input.CopyTo(output); } } }
public void Zip3 () { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); string from = " i am here ,so cool !asdfasfsafsafsfsdfsdf"; from += " i am here ,so cool !asdfasfsafsafsfsdfsdf"; from += " i am here ,so cool !asdfasfsafsafsfsdfsdf"; byte[] array = Encoding.UTF8.GetBytes(from); MemoryStream input = new MemoryStream (array); Debug.Log (Application.dataPath + "/2.zip"); MemoryStream output = new MemoryStream (); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); //PlayerPrefs. // Encode the file. coder.Code(input, output, input.Length, -1, null); //System.Text.Encoding. var b = output.GetBuffer (); Debug.Log (from.Length); Debug.Log (System.Convert.ToBase64String (b).Length); var bb = System.Convert.ToByte (System.Convert.ToBase64String (b)); output.Flush(); output.Close(); input.Close(); }
public LZMACompressionStream(Stream stream, CompressionMode mode, bool LeaveOpen, int DictionarySize, int PosStateBits, int LitContextBits, int LitPosBits, int Algorithm, int NumFastBytes, string MatchFinder, bool EndMarker) { this.stream = stream; this.LeaveOpen = LeaveOpen; BufferStream = new PumpStream(); if (mode == CompressionMode.Compress) { Encoder = new SevenZip.Compression.LZMA.Encoder(); if (DictionarySize != 0) { Encoder.SetCoderProperties( new SevenZip.CoderPropID[8] { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }, new object[8] { DictionarySize, PosStateBits, LitContextBits, LitPosBits, Algorithm, NumFastBytes, MatchFinder, EndMarker }); } Encoder.WriteCoderProperties(stream); WorkThread = new Thread(new ThreadStart(Encode)); } else { byte[] DecoderProperties = new byte[5]; stream.Read(DecoderProperties, 0, 5); Decoder = new SevenZip.Compression.LZMA.Decoder(); Decoder.SetDecoderProperties(DecoderProperties); WorkThread = new Thread(new ThreadStart(Decode)); } WorkThread.Start(); }
private void SendCompressedMessage(ref byte[] uncompressed) { if (TcpConnection == null) { Console.WriteLine("- TCP connection closed, can't send large message"); return; } // prepare streams MemoryStream inData = new MemoryStream(uncompressed); inData.Position = 0; MemoryStream outData = new MemoryStream(); // compress the data Encoder compressor = new Encoder(); compressor.WriteCoderProperties(outData); outData.Write(BitConverter.GetBytes(inData.Length), 0, 8); compressor.Code(inData, outData, inData.Length, -1, null); outData.Position = 0; Console.WriteLine("< !COMPRESSED-TCP! {0} -> {1} bytes", inData.Length, outData.Length); // close the in stream as it is no longer needed inData.Close(); if (TcpConnection != null) { TcpConnection.SendObject(TcpByteMessageName, outData.ToArray()); } outData.Close(); }
public static byte[] Zip(byte[] buf) { byte[] outBuf = null; using (MemoryStream outStream = new MemoryStream()) { using (MemoryStream inStream = new MemoryStream(buf)) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); //写入长度 outStream.Write(BitConverter.GetBytes(inStream.Length), 0, 8); //写入属性 coder.WriteCoderProperties(outStream); //压缩 coder.Code(inStream, outStream, inStream.Length, -1, null); outStream.Flush(); outBuf = outStream.ToArray(); } } return(outBuf); }
public static byte[] CompressData(byte[] input) { var encoder = new LZMAEncoder(); using (var uncompressed = new MemoryStream(input)) { using (var compressed = new MemoryStream()) { encoder.SetCoderProperties(new[] { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }, new object[] { 262144, 2, 3, 0, 2, 32, "bt4", false }); encoder.WriteCoderProperties(compressed); compressed.Write(BitConverter.GetBytes(uncompressed.Length), 0, 4); encoder.Code(uncompressed, compressed, uncompressed.Length, -1L, null); return(compressed.ToArray()); } } }
public static void CompressStrLZMA(byte[] inBytes, uint startPos, uint inLen, ref byte[] outBytes, ref uint outLen) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); MemoryStream inStream = new MemoryStream(); inStream.Write(inBytes, (int)startPos, (int)inLen); inStream.Position = 0; int saveinsize = (int)inLen; int saveoutsize = (int)(saveinsize * 1.1 + 1026 * 16 + LZMA_HEADER_LEN); outBytes = new byte[saveoutsize]; MemoryStream outStream = new MemoryStream(outBytes); // Write the encoder properties coder.WriteCoderProperties(outStream); // Write the decompressed file size. outStream.Write(BitConverter.GetBytes(inStream.Length), 0, 8); // Encode the file. coder.Code(inStream, outStream, inStream.Length, -1, null); saveoutsize = (int)outStream.Position; outStream.Flush(); outStream.Close(); inStream.Close(); outLen = (uint)saveoutsize; }
public static void CompressInternalLzma(Stream inStream, Stream outStream, int dictionary, int posStateBits, int litContextBits, int litPosBits, int numFastBytes, string mf) { // somewhat copied from LzmaAlone int algorithm = 2; bool eos = false; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); outStream.WriteInt64(inStream.Length, EndianUtils.Endianness.LittleEndian); encoder.Code(inStream, outStream, -1, -1, null); }
private static byte[] LZMAEncode(byte[] inputData, byte[] properties) { if (inputData.Length < 5) { throw new ArgumentException("Input data is too short."); } var encoder = new SevenZip.Compression.LZMA.Encoder(); var val = properties[0] / 9; var dSize = BitConverter.ToUInt32(properties, 1); var obs = new object[] { properties[0] % 9, val % 5, val / 5, dSize }; encoder.SetCoderProperties(new CoderPropID[] { CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.PosStateBits, CoderPropID.DictionarySize }, obs); using (var outLZMA = new MemoryStream()) using (var inLZMA = new MemoryStream(inputData, 0, inputData.Length)) { encoder.Code(inLZMA, outLZMA, inputData.Length, -1, null); return(outLZMA.ToArray()); } }
public override void Write(byte[] buffer, int offset, int count) { if (CanWrite) { SevenZip.Compression.LZMA.Encoder encoder = (SevenZip.Compression.LZMA.Encoder)coder; if (iniciadoCoder == false) { Int32 dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), "bt4", false }; encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(stream); Int64 fileSize = buffer.Length; for (int i = 0; i < 8; i++) { stream.WriteByte((Byte)(fileSize >> (8 * i))); } iniciadoCoder = true; } MemoryStream flujoEntrada = new MemoryStream(buffer, offset, count); encoder.Code(flujoEntrada, stream, -1, -1, null); } }
static Data() { var encoder = new SevenZip.Compression.LZMA.Encoder(); using var propertiesStream = new MemoryStream(5); encoder.WriteCoderProperties(propertiesStream); propertiesStream.Flush(); Properties = propertiesStream.ToArray(); }
public static void Compress(Stream input, Stream output) { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.WriteCoderProperties(output); output.Write(BitConverter.GetBytes(input.Length), 0, 8); encoder.Code(input, output, input.Length, -1, null); output.Flush(); }
//re-compress node public override void Encode(BinaryWriter writer) { // encode the node into bytes byte[] data; MemoryStream uncompressedStream = new MemoryStream(); using (BinaryWriter w = new BinaryWriter(uncompressedStream)) { // use the node's own codec or we'll mess up the string lists Decoded.Codec.EncodeRootNode(w, Decoded); data = uncompressedStream.ToArray(); } uint uncompressedSize = (uint)data.LongLength; // compress the encoded data #if DEBUG Console.WriteLine("compressing..."); #endif MemoryStream outStream = new MemoryStream(); LzmaEncoder encoder = new LzmaEncoder(); using (uncompressedStream = new MemoryStream(data)) { encoder.Code(uncompressedStream, outStream, data.Length, long.MaxValue, null); data = outStream.ToArray(); } #if DEBUG Console.WriteLine("ok, compression done"); #endif // prepare decoding information List <EsfNode> infoItems = new List <EsfNode>(); infoItems.Add(new UIntNode { Value = uncompressedSize, TypeCode = EsfType.UINT32, Codec = Codec }); using (MemoryStream propertyStream = new MemoryStream()) { encoder.WriteCoderProperties(propertyStream); infoItems.Add(new RawDataNode(Codec) { Value = propertyStream.ToArray() }); } // put together the items expected by the unzipper List <EsfNode> dataItems = new List <EsfNode>(); dataItems.Add(new RawDataNode(Codec) { Value = data }); dataItems.Add(new RecordNode(Codec) { Name = CompressedNode.INFO_TAG, Value = infoItems }); RecordNode compressedNode = new RecordNode(Codec) { Name = CompressedNode.TAG_NAME, Value = dataItems }; // and finally encode compressedNode.Encode(writer); }
public static byte[] Compress(byte[] buffer) { using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms)) { byte[] crc = CryptoHelper.CRCHash(buffer); writer.Write(VZipHeader); writer.Write((byte)Version); writer.Write(crc); Int32 dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; Int32 litPosBits = 0; Int32 algorithm = 2; Int32 numFastBytes = 128; SevenZip.CoderPropID[] propIDs = { SevenZip.CoderPropID.DictionarySize, SevenZip.CoderPropID.PosStateBits, SevenZip.CoderPropID.LitContextBits, SevenZip.CoderPropID.LitPosBits, SevenZip.CoderPropID.Algorithm, SevenZip.CoderPropID.NumFastBytes, SevenZip.CoderPropID.MatchFinder, SevenZip.CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), "bt4", false }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(ms); using(MemoryStream input = new MemoryStream(buffer)) { encoder.Code(input, ms, -1, -1, null); } writer.Write(crc); writer.Write((uint)buffer.Length); writer.Write(VZipFooter); return ms.ToArray(); } }
public static byte[] Compress(byte[] buffer) { using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms)) { byte[] crc = CryptoHelper.CRCHash(buffer); writer.Write(VZipHeader); writer.Write((byte)Version); writer.Write(crc); Int32 dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; Int32 litPosBits = 0; Int32 algorithm = 2; Int32 numFastBytes = 128; SevenZip.CoderPropID[] propIDs = { SevenZip.CoderPropID.DictionarySize, SevenZip.CoderPropID.PosStateBits, SevenZip.CoderPropID.LitContextBits, SevenZip.CoderPropID.LitPosBits, SevenZip.CoderPropID.Algorithm, SevenZip.CoderPropID.NumFastBytes, SevenZip.CoderPropID.MatchFinder, SevenZip.CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), "bt4", false }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(ms); using (MemoryStream input = new MemoryStream(buffer)) { encoder.Code(input, ms, -1, -1, null); } writer.Write(crc); writer.Write((uint)buffer.Length); writer.Write(VZipFooter); return(ms.ToArray()); } }
internal static void CompressCR(string file, string outputlocation) { File.Copy(file, file += ".clone"); byte[] hash; using (var md5 = MD5.Create()) { hash = md5.ComputeHash(File.ReadAllBytes(file)); } var encoder = new Encoder(); using (var input = new FileStream(file, FileMode.Open)) { using (var output = new FileStream(outputlocation, FileMode.Create, FileAccess.Write)) { CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { Dictionary, PosStateBits, LitContextBits, LitPosBits, Algorithm, NumFastBytes, Mf, Eos }; output.Write(Encoding.UTF8.GetBytes("SC"), 0, 2); output.Write(BitConverter.GetBytes(1).Reverse().ToArray(), 0, 4); output.Write(BitConverter.GetBytes(hash.Length).Reverse().ToArray(), 0, 4); output.Write(hash, 0, hash.Length); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(output); output.Write(BitConverter.GetBytes(input.Length).Reverse().ToArray(), 0, 4); encoder.Code(input, output, input.Length, -1, null); output.Flush(); output.Dispose(); } input.Dispose(); } File.Delete(file); }
private void SerializeLZMA(Stream streamOut, bool bSerializeExamples) { CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; Int32 dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; string mf = "bt4"; bool eos = false; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), mf, eos }; MemoryStream msTemp = new MemoryStream(); BinaryWriter binWrtTemp = new BinaryWriter(msTemp); this.Serialize(binWrtTemp, bSerializeExamples); msTemp.Position = 0; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(streamOut); Int64 fileSize = msTemp.Length; for (int i = 0; i < 8; i++) { streamOut.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(msTemp, streamOut, -1, -1, null); binWrtTemp.Close(); msTemp.Close(); }
private static byte[] compress(byte[] decompressed) { byte[] retVal = null; bool eos = true; Int32 dictionary = 1 << 16; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 64; string mf = "bt4"; var propIDs = new CoderPropID[] { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; var properties = new object[] { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); using (Stream strmInStream = new MemoryStream(decompressed)) { strmInStream.Seek(0, 0); using (MemoryStream strmOutStream = new MemoryStream()) { encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(strmOutStream); Int64 fileSize = strmInStream.Length; for (int i = 0; i < 8; i++) { strmOutStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(strmInStream, strmOutStream, -1, -1, null); retVal = strmOutStream.ToArray(); } } return(retVal); }
public static void CompressMemory(MemoryStream input, out MemoryStream output) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); output = new MemoryStream(); coder.WriteCoderProperties(output); output.Write(System.BitConverter.GetBytes((int)input.Length), 0, 4); coder.Code(input, output, input.Length, -1, null); output.Flush(); input.Close(); }
public static byte[] Compress(byte[] inputBytes) { MemoryStream inStream = new MemoryStream(inputBytes); MemoryStream outStream = new MemoryStream(); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); encoder.Code(inStream, outStream, inStream.Length, -1, null); return outStream.ToArray(); }
public static MemoryStream LZMACompress(Stream input) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); MemoryStream output = new MemoryStream(); coder.WriteCoderProperties(output); output.Write(BitConverter.GetBytes(input.Length), 0, 8); coder.Code(input, output, input.Length, -1, null); output.Flush(); return(output); }
public static void LZMAEncode(Stream inStream, Stream outStream) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); // Write the encoder properties coder.WriteCoderProperties(outStream); // Write the decompressed file size. outStream.Write(BitConverter.GetBytes(inStream.Length), 0, 8); // Encode the file. coder.Code(inStream, outStream, inStream.Length, -1, null); outStream.Flush(); }
public static byte[] Compress(byte[] inputBytes) { MemoryStream inStream = new MemoryStream(inputBytes); MemoryStream outStream = new MemoryStream(); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); encoder.Code(inStream, outStream, inStream.Length, -1, null); return(outStream.ToArray()); }
public static byte[] Compress(byte[] inputBytes) { MemoryStream inStream = new MemoryStream(inputBytes); MemoryStream outStream = new MemoryStream(); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); /*long fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i)));*/ encoder.Code(inStream, outStream, inStream.Length, -1, null); return outStream.ToArray(); }
public static void CompressFileLZMA (string inFile, string outFile) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder (); FileStream input = new FileStream (inFile, FileMode.Open); FileStream output = new FileStream (outFile, FileMode.Create); // Write the encoder properties coder.WriteCoderProperties (output); // Write the decompressed file size. output.Write (BitConverter.GetBytes (input.Length), 0, 8); // Encode the file. coder.Code (input, output, input.Length, -1, null); output.Flush (); output.Close (); input.Close (); }
public void Compress(Stream inStream, Stream outStream, bool closeInStream) { inStream.Position = 0; Int64 fileSize = inStream.Length; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); if (BitConverter.IsLittleEndian) { byte[] LengthHeader = BitConverter.GetBytes(fileSize); outStream.Write(LengthHeader, 0, LengthHeader.Length); } encoder.Code(inStream, outStream, -1, -1, null); if (closeInStream) inStream.Close(); }
public static void Compress(Stream inStream, Stream outStream, IProgress<ProgressReport> progress) { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(1 << 26), (Int32)(1), (Int32)(8), (Int32)(0), (Int32)(2), (Int32)(96), "bt4", false }; encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 inSize = inStream.Length; for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(inSize >> (8 * i))); } var lzmaProgress = new LzmaProgress(progress, "Compressing", inSize, MeasureBy.Input); lzmaProgress.SetProgress(0, 0); encoder.Code(inStream, outStream, -1, -1, lzmaProgress); lzmaProgress.SetProgress(inSize, outStream.Length); }
public static uint CompressStrLZMA (byte[] inBytes, uint inLen, ref byte[] outBytes, ref uint outLen) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder (); MemoryStream inStream = new MemoryStream (inBytes); int saveinsize = (int)inLen; int saveoutsize = (int)(saveinsize * 1.1 + 1026 * 16 + LZMA_HEADER_LEN); outBytes = new byte[saveoutsize]; MemoryStream outStream = new MemoryStream (outBytes); // Write the encoder properties coder.WriteCoderProperties (outStream); // Write the decompressed file size. outStream.Write (BitConverter.GetBytes (inStream.Length), 0, 8); // Encode the file. coder.Code (inStream, outStream, inStream.Length, saveoutsize, null); outStream.Flush (); outStream.Close (); inStream.Close (); return (uint)saveoutsize; }
public static byte[] lze(string plain, bool fjdsuioafuweabnfowa) { byte[] bytes = Encoding.UTF8.GetBytes(plain); MemoryStream msi = new MemoryStream(bytes); MemoryStream mso = new MemoryStream(); SevenZip.Compression.LZMA.Encoder enc = new SevenZip.Compression.LZMA.Encoder(); enc.WriteCoderProperties(mso); mso.Write(BitConverter.GetBytes(msi.Length), 0, 8); enc.Code(msi, mso, msi.Length, -1, null); return mso.ToArray(); }
private void button4_Click(object sender, EventArgs e) { button1.Enabled = false; button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; DevFileLocation fl = new DevFileLocation(textBox1.Text); ContentBinaryWriter bw = new ContentBinaryWriter(fl); bw.Write(LpkArchive.FileId); int count = listView1.Items.Count; bw.Write(count); progressBar1.Value = 0; progressBar1.Maximum = count; int oldPos = (int)bw.BaseStream.Position; ListView.ListViewItemCollection coll = listView1.Items; LpkArchiveEntry[] entries = new LpkArchiveEntry[count]; for (int i = 0; i < count; i++) { entries[i].Name = Path.GetFileName(coll[i].Text); bw.WriteStringUnicode(entries[i].Name); bw.Write(entries[i].CompressedSize); bw.Write(entries[i].Offset); bw.Write(entries[i].Size); bw.Write(entries[i].Flag); } #region 打包文件 for (int i = 0; i < count; i++) { FileStream fs = new FileStream(coll[i].Text, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); entries[i].Offset = (int)bw.BaseStream.Position; entries[i].Size = (int)br.BaseStream.Length; entries[i].CompressedSize = entries[i].Size; entries[i].Flag = 0; bw.Write(br.ReadBytes(entries[i].Size)); br.Close(); fs.Close(); Application.DoEvents(); progressBar1.Value = i + 1; } CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.Algorithm }; object[] properties = { 1048576*8, 2 }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); System.IO.MemoryStream propms = new System.IO.MemoryStream(); encoder.WriteCoderProperties(propms); bw.Write((int)propms.Length); propms.Close(); bw.Write(propms.ToArray()); for (int i = 0; i < count; i++) { entries[i].Offset = (int)bw.BaseStream.Position; FileStream fs = new FileStream(coll[i].Text, FileMode.Open, FileAccess.Read); System.IO.MemoryStream ms = new System.IO.MemoryStream((int)fs.Length / 2); encoder.Code(fs, ms, -1, -1, null); entries[i].Size = (int)fs.Length; fs.Close(); ms.Close(); byte[] buffer = ms.ToArray(); bw.Write(buffer); entries[i].CompressedSize = buffer.Length; entries[i].Flag = 0; Application.DoEvents(); progressBar1.Value = i + 1; } #endregion bw.Seek(oldPos, SeekOrigin.Begin); for (int i = 0; i < count; i++) { bw.WriteStringUnicode(entries[i].Name); bw.Write(entries[i].CompressedSize); bw.Write(entries[i].Offset); bw.Write(entries[i].Size); bw.Write(entries[i].Flag); } bw.Close(); button1.Enabled = true; button2.Enabled = true; button3.Enabled = true; button4.Enabled = true; }
internal List<MultipartMessage> PrepareMultipartMessages(ref byte[] data) { List<MultipartMessage> result = new List<MultipartMessage>(); // prepare streams MemoryStream inData = new MemoryStream(data); inData.Position = 0; MemoryStream outData = new MemoryStream(); // compress the data Encoder compressor = new Encoder(); compressor.WriteCoderProperties(outData); outData.Write(BitConverter.GetBytes(inData.Length), 0, 8); compressor.Code(inData, outData, inData.Length, -1, null); outData.Position = 0; // close the in stream as it is no longer needed inData.Close(); // assemble multipart messages int maxMessageSize = _MaxMessageSize - MultipartMessageHeaderLength; ushort partCount = Convert.ToUInt16(Math.Ceiling((double)outData.Length / (double)maxMessageSize)); uint messageId = GetNextMultipartMessageID(); _WaitingMultipartId = messageId; for (ushort partIndex = 0; partIndex < partCount; partIndex++) { int currentOffset = partIndex * maxMessageSize; int bytesLeft = (int)outData.Length - currentOffset; int currentSize = (bytesLeft > maxMessageSize ? maxMessageSize : bytesLeft); byte[] partData = new byte[currentSize]; outData.Read(partData, 0, currentSize); result.Add(new MultipartMessage() { Data = partData, MessageID = messageId, PartCount = partCount, PartIndex = partIndex }); } // close the out data stream outData.Close(); // return messages return result; }
public byte[] Compress( byte[] buffer, int numFastBytes = 64, int litContextBits = 3, int litPosBits = 0, int posStateBits = 2, int blockSize = 0, int matchFinderCycles = 32 ) { MemoryStream result = new MemoryStream(); int inSize = buffer.Length; int streamCount = ( inSize + 0xffff ) >> 16; int offset = 0; BinaryWriter bw = new BinaryWriter( result ); bw.Write( new byte[] { 0x54, 0x4C, 0x5A, 0x43 } ); bw.Write( (byte)0x01 ); bw.Write( (byte)0x04 ); 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 // next comes the coder properties (5 bytes), followed by stream lengths, followed by the streams themselves. var encoder = new SevenZip.Compression.LZMA.Encoder(); var props = new Dictionary<SevenZip.CoderPropID, object>(); props[SevenZip.CoderPropID.DictionarySize] = 0x10000; props[SevenZip.CoderPropID.MatchFinder] = "BT4"; props[SevenZip.CoderPropID.NumFastBytes] = numFastBytes; props[SevenZip.CoderPropID.LitContextBits] = litContextBits; props[SevenZip.CoderPropID.LitPosBits] = litPosBits; props[SevenZip.CoderPropID.PosStateBits] = posStateBits; //props[SevenZip.CoderPropID.BlockSize] = blockSize; // this always throws an exception when set //props[SevenZip.CoderPropID.MatchFinderCycles] = matchFinderCycles; // ^ same here //props[SevenZip.CoderPropID.DefaultProp] = 0; //props[SevenZip.CoderPropID.UsedMemorySize] = 100000; //props[SevenZip.CoderPropID.Order] = 1; //props[SevenZip.CoderPropID.NumPasses] = 10; //props[SevenZip.CoderPropID.Algorithm] = 0; //props[SevenZip.CoderPropID.NumThreads] = ; //props[SevenZip.CoderPropID.EndMarker] = ; encoder.SetCoderProperties( props.Keys.ToArray(), props.Values.ToArray() ); encoder.WriteCoderProperties( result ); // reserve space for the stream lengths. we'll fill them in later after we know what they are. bw.Write( new byte[streamCount * 2] ); List<int> streamSizes = new List<int>(); for ( int i = 0; i < streamCount; i++ ) { long preLength = result.Length; encoder.Code( new MemoryStream( buffer, offset, Math.Min( inSize, 0x10000 ) ), result, Math.Min( inSize, 0x10000 ), -1, null ); int streamSize = (int)( result.Length - preLength ); if ( streamSize >= 0x10000 ) { System.Diagnostics.Trace.WriteLine( "Warning! stream did not compress at all. This will cause a different code path to be executed on the PS3 whose operation is assumed and not tested!" ); result.Position = preLength; result.SetLength( preLength ); result.Write( buffer, offset, Math.Min( inSize, 0x10000 ) ); streamSize = 0; } inSize -= 0x10000; offset += 0x10000; streamSizes.Add( streamSize ); } // fill in compressed size result.Position = 8; bw.Write( (int)result.Length ); byte[] temp = result.ToArray(); // fill in stream sizes for ( int i = 0; i < streamSizes.Count; i++ ) { temp[5 + 0x18 + i * 2] = (byte)streamSizes[i]; temp[6 + 0x18 + i * 2] = (byte)( streamSizes[i] >> 8 ); } return temp; }
void makeDFC() { archivequeue = new List<string>(); string src = Path.GetFullPath(@"..\..\tools\"); recurse(src, archivequeue); string[] files = archivequeue.ToArray(); for (int a = 0; a < files.Length; a++) { files[a] = files[a].Substring(src.Length).Replace('\\', '/'); } int filenameslength = 0; long[] cLen = new long[files.Length]; long[] eLen = new long[files.Length]; byte[][] filename = new byte[files.Length][]; for (int a = 0; a < files.Length; a++) { eLen[a] = new FileInfo(src + files[a]).Length; filename[a] = Encoding.UTF8.GetBytes(files[a]); filenameslength += filename[a].Length; bytesToCompress += eLen[a]; } byte[] buffer = new byte[8192]; byte[] header = new byte[ 4 + // header length 4 + // file count files.Length * 4 + // filename length files.Length * 8 + // compressed length files.Length * 8 + // extracted length filenameslength ]; for (int a = 0; a < header.Length; a++) { header[a] = 0xFF; } using (FileStream fso = new FileStream(@"..\..\res\tools.dfc", FileMode.Create)) { encoding = true; fso.Write(header, 0, header.Length); for (int a = 0; a < files.Length; a++) { using (FileStream fsi = new FileStream(src + files[a], FileMode.Open, FileAccess.Read)) { long pre = fso.Position; SevenZip.Compression.LZMA.Encoder enc = new SevenZip.Compression.LZMA.Encoder(); enc.WriteCoderProperties(fso); fso.Write(BitConverter.GetBytes(fsi.Length), 0, 8); enc.Code(fsi, fso, fsi.Length, -1, icp); cLen[a] = fso.Position - pre; bytesCompressed += icp.i; icp = new ICP(); } } using (MemoryStream msh = new MemoryStream(header.Length)) { msh.Write(BitConverter.GetBytes((Int32)header.Length), 0, 4); msh.Write(BitConverter.GetBytes((Int32)files.Length), 0, 4); foreach (byte[] fn in filename) { msh.Write(BitConverter.GetBytes((Int32)fn.Length), 0, 4); } foreach (long cLn in cLen) { msh.Write(BitConverter.GetBytes((Int64)cLn), 0, 8); } foreach (long eLn in eLen) { msh.Write(BitConverter.GetBytes((Int64)eLn), 0, 8); } foreach (byte[] fn in filename) { msh.Write(fn, 0, fn.Length); } fso.Seek(0, SeekOrigin.Begin); msh.Seek(0, SeekOrigin.Begin); msh.WriteTo(fso); } } encoding = false; }
public override void Process(NameValueCollection parameters, MetadataProcessor.MetadataAccessor accessor) { _Context txt = rc.txts[accessor.Module]; ModuleDefinition mod = accessor.Module; for (int i = 0; i < mod.Resources.Count; i++) if (mod.Resources[i] is EmbeddedResource) { txt.dats.Add(new KeyValuePair<string, byte[]>(mod.Resources[i].Name, (mod.Resources[i] as EmbeddedResource).GetResourceData())); mod.Resources.RemoveAt(i); i--; } if (txt.dats.Count > 0) { MemoryStream ms = new MemoryStream(); BinaryWriter wtr = new BinaryWriter(ms); byte[] dat = GetAsm(mod.TimeStamp, txt.dats); wtr.Write(dat.Length); wtr.Write(dat); ms.Position = 0; int dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; string mf = "bt4"; SevenZip.CoderPropID[] propIDs = { SevenZip.CoderPropID.DictionarySize, SevenZip.CoderPropID.PosStateBits, SevenZip.CoderPropID.LitContextBits, SevenZip.CoderPropID.LitPosBits, SevenZip.CoderPropID.Algorithm, SevenZip.CoderPropID.NumFastBytes, SevenZip.CoderPropID.MatchFinder, SevenZip.CoderPropID.EndMarker }; object[] properties = { (int)dictionary, (int)posStateBits, (int)litContextBits, (int)litPosBits, (int)algorithm, (int)numFastBytes, mf, false }; MemoryStream x = new MemoryStream(); var encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(x); Int64 fileSize; fileSize = ms.Length; for (int i = 0; i < 8; i++) x.WriteByte((Byte)(fileSize >> (8 * i))); ms.Position = 0; encoder.Code(ms, x, -1, -1, null); dat = Transform(x.ToArray(), txt.key0, txt.key1); mod.Resources.Add(new EmbeddedResource(txt.resId, ManifestResourceAttributes.Private, dat)); } }
public static Stream CompressAsStream(Stream value) { Contract.Requires(null != value, "value"); value.Position = 0; // Define params int dictionary = 1 << 23; // Dictionary - [0, 29], default: 23 (8MB) int posStateBits = 2; // Number of pos bits - [0, 4], default: 2 int litContextBits = 3; // Number of literal context bits - [0, 8], default: 3 int litPosBits = 0;// Number of literal pos bits - [0, 4], default: 0 int algorithm = 2; // Mode int numFastBytes = 128;// Number of fast bytes - [5, 273], default: 128 var eos = false; // Write End Of Stream marker? var mf = "bt4";// Match Finder: [bt2, bt4], default: bt4 CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (int)dictionary, (int)posStateBits, (int)litContextBits, (int)litPosBits, (int)algorithm, (int)numFastBytes, mf, eos }; long fileSize = eos ? -1 : value.Length; // Start output stream var outStream = new MemoryStream(); try { // Prepare encoder var encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); // Write header to output stream for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } // Encode encoder.Code(value, outStream, -1, -1, null); // Convert output to string return outStream; } finally { outStream.Dispose(); } }
private void CompressArchive() { if (archiveFileName2 == null) { archiveFileName2 = Path.Combine(Path.GetTempPath(), "log-" + DateTime.UtcNow.ToString("yyyyMMdd-HHmmss") + ".tar.lzma"); SharedData.Instance.ArchiveFileName = archiveFileName2; SharedData.Instance.TempFiles.Add(archiveFileName2); } // Compress the tar file with LZMA // Source: http://stackoverflow.com/a/8605828/143684 SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); using (lzmaInput = new FileStream(archiveFileName1, FileMode.Open)) using (lzmaOutput = new FileStream(archiveFileName2, FileMode.Create)) // Creates new or truncates existing { SharedData.Instance.OpenDisposables.Add(lzmaInput); SharedData.Instance.OpenDisposables.Add(lzmaOutput); coder.WriteCoderProperties(lzmaOutput); // Write the decompressed file size lzmaOutput.Write(BitConverter.GetBytes(lzmaInput.Length), 0, 8); coder.Code(lzmaInput, lzmaOutput, lzmaInput.Length, -1, this); } SharedData.Instance.OpenDisposables.Remove(lzmaInput); SharedData.Instance.OpenDisposables.Remove(lzmaOutput); // Clean up the uncompressed tar file, we only keep the name of the compressed lzma file // from now on to delete it later. File.Delete(archiveFileName1); SharedData.Instance.TempFiles.Remove(archiveFileName1); archiveFileName1 = null; }
public override void DeInitialize() { _Context txt = cc.txts[mod]; byte[] final; MemoryStream str = new MemoryStream(); using (BinaryWriter wtr = new BinaryWriter(str)) foreach (byte[] dat in txt.dats) wtr.Write(dat); byte[] buff = XorCrypt(str.ToArray(), txt.key); if (txt.isDyn || txt.isNative) { byte[] e = Encrypt(buff, txt.exp); int dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; string mf = "bt4"; SevenZip.CoderPropID[] propIDs = { SevenZip.CoderPropID.DictionarySize, SevenZip.CoderPropID.PosStateBits, SevenZip.CoderPropID.LitContextBits, SevenZip.CoderPropID.LitPosBits, SevenZip.CoderPropID.Algorithm, SevenZip.CoderPropID.NumFastBytes, SevenZip.CoderPropID.MatchFinder, SevenZip.CoderPropID.EndMarker }; object[] properties = { (int)dictionary, (int)posStateBits, (int)litContextBits, (int)litPosBits, (int)algorithm, (int)numFastBytes, mf, false }; MemoryStream x = new MemoryStream(); var encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(x); Int64 fileSize; MemoryStream output = new MemoryStream(); fileSize = e.Length; for (int i = 0; i < 8; i++) x.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(new MemoryStream(e), x, -1, -1, null); using (var s = new CryptoStream(output, new RijndaelManaged().CreateEncryptor(txt.keyBuff, MD5.Create().ComputeHash(txt.keyBuff)) , CryptoStreamMode.Write)) s.Write(x.ToArray(), 0, (int)x.Length); final = output.ToArray(); } else { int dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; string mf = "bt4"; SevenZip.CoderPropID[] propIDs = { SevenZip.CoderPropID.DictionarySize, SevenZip.CoderPropID.PosStateBits, SevenZip.CoderPropID.LitContextBits, SevenZip.CoderPropID.LitPosBits, SevenZip.CoderPropID.Algorithm, SevenZip.CoderPropID.NumFastBytes, SevenZip.CoderPropID.MatchFinder, SevenZip.CoderPropID.EndMarker }; object[] properties = { (int)dictionary, (int)posStateBits, (int)litContextBits, (int)litPosBits, (int)algorithm, (int)numFastBytes, mf, false }; MemoryStream x = new MemoryStream(); var encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(x); Int64 fileSize; fileSize = buff.Length; for (int i = 0; i < 8; i++) x.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(new MemoryStream(buff), x, -1, -1, null); MemoryStream output = new MemoryStream(); using (var s = new CryptoStream(output, new RijndaelManaged().CreateEncryptor(txt.keyBuff, MD5.Create().ComputeHash(txt.keyBuff)) , CryptoStreamMode.Write)) s.Write(x.ToArray(), 0, (int)x.Length); final = EncryptSafe(output.ToArray(), BitConverter.ToUInt32(txt.keyBuff, 0xc) * (uint)txt.resKey); } mod.Resources.Add(new EmbeddedResource(txt.resId, ManifestResourceAttributes.Private, final)); }
public static byte[] Encode(string s) { byte[] result; byte[] str = new byte[s.Length]; int v = 0; foreach (var item in s) { str[v] = (byte)item; v++; } MemoryStream ins = new MemoryStream(str); MemoryStream outs = new MemoryStream(); SevenZip.Compression.LZMA.Encoder lzma = new SevenZip.Compression.LZMA.Encoder(); lzma.WriteCoderProperties(outs); outs.Write(BitConverter.GetBytes(ins.Length), 0, 8); lzma.Code(ins, outs, -1, -1, null); result = outs.ToArray(); //outs.Flush(); outs.Close(); outs.Dispose(); ins.Close(); ins.Dispose(); return result; }
private void SerializeLZMA(Stream streamOut, bool bSerializeExamples) { CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; Int32 dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; string mf = "bt4"; bool eos = false; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), mf, eos }; MemoryStream msTemp = new MemoryStream(); BinaryWriter binWrtTemp = new BinaryWriter(msTemp); this.Serialize(binWrtTemp, bSerializeExamples); msTemp.Position = 0; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(streamOut); Int64 fileSize = msTemp.Length; for (int i = 0; i < 8; i++) streamOut.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(msTemp, streamOut, -1, -1, null); binWrtTemp.Close(); msTemp.Close(); }
private void SendCompressedMessage(ref byte[] uncompressed) { if (TcpConnection == null) { Console.WriteLine("- TCP connection closed, can't send large message"); return; } // prepare streams MemoryStream inData = new MemoryStream(uncompressed); inData.Position = 0; MemoryStream outData = new MemoryStream(); // compress the data Encoder compressor = new Encoder(); compressor.WriteCoderProperties(outData); outData.Write(BitConverter.GetBytes(inData.Length), 0, 8); compressor.Code(inData, outData, inData.Length, -1, null); outData.Position = 0; Console.WriteLine("< !COMPRESSED-TCP! {0} -> {1} bytes", inData.Length, outData.Length); // close the in stream as it is no longer needed inData.Close(); if (TcpConnection != null) { TcpConnection.SendObject(TcpByteMessageName, outData.ToArray()); } outData.Close(); }