public static System.IO.Stream DeCompress(System.IO.Stream stream, uint length, bool EOF = false) { SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); int tagsize = 5; int filelen = -1;//如果压缩时使用了结束标志选项,则这里传-1,解压缩会知道文件长度 if (!EOF) { //读取文件长度 byte[] lbuf = new byte[4]; stream.Read(lbuf, 0, 4); filelen = (int)BitConverter.ToUInt32(lbuf, 0); tagsize += 4; } //读取压缩属性 byte[] properties = new byte[5]; if (stream.Read(properties, 0, 5) != 5) throw (new Exception("input .lzma is too short")); decoder.SetDecoderProperties(properties); System.IO.MemoryStream sout = new System.IO.MemoryStream(); decoder.Code(stream, sout, length - tagsize, filelen, null); sout.Position = 0; return sout; }
private static string DecompressFromFile(string filename) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read); MemoryStream output = new MemoryStream(); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte [] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); string ret = System.Text.Encoding.UTF8.GetString(output.ToArray()); output.Close(); input.Close(); return(ret); }
public static MemoryStream Decompress(FileStream inStream) { var decoder = new Decoder(); var properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) throw (new Exception("input .lzma is too short")); decoder.SetDecoderProperties(properties); long outSize = 0; for (var i = 0; i < 8; i++) { var v = inStream.ReadByte(); if (v < 0) break; outSize |= ((long) (byte) v) << (8 * i); } var compressedSize = inStream.Length - inStream.Position; var outStream = new MemoryStream(); decoder.Code(inStream, outStream, compressedSize, outSize, null); outStream.Flush(); outStream.Position = 0; return outStream; }
public static void DecompressFileLZMA(string inFile, string outFile) { Debug.Log("DecompressFileLZMA in File is :" + inFile); Debug.Log("DecompressFileLZMA in outFile is :" + outFile); SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); FileStream input = new FileStream(inFile, FileMode.Open, FileAccess.Read); string path = outFile.Substring(0, outFile.LastIndexOf("/")); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } FileStream output = new FileStream(outFile, FileMode.Create); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte [] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); output.Close(); input.Close(); }
/// <summary> /// Decompresses the specified <see cref="byte"/> into /// LZMA. /// </summary> /// <param name="bytes">The <see cref="byte"/> array to decompress.</param> /// <returns>The decompressed <see cref="byte"/> array.</returns> public static byte[] Decompress(byte[] bytes) { if (bytes == null) throw new ArgumentNullException("bytes"); using (var outStream = new MemoryStream()) { using (var inStream = new MemoryStream(bytes)) { var decoder = new Decoder(); inStream.Seek(0, 0); var properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) throw new Exception("Input .lzma is too short"); var outSize = 0L; for (int i = 0; i < 8; i++) { var v = inStream.ReadByte(); if (v < 0) throw new Exception("Can't Read 1"); outSize |= ((long)v) << (8 * i); } var compressedSize = inStream.Length - inStream.Position; decoder.SetDecoderProperties(properties); decoder.Code(inStream, outStream, compressedSize, outSize, null); return outStream.ToArray(); } } }
public static string Decompressed(string text) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); byte[] array = System.Convert.FromBase64String(text); MemoryStream input = new MemoryStream(array); MemoryStream output = new MemoryStream(); byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte [] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); string ret = System.Text.Encoding.UTF8.GetString(output.ToArray()); output.Close(); input.Close(); return(ret); }
public static void DecompressBytesLZMA(byte[] bytes, string outFile) { if (File.Exists(outFile)) { File.Delete(outFile); } SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); using (MemoryStream input = new MemoryStream(bytes, 0, bytes.Length)) { using (FileStream output = new FileStream(outFile, FileMode.Create)) { // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); //output.Close(); } //input.Close(); } }
public static byte[] DecompressBuffer(byte[] inbuffer) { MemoryStream input = new MemoryStream(inbuffer); MemoryStream output = new MemoryStream(); input.Position = 0; byte[] fileLengthBytes = new byte[4]; input.Read(fileLengthBytes, 0, 4); int fileLength = BitConverter.ToInt32(fileLengthBytes, 0); byte[] properties = new byte[5]; input.Read(properties, 0, 5); try { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Position = 0; byte[] bytes = new byte[output.Length]; output.Read(bytes, 0, bytes.Length); return(bytes); } catch (System.Exception ex) { //Debugger.LogError(ex.Message); return(null); } }
public static byte[] Decompress(byte[] inputBytes) { MemoryStream newInStream = new MemoryStream(inputBytes); SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); newInStream.Seek(0, 0); MemoryStream newOutStream = new MemoryStream(); byte[] properties2 = new byte[5]; if (newInStream.Read(properties2, 0, 5) != 5) throw (new Exception("input .lzma is too short")); long outSize = 0; for (int i = 0; i < 8; i++) { int v = newInStream.ReadByte(); if (v < 0) throw (new Exception("Can't Read 1")); outSize |= ((long)(byte)v) << (8 * i); } decoder.SetDecoderProperties(properties2); long compressedSize = newInStream.Length - newInStream.Position; decoder.Code(newInStream, newOutStream, compressedSize, outSize, null); byte[] b = newOutStream.ToArray(); return b; }
private static void DecompressFileLZMA(string inFile, string outFile) { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); output.Close(); input.Flush(); input.Close(); stopwatch.Stop(); Debug.LogFormat("7z encode time: {0}", stopwatch.Elapsed.TotalSeconds); }
//decompresses the given byte array public static byte[] Decompress(byte[] combytes) { if (combytes == null) { return(null); } //create the decoder object SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); //create the memory streams; MemoryStream input = new MemoryStream(combytes); MemoryStream output = new MemoryStream(); //set the properties of teh decoder byte[] properties = new byte[5]; input.Read(properties, 0, 5); coder.SetDecoderProperties(properties); //read the length of the original file byte[] filelendata = new byte[8]; input.Read(filelendata, 0, 8); long filelen = System.BitConverter.ToInt64(filelendata, 0); //decode the byte array coder.Code(input, output, input.Length, filelen, null); //return the decompressed array return(output.GetBuffer()); }
public static void Decompress(Stream src, Stream dst) { byte[] decode_properties = new byte[5]; int n = src.Read(decode_properties, 0, 5); if (n != 5) { Console.WriteLine("read encode_properties error."); return; } SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); decoder.SetDecoderProperties(decode_properties); long outSize = 0; for (int i = 0; i < 8; i++) { int v = src.ReadByte(); if (v < 0) { Console.WriteLine("read outSize error."); return; } outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = src.Length - src.Position; decoder.Code(src, dst, compressedSize, outSize, null); }
public static System.IO.Stream DeCompress(System.IO.Stream stream, uint length, bool EOF = false) { SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); int tagsize = 5; int filelen = -1;//如果压缩时使用了结束标志选项,则这里传-1,解压缩会知道文件长度 if (!EOF) { //读取文件长度 byte[] lbuf = new byte[4]; stream.Read(lbuf, 0, 4); filelen = (int)BitConverter.ToUInt32(lbuf, 0); tagsize += 4; } //读取压缩属性 byte[] properties = new byte[5]; if (stream.Read(properties, 0, 5) != 5) { throw (new Exception("input .lzma is too short")); } decoder.SetDecoderProperties(properties); System.IO.MemoryStream sout = new System.IO.MemoryStream(); decoder.Code(stream, sout, length - tagsize, filelen, null); sout.Position = 0; return(sout); }
public static byte[] Decompress(byte[] src) { if (src == null) { return(null); } SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); MemoryStream input = new MemoryStream(src); MemoryStream output = new MemoryStream(); byte[] properties = new byte[5]; input.Read(properties, 0, 5); byte[] bytes = new byte[8]; input.Read(bytes, 0, 8); long size = BitConverter.ToInt64(bytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, size, null); output.Flush(); input.Flush(); bytes = output.ToArray(); output.Close(); input.Close(); return(bytes); }
public static byte[] Decompress(this byte[] buffer) { if (buffer.Length > 17 && buffer.IsCompressed()) { MemoryStream inputStream = new MemoryStream(buffer); int lzmaID = inputStream.ReadInt(); int acutalSize = inputStream.ReadInt(); int lzmaSize = inputStream.ReadInt(); byte[] props = inputStream.ReadBytes(5); int lzmaBufferSize = buffer.Length - 17; if (lzmaBufferSize != lzmaSize) throw new Exception(string.Format("LZMA data corruption. Expected {0} bytes got {1}", lzmaSize, lzmaBufferSize)); byte[] uncompressedBuffer = new byte[acutalSize]; MemoryStream outputStream = new MemoryStream(uncompressedBuffer); try { LZMADecoder = new Decoder(); LZMADecoder.SetDecoderProperties(props); LZMADecoder.Code(inputStream, outputStream, inputStream.Length, outputStream.Length, null); } catch (Exception e) { throw new Exception(e.Message); } return uncompressedBuffer; } else throw new Exception("Buffer is not compressed!"); }
/// <summary> /// real uncompress file /// </summary> /// <param name="inFile"></param> /// <param name="outFile"></param> private static void DecompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); FileStream Inputfs, Outputfs; Inputfs = File.Open(inFile, FileMode.Open); Outputfs = File.Open(outFile, FileMode.Create); { // Read the decoder properties byte[] properties = new byte[5]; Inputfs.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; Inputfs.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(Inputfs, Outputfs, Inputfs.Length, fileLength, null); Outputfs.Flush(); Inputfs.Flush(); Inputfs.Close(); Outputfs.Close(); } }
public void Zip2() { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); FileStream input = new FileStream(Application.dataPath+"/2.zip", FileMode.Open); MemoryStream output = new MemoryStream (); //FileStream output = new FileStream(outFile, FileMode.Create); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte [] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); var b = output.GetBuffer (); var ret = System.Text.Encoding.UTF8.GetString (b); //StreamReader reader = new StreamReader(output); Debug.Log (ret); output.Close(); input.Close(); }
public static void DecompressFileLZMA(string inFile, string outFile) { using (FileStream input = new FileStream(inFile, FileMode.Open)) { using (FileStream output = new FileStream(outFile, FileMode.Create)) { SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); byte[] properties = new byte[5]; if (input.Read(properties, 0, 5) != 5) { throw (new Exception("input .lzma is too short")); } decoder.SetDecoderProperties(properties); long outSize = 0; for (int i = 0; i < 8; i++) { int v = input.ReadByte(); if (v < 0) { throw (new Exception("Can't Read 1")); } outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = input.Length - input.Position; decoder.Code(input, output, compressedSize, outSize, null); } } }
public static byte[] DecompressLzmaStream(Stream input) { SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); byte[] properties = new byte[5]; if (input.Read(properties, 0, 5) != 5) { throw (new Exception("input .lzma is too short")); } decoder.SetDecoderProperties(properties); long outSize = 0; for (int i = 0; i < 8; i++) { int v = input.ReadByte(); if (v < 0) { throw (new Exception("Can't Read 1")); } outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = input.Length - input.Position; byte[] buffer = new byte[outSize]; using (MemoryStream output = new MemoryStream(buffer)) { decoder.Code(input, output, compressedSize, outSize, null); } return(buffer); }
/// <summary> /// 使用LZMA算法解压文件 /// </summary> /// <param name="inFile"></param> /// <param name="outFile"></param> public static void DecompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); using (FileStream input = new FileStream(inFile, FileMode.Open)) { using (FileStream output = new FileStream(outFile, FileMode.Create)) { // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size byte[] fileLenthBytes = new byte[8]; input.Read(fileLenthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLenthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); output.Close(); input.Close(); } } }
private static Stream DecompressInternal(Stream inStream) { var outStream = new MemoryStream(); var decoder = new LZMA.Decoder(); var properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) { throw new InvalidDataException("Stream has an invalid header."); } else { // Make sure we have the 4 bytes representing the uncompressed size. // The Clash of Clans LZMA header's uncompressed size is 4 bytes instead of 8. var outSizeBytes = new byte[4]; if (inStream.Read(outSizeBytes, 0, 4) != 4) { throw new InvalidDataException("Stream has an invalid header."); } var inSize = inStream.Length - inStream.Position; var outSize = BitConverter.ToInt32(outSizeBytes, 0); decoder.SetDecoderProperties(properties); decoder.Code(inStream, outStream, inSize, outSize, null); } outStream.Seek(0, SeekOrigin.Begin); return(outStream); }
private void DecompressFileLZMA(string inFile, string outFile) { coder = new SevenZip.Compression.LZMA.Decoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile.Replace(".zip", ""), FileMode.Create); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); output.Close(); input.Flush(); input.Close(); StartCoroutine(UnPreccFiles()); //DownLoadRes(); }
public static void Decompress(Stream input, Stream output, Action <long, long> onProgress = null) { Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); byte[] properties = new byte[5]; if (input.Read(properties, 0, 5) != 5) { throw new Exception("input .lzma is too short"); } decoder.SetDecoderProperties(properties); long fileLength = 0; for (int i = 0; i < 8; i++) { int v = input.ReadByte(); if (v < 0) { throw new Exception("Can't Read 1"); } fileLength |= ((long)(byte)v) << (8 * i); } ICodeProgress prg = null; if (onProgress != null) { prg = new DelegateCodeProgress(onProgress); } long compressedSize = input.Length - input.Position; decoder.Code(input, output, compressedSize, fileLength, prg); }
/// <summary> /// Decompresses the inputBytes from Lzma /// </summary> /// <param name="inputBytes">Bytes to decompress</param> /// <returns>Decompressed bytes</returns> public static byte[] Decompress(byte[] inputBytes) { if (inputBytes == null) throw new ArgumentNullException("inputBytes"); var decoder = new Decoder(); var newOutStream = new MemoryStream(); var newInStream = new MemoryStream(inputBytes); newInStream.Seek(0, 0); var properties2 = new byte[5]; if (newInStream.Read(properties2, 0, 5) != 5) throw (new Exception("input .lzma is too short")); var outSize = 0L; for (int i = 0; i < 8; i++) { var v = newInStream.ReadByte(); if (v < 0) throw (new Exception("Can't Read 1")); outSize |= ((long)(byte)v) << (8 * i); } var compressedSize = newInStream.Length - newInStream.Position; decoder.SetDecoderProperties(properties2); decoder.Code(newInStream, newOutStream, compressedSize, outSize, null); return newOutStream.ToArray(); }
//解压缩文件 public static void DecompressFileLZMA(byte[] inFile, string outFile, Action callBack) { DGFileUtil.CreateDirectoryWhenNotExists(outFile); SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); MemoryStream input = new MemoryStream(inFile); FileStream output = new FileStream(outFile, FileMode.Create); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte [] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); output.Close(); input.Close(); output.Dispose(); input.Dispose(); if (callBack != null) { callBack(); } }
public static byte[] DecompressBytesLZMA(byte[] bytes) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); byte[] bs = null; using (MemoryStream input = new MemoryStream(bytes, 0, bytes.Length)) { using (MemoryStream output = new MemoryStream()) { // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); bytes = output.GetBuffer(); //output.Close(); } //input.Close(); } return(bytes); }
public static bool DecompressFile(string inPath, string outFile) { bool rsl = true; FileStream output = new FileStream(outFile, FileMode.Create); FileStream input = new FileStream(inPath, FileMode.Open); input.Position = 0; byte[] fileLengthBytes = new byte[4]; input.Read(fileLengthBytes, 0, 4); int fileLength = BitConverter.ToInt32(fileLengthBytes, 0); byte[] properties = new byte[5]; input.Read(properties, 0, 5); try { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); } catch (System.Exception ex) { //Debugger.LogError(ex.Message); rsl = false; } input.Flush(); input.Close(); output.Flush(); output.Close(); return(rsl); }
private static byte[] DecompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); FileStream input = new FileStream(inFile, FileMode.Open); // FileStream output = new FileStream(outFile, FileMode.Create); var output = new MemoryStream(); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); var bytes = output.ToArray(); // output.Flush(); output.Close(); input.Flush(); input.Close(); return(bytes); }
public byte DecodeNormal(Decoder rangeDecoder) { uint symbol = 1; do symbol = (symbol << 1) | m_Decoders[symbol].Decode(rangeDecoder); while (symbol < 0x100); return (byte)symbol; }
public Decode(SevenZip.Compression.LZMA.Decoder decoder, java.io.InputStream inputStream, java.io.OutputStream outputStream, long size) : base() { this.decoder = decoder; this.inputStream = inputStream; this.outputStream = outputStream; this.size = size; }
public static byte[] Decompress(byte[] input, int uncompressedLength) { Contract.Requires(input != null); Contract.Requires(uncompressedLength >= 0); Contract.Ensures(Contract.Result<byte[]>() != null); Contract.Ensures(Contract.Result<byte[]>().Length == uncompressedLength); var decoder = new Decoder(); var result = decoder.Code(input, uncompressedLength, uncompressedLength); Contract.Assume(result.Length == uncompressedLength); return result; }
public static byte[] Decompress(byte[] inBytes, string outPath = "", SevenZip.ICodeProgress progress = null) { byte[] nbytes = null; Stream inStream = new MemoryStream(inBytes); Stream outStream = null; if (string.IsNullOrEmpty(outPath)) { outStream = new MemoryStream(); } else { outStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite); } BinaryReader sr = new BinaryReader(inStream); if (inStream != null && outStream != null) { byte[] properties = new byte[5]; if (sr.Read(properties, 0, 5) != 5) { throw (new Exception("input .lzma is too short")); } //inStream.Seek(5, SeekOrigin.Begin); SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); decoder.SetDecoderProperties(properties); long outSize = 0; for (int i = 0; i < 8; i++) { int v = inStream.ReadByte(); if (v < 0) { throw (new Exception("Can't Read 1")); } outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = inStream.Length - inStream.Position; mLength = compressedSize; decoder.Code(inStream, outStream, compressedSize, outSize, progress); sr.Close(); inStream.Close(); outStream.Position = 0;//Seek(0, SeekOrigin.Begin); sr = new BinaryReader(outStream); nbytes = sr.ReadBytes((int)outStream.Length); sr.Close(); outStream.Close(); } return(nbytes); }
int LzmaDecompressBlock(uint packed_size, uint unpacked_size) { var decoder = new LZMA.Decoder(); var props = m_input.ReadBytes(5); decoder.SetDecoderProperties(props); var buffer = PrepareBuffer(unpacked_size); using (var output = new MemoryStream(buffer)) { decoder.Code(m_input, output, packed_size - 5, unpacked_size, null); return((int)output.Length); } }
public static byte[] Decompress(byte[] inbuf) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); byte[] prop = new byte[5]; Array.Copy(inbuf, prop, 5); coder.SetDecoderProperties(prop); MemoryStream msInp = new MemoryStream(inbuf); msInp.Seek(5, SeekOrigin.Current); MemoryStream msOut = new MemoryStream(); coder.Code(msInp, msOut, -1, -1, null); return(msOut.ToArray()); }
// 解压文件 static public void UnCompressStream(Stream input, Stream output) { // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = System.BitConverter.ToInt64(fileLengthBytes, 0); SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); }
private static byte[] LZMADecode(Stream inStream, int compressedSize, int uncompressedSize) { var decoder = new LZMA.Decoder(); var properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) { throw new ArgumentException("Input data is too short."); } decoder.SetDecoderProperties(properties); using (var outLZMA = new MemoryStream()) { decoder.Code(inStream, outLZMA, compressedSize, uncompressedSize, null); return(outLZMA.ToArray()); } }
public static void DecompressMemory(MemoryStream input, out MemoryStream output) { byte[] properties = new byte[5]; input.Read(properties, 0, 5); byte[] fileLengthBytes = new byte[4]; input.Read(fileLengthBytes, 0, 4); int fileLength = System.BitConverter.ToInt32(fileLengthBytes, 0); output = new MemoryStream(); SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); }
public static byte[] Decompress(byte[] data) { using (MemoryStream instrm = new MemoryStream (data)) using (MemoryStream outstrm = new MemoryStream ()) { Decoder decoder = new Decoder (); byte[] tmp = new byte[5]; instrm.Read (tmp, 0, 5); decoder.SetDecoderProperties (tmp); instrm.Read (tmp, 0, 4); int outsize = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3]; decoder.Code (instrm, outstrm, data.Length - 9, outsize, null); outstrm.Close (); return outstrm.ToArray (); } }
public uint Decode(Decoder rangeDecoder, uint posState) { if (m_Choice.Decode(rangeDecoder) == 0) return m_LowCoder[posState].Decode(rangeDecoder); else { uint symbol = LZMABase.kNumLowLenSymbols; if (m_Choice2.Decode(rangeDecoder) == 0) symbol += m_MidCoder[posState].Decode(rangeDecoder); else { symbol += LZMABase.kNumMidLenSymbols; symbol += m_HighCoder.Decode(rangeDecoder); } return symbol; } }
public static byte[] DecompressFileLZMA(byte[] inBytes) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); var input = new MemoryStream(inBytes); var output = new MemoryStream(); byte[] ret = null; try { // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); ret = output.GetBuffer(); } catch { throw; } finally { output.Close(); output.Dispose(); input.Close(); input.Dispose(); } return ret; }
public byte DecodeWithMatchByte(Decoder rangeDecoder, byte matchByte) { uint symbol = 1; do { uint matchBit = (uint)(matchByte >> 7) & 1; matchByte <<= 1; uint bit = m_Decoders[((1 + matchBit) << 8) + symbol].Decode(rangeDecoder); symbol = (symbol << 1) | bit; if (matchBit != bit) { while (symbol < 0x100) symbol = (symbol << 1) | m_Decoders[symbol].Decode(rangeDecoder); break; } } while (symbol < 0x100); return (byte)symbol; }
public void uncompress(Stream inStream, Stream outStream) { var coder = new lzma.Decoder(); // Read the decoder properties byte[] properties = new byte[5]; inStream.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; inStream.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decompress the file. coder.SetDecoderProperties(properties); coder.Code(inStream, outStream, inStream.Length, fileLength, null); }
public static void DecompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = System.BitConverter.ToInt64(fileLengthBytes, 0); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); output.Close(); }
public static byte[] Decompress(byte[] inputBytes, long decompressedSize) { var compressed = new MemoryStream(inputBytes); var decoder = new Decoder(); var properties2 = new byte[5]; if (compressed.Read(properties2, 0, 5) != 5) { throw (new Exception("input .lzma is too short")); } decoder.SetDecoderProperties(properties2); var compressedSize = compressed.Length - compressed.Position; var decompressed = new MemoryStream(); decoder.Code(compressed, decompressed, compressedSize, decompressedSize, null); if (decompressed.Length != decompressedSize) throw new Exception("Decompression Error"); return decompressed.ToArray(); }
public byte[] readCompressedData(int size) { Decoder decoder = new Decoder (); MemoryStream newOutStream = new MemoryStream (); int compressedSize = readLittleInt (); byte[] properties = ReadBytes (5); if (properties.Length == 0) { throw new IOException ("End of file reached while parsing the file!"); } long accPos = BaseStream.Position; decoder.SetDecoderProperties (properties); decoder.Code (BaseStream, newOutStream, compressedSize, size, null); BaseStream.Position = accPos + compressedSize; return newOutStream.ToArray (); }
public static byte[] Decompress(Stream inStream) { MemoryStream outStream = new MemoryStream(); byte[] buffer = new byte[5]; if (inStream.Read(buffer, 0, 5) != 5) { throw new Exception("Err"); } Decoder decoder = new Decoder(); decoder.SetDecoderProperties(buffer); long outSize = 0L; for (int i = 0; i < 8; i++) { int num3 = inStream.ReadByte(); if (num3 < 0) { throw new Exception("Err"); } outSize |= ((byte)num3) << (8 * i); } long inSize = inStream.Length - inStream.Position; decoder.Code(inStream, outStream, inSize, outSize, null); return outStream.ToArray(); }
private static void Decompress(Stream inputStream, Stream outputStream) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); // Read decoder properties byte[] properties = new byte[5]; // 5 comes from kPropSize (LzmaEncoder.cs) inputStream.Read(properties, 0, 5); // Read the size of the output stream. byte [] fileLengthBytes = new byte[8]; inputStream.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); // Decode coder.SetDecoderProperties(properties); coder.Code(inputStream, outputStream, inputStream.Length, fileLength, null); outputStream.Flush(); outputStream.Close(); }
public static bool Decompress(string FileName, out byte[] outputBytes) { outputBytes = null; if (!File.Exists(FileName)) return false; using(FileStream newInStream = File.OpenRead(FileName)) { SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); newInStream.Seek(0, 0); MemoryStream newOutStream = new MemoryStream(); byte[] properties2 = new byte[5]; if (newInStream.Read(properties2, 0, 5) != 5) throw (new Exception("input .lzma is too short")); long outSize = 0; for (int i = 0; i < 8; i++) { int v = newInStream.ReadByte(); if (v < 0) throw (new Exception("Can't Read 1")); outSize |= ((long)(byte)v) << (8 * i); } decoder.SetDecoderProperties(properties2); long compressedSize = newInStream.Length - newInStream.Position; decoder.Code(newInStream, newOutStream, compressedSize, outSize, null); outputBytes = newOutStream.ToArray(); } return true; }
static SevenZipHelper() { encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); decoder = new Decoder(); }
public void Read(System.IO.Stream instream) { {//Header byte[] b = new byte[28]; instream.Read(b, 0, 28); header.fourcc = BitConverter.ToUInt32(b, 0); int fourcc = MakeFourCC('K', 'F', 'N', 'T'); header.version = BitConverter.ToUInt32(b, 4); header.start_ptr = BitConverter.ToUInt32(b, 8); header.validate_chars = BitConverter.ToUInt32(b, 12); header.non_empty_chars = BitConverter.ToUInt32(b, 16); header.char_size = BitConverter.ToUInt32(b, 20); header._base = BitConverter.ToInt16(b, 24); header.scale = BitConverter.ToInt16(b, 25); } { //instream.Seek(header.start_ptr, System.IO.SeekOrigin.Begin); Dictionary<Int32, Int32> nedata = new Dictionary<int, int>(); for (int i = 0; i < header.non_empty_chars; i++) { byte[] buf = new byte[8]; instream.Read(buf, 0, 8); int l = BitConverter.ToInt32(buf, 0); int r = BitConverter.ToInt32(buf, 4); chardata[l] = new KeyValuePair<int, uint>(r, 0); //nedata[l] = r; } Dictionary<Int32, UInt32> vdata = new Dictionary<int, UInt32>(); for (int i = 0; i < header.validate_chars; i++) { byte[] buf = new byte[8]; instream.Read(buf, 0, 8); int l = BitConverter.ToInt32(buf, 0); uint r = BitConverter.ToUInt32(buf, 4); if (chardata.ContainsKey(l)) { chardata[l] = new KeyValuePair<int, uint>(chardata[l].Key, r); } else { chardata[l] = new KeyValuePair<int, uint>(-1, r); } //vdata[l] = r; } } {//FontInfo fontinfo = new List<font_info>(); for (int i = 0; i < header.non_empty_chars; i++) { byte[] buf = new byte[8]; instream.Read(buf, 0, 8); font_info info; info.top = BitConverter.ToInt16(buf, 0); info.left = BitConverter.ToInt16(buf, 2); info.width = BitConverter.ToUInt16(buf, 4); info.height = BitConverter.ToUInt16(buf, 6); fontinfo.Add(info); } } {//FontData float timer = 0; fontdata = new List<byte[]>(); for (int i = 0; i < header.non_empty_chars; i++) { DateTime t = DateTime.Now; byte[] buf = new byte[8]; instream.Read(buf, 0, 8); ulong len = BitConverter.ToUInt64(buf, 0); byte[] data = new byte[len]; instream.Read(data, 0, (int)len); System.IO.MemoryStream sin = new System.IO.MemoryStream(data); System.IO.MemoryStream sout = new System.IO.MemoryStream(); SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); byte[] properties = new byte[5]; if (sin.Read(properties, 0, 5) != 5) throw (new Exception("input .lzma is too short")); decoder.SetDecoderProperties(properties); //sin.Read(buf, 0,2); int len2 = (int)(header.char_size * header.char_size); decoder.Code(sin, sout, (long)len - 5, (int)len2, null); sout.Position = 0; byte[] decodedata = new byte[len2]; sout.Read(decodedata, 0, len2); fontdata.Add(decodedata); DateTime t2 = DateTime.Now; float delta = (float)(t2 - t).TotalSeconds; timer += delta; } Debug.Log("Load time total:" + timer + " per:" + (timer / header.non_empty_chars)); } }
private void FileDownloaded(Uri uri, byte[] raw, string error, object decompressionTask) { SetTaskProgressBar_ThreadSafe(100, ""); if (error != null) { if (decompressionTask != null) { CompressionTask task = decompressionTask as CompressionTask; if (task != null) task.done = true; } IncrementPatchProgressBar_ThreadSafe(true); Log_ThreadSafe("\tFailed to download " + uri.ToString() + ": " + error); return; } if (decompressionTask != null) { string file = "?"; CompressionTask task = null; Stream inStream = null; Stream outStream = null; try { task = (CompressionTask)decompressionTask; file = task.dl.destination.Name; task.dl.destination.Directory.Create(); inStream = task.dl.tempCompressed.OpenRead(); outStream = task.dl.destination.OpenWrite(); byte[] properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) throw (new Exception("input .lzma is too short")); LZMA.Decoder decoder = new LZMA.Decoder(); decoder.SetDecoderProperties(properties); long outSize = 0; for (int i = 0; i < 8; i++) { int v = inStream.ReadByte(); if (v < 0) throw (new Exception("Can't Read 1")); outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = inStream.Length - inStream.Position; decoder.Code(inStream, outStream, compressedSize, outSize, null); task.done = true; } catch (Exception e) { if (task != null) task.done = true; IncrementPatchProgressBar_ThreadSafe(true); Log_ThreadSafe("\tFailed during decompression of " + file + ": " + e.ToString()); } finally { if (inStream != null) inStream.Close(); if (outStream != null) outStream.Close(); try { if (task != null) task.dl.tempCompressed.Delete(); } catch (Exception) { } } } IncrementPatchProgressBar_ThreadSafe(false); }
public static void ExpandNatives() { Decoder decoder = new Decoder(); MemoryStream output = new MemoryStream(); using (Stream input = File.Open(Path.Combine(DotMinecraft, "bin/" + GetNativeArchiveFile()), FileMode.Open)) { Decoder coder = new Decoder(); byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte[] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); } output.Seek(0, SeekOrigin.Begin); FastZip fastZip = new FastZip(); fastZip.ExtractZip(output, Path.Combine(DotMinecraft, "bin/natives"), FastZip.Overwrite.Always, null, "", "", false, false); File.Delete(Path.Combine(DotMinecraft, "bin/" + GetNativeArchiveFile())); }
/// <summary> /// Decompress the specified stream (C# inside) /// </summary> /// <param name="inStream">The source compressed stream</param> /// <param name="outStream">The destination uncompressed stream</param> /// <param name="inLength">The length of compressed data (null for inStream.Length)</param> /// <param name="codeProgressEvent">The event for handling the code progress</param> public static void DecompressStream(Stream inStream, Stream outStream, int? inLength, EventHandler<ProgressEventArgs> codeProgressEvent) { if (!inStream.CanRead || !outStream.CanWrite) { throw new ArgumentException("The specified streams are invalid."); } var decoder = new Decoder(); long outSize, inSize = (inLength.HasValue ? inLength.Value : inStream.Length) - inStream.Position; decoder.SetDecoderProperties(GetLzmaProperties(inStream, out outSize)); decoder.Code( inStream, outStream, inSize, outSize, new LzmaProgressCallback(inSize, codeProgressEvent)); }
/// <summary> /// Decompress byte array compressed with LZMA algorithm (C# inside) /// </summary> /// <param name="data">Byte array to decompress</param> /// <returns>Decompressed byte array</returns> public static byte[] ExtractBytes(byte[] data) { using (var inStream = new MemoryStream(data)) { var decoder = new Decoder(); inStream.Seek(0, 0); using (var outStream = new MemoryStream()) { long outSize; decoder.SetDecoderProperties(GetLzmaProperties(inStream, out outSize)); decoder.Code(inStream, outStream, inStream.Length - inStream.Position, outSize, null); return outStream.ToArray(); } } }
public static byte[] Uncompress(byte[] bytes) { if (bytes.Length < 5) { throw new Exception("LZMA data is too short."); } using (MemoryStream input = new MemoryStream(bytes)) { // read the decoder properties byte[] properties = new byte[5]; if (input.Read(properties, 0, 5) != 5) { throw (new Exception("LZMA data is too short.")); } long outSize = 0; if (BitConverter.IsLittleEndian) { for (int i = 0; i < 8; i++) { int v = input.ReadByte(); if (v < 0) { throw (new Exception("Can't Read 1.")); } outSize |= ((long)v) << (8 * i); } } MemoryStream output = new MemoryStream(); long compressedSize = input.Length - input.Position; Decoder decoder = new Decoder(); decoder.SetDecoderProperties(properties); decoder.Code(input, output, compressedSize, outSize, null); return output.ToArray(); } }
/// <summary> /// Decompress LZMA compressed stream into outStream. /// Remeber to set desirable positions in input and output streams. /// </summary> /// <param name="inStream">Input stream</param> /// <param name="outStream">Output stream</param> public static void Decompress(Stream inStream, Stream outStream) { byte[] properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) throw (new Exception("Input stream is too short.")); Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); decoder.SetDecoderProperties(properties); var br = new BinaryReader(inStream, Encoding.UTF8); long decompressedSize = br.ReadInt64(); long compressedSize = br.ReadInt64(); decoder.Code(inStream, outStream, compressedSize, decompressedSize, null); }
static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize) { if (numIterations <= 0) return 0; if (dictionarySize < (1 << 18)) { System.Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)"); return 1; } System.Console.Write("\n Compressing Decompressing\n\n"); Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); CoderPropID[] propIDs = { CoderPropID.DictionarySize, }; object[] properties = { (Int32)(dictionarySize), }; UInt32 kBufferSize = dictionarySize + kAdditionalSize; UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize; encoder.SetCoderProperties(propIDs, properties); System.IO.MemoryStream propStream = new System.IO.MemoryStream(); encoder.WriteCoderProperties(propStream); byte[] propArray = propStream.ToArray(); CBenchRandomGenerator rg = new CBenchRandomGenerator(); rg.Set(kBufferSize); rg.Generate(); CRC crc = new CRC(); crc.Init(); crc.Update(rg.Buffer, 0, rg.BufferSize); CProgressInfo progressInfo = new CProgressInfo(); progressInfo.ApprovedStart = dictionarySize; UInt64 totalBenchSize = 0; UInt64 totalEncodeTime = 0; UInt64 totalDecodeTime = 0; UInt64 totalCompressedSize = 0; MemoryStream inStream = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize); MemoryStream compressedStream = new MemoryStream((int)kCompressedBufferSize); CrcOutStream crcOutStream = new CrcOutStream(); for (Int32 i = 0; i < numIterations; i++) { progressInfo.Init(); inStream.Seek(0, SeekOrigin.Begin); compressedStream.Seek(0, SeekOrigin.Begin); encoder.Code(inStream, compressedStream, -1, -1, progressInfo); TimeSpan sp2 = DateTime.UtcNow - progressInfo.Time; UInt64 encodeTime = (UInt64)sp2.Ticks; long compressedSize = compressedStream.Position; if (progressInfo.InSize == 0) throw (new Exception("Internal ERROR 1282")); UInt64 decodeTime = 0; for (int j = 0; j < 2; j++) { compressedStream.Seek(0, SeekOrigin.Begin); crcOutStream.Init(); decoder.SetDecoderProperties(propArray); UInt64 outSize = kBufferSize; System.DateTime startTime = DateTime.UtcNow; decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null); TimeSpan sp = (DateTime.UtcNow - startTime); decodeTime = (ulong)sp.Ticks; if (crcOutStream.GetDigest() != crc.GetDigest()) throw (new Exception("CRC Error")); } UInt64 benchSize = kBufferSize - (UInt64)progressInfo.InSize; PrintResults(dictionarySize, encodeTime, benchSize, false, 0); System.Console.Write(" "); PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize); System.Console.WriteLine(); totalBenchSize += benchSize; totalEncodeTime += encodeTime; totalDecodeTime += decodeTime; totalCompressedSize += (ulong)compressedSize; } System.Console.WriteLine("---------------------------------------------------"); PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0); System.Console.Write(" "); PrintResults(dictionarySize, totalDecodeTime, kBufferSize * (UInt64)numIterations, true, totalCompressedSize); System.Console.WriteLine(" Average"); return 0; }
static int Main2(string[] args) { System.Console.WriteLine("\nLZMA# 4.61 2008-11-23\n"); if (args.Length == 0) { PrintHelp(); return 0; } SwitchForm[] kSwitchForms = new SwitchForm[13]; int sw = 0; kSwitchForms[sw++] = new SwitchForm("?", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("H", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("EOS", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("SI", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("SO", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1); Parser parser = new Parser(sw); try { parser.ParseStrings(kSwitchForms, args); } catch { return IncorrectCommand(); } if (parser[(int)Key.Help1].ThereIs || parser[(int)Key.Help2].ThereIs) { PrintHelp(); return 0; } System.Collections.ArrayList nonSwitchStrings = parser.NonSwitchStrings; int paramIndex = 0; if (paramIndex >= nonSwitchStrings.Count) return IncorrectCommand(); string command = (string)nonSwitchStrings[paramIndex++]; command = command.ToLower(); bool dictionaryIsDefined = false; Int32 dictionary = 1 << 21; if (parser[(int)Key.Dictionary].ThereIs) { Int32 dicLog; if (!GetNumber((string)parser[(int)Key.Dictionary].PostStrings[0], out dicLog)) IncorrectCommand(); dictionary = (Int32)1 << dicLog; dictionaryIsDefined = true; } string mf = "bt4"; if (parser[(int)Key.MatchFinder].ThereIs) mf = (string)parser[(int)Key.MatchFinder].PostStrings[0]; mf = mf.ToLower(); if (command == "b") { const Int32 kNumDefaultItereations = 10; Int32 numIterations = kNumDefaultItereations; if (paramIndex < nonSwitchStrings.Count) if (!GetNumber((string)nonSwitchStrings[paramIndex++], out numIterations)) numIterations = kNumDefaultItereations; return LzmaBench.LzmaBenchmark(numIterations, (UInt32)dictionary); } string train = ""; if (parser[(int)Key.Train].ThereIs) train = (string)parser[(int)Key.Train].PostStrings[0]; bool encodeMode = false; if (command == "e") encodeMode = true; else if (command == "d") encodeMode = false; else IncorrectCommand(); bool stdInMode = parser[(int)Key.StdIn].ThereIs; bool stdOutMode = parser[(int)Key.StdOut].ThereIs; Stream inStream = null; if (stdInMode) { throw (new Exception("Not implemeted")); } else { if (paramIndex >= nonSwitchStrings.Count) IncorrectCommand(); string inputName = (string)nonSwitchStrings[paramIndex++]; inStream = new FileStream(inputName, FileMode.Open, FileAccess.Read); } FileStream outStream = null; if (stdOutMode) { throw (new Exception("Not implemeted")); } else { if (paramIndex >= nonSwitchStrings.Count) IncorrectCommand(); string outputName = (string)nonSwitchStrings[paramIndex++]; outStream = new FileStream(outputName, FileMode.Create, FileAccess.Write); } FileStream trainStream = null; if (train.Length != 0) trainStream = new FileStream(train, FileMode.Open, FileAccess.Read); if (encodeMode) { if (!dictionaryIsDefined) 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; bool eos = parser[(int)Key.EOS].ThereIs || stdInMode; if (parser[(int)Key.Mode].ThereIs) if (!GetNumber((string)parser[(int)Key.Mode].PostStrings[0], out algorithm)) IncorrectCommand(); if (parser[(int)Key.FastBytes].ThereIs) if (!GetNumber((string)parser[(int)Key.FastBytes].PostStrings[0], out numFastBytes)) IncorrectCommand(); if (parser[(int)Key.LitContext].ThereIs) if (!GetNumber((string)parser[(int)Key.LitContext].PostStrings[0], out litContextBits)) IncorrectCommand(); if (parser[(int)Key.LitPos].ThereIs) if (!GetNumber((string)parser[(int)Key.LitPos].PostStrings[0], out litPosBits)) IncorrectCommand(); if (parser[(int)Key.PosBits].ThereIs) if (!GetNumber((string)parser[(int)Key.PosBits].PostStrings[0], out posStateBits)) IncorrectCommand(); 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), mf, eos }; Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; if (eos || stdInMode) fileSize = -1; else fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); if (trainStream != null) { CDoubleStream doubleStream = new CDoubleStream(); doubleStream.s1 = trainStream; doubleStream.s2 = inStream; doubleStream.fileIndex = 0; inStream = doubleStream; long trainFileSize = trainStream.Length; doubleStream.skipSize = 0; if (trainFileSize > dictionary) doubleStream.skipSize = trainFileSize - dictionary; trainStream.Seek(doubleStream.skipSize, SeekOrigin.Begin); encoder.SetTrainSize((uint)(trainFileSize - doubleStream.skipSize)); } encoder.Code(inStream, outStream, -1, -1, null); } else if (command == "d") { byte[] properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) throw (new Exception("input .lzma is too short")); Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); decoder.SetDecoderProperties(properties); if (trainStream != null) { if (!decoder.Train(trainStream)) throw (new Exception("can't train")); } long outSize = 0; for (int i = 0; i < 8; i++) { int v = inStream.ReadByte(); if (v < 0) throw (new Exception("Can't Read 1")); outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = inStream.Length - inStream.Position; decoder.Code(inStream, outStream, compressedSize, outSize, null); } else throw (new Exception("Command Error")); return 0; }
/// <summary>Uncompresses a stream of LZMA-compressed data into a memory stream</summary> /// <param name="source">Source stream containing the LZMA-compressed data</param> /// <param name="compressedLength">Length of the compressed data</param> /// <param name="uncompressedLength">Length the uncompressed data will have</param> /// <returns>A memory stream containing the uncompressed data</returns> private Stream uncompress(Stream source, int compressedLength, int uncompressedLength) { BinaryReader reader = new BinaryReader(source); // Build a memory chunk we can uncompress into MemoryStream uncompressedMemory = new MemoryStream(uncompressedLength); // Set up the LZMA decoder and decode the compressed asset into the memory chunk Decoder decoder = new Decoder(); decoder.SetDecoderProperties(reader.ReadBytes(5)); decoder.Code( source, uncompressedMemory, compressedLength - 5, uncompressedLength, null ); // Done, set the file pointer to the beginning of the memory chunk and return it uncompressedMemory.Position = 0; return uncompressedMemory; }
public static byte[] Decompress(byte[] inputBytes) { MemoryStream newInStream = new MemoryStream(inputBytes); SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); newInStream.Seek(0, 0); MemoryStream newOutStream = new MemoryStream(); byte[] properties2 = new byte[5]; if (newInStream.Read(properties2, 0, 5) != 5) { throw (new Exception("input .lzma is too short")); } long outSize = 0; for (int i = 0; i < 8; i++) { int v = newInStream.ReadByte(); if (v < 0) { throw (new Exception("Can't Read 1")); } outSize |= ((long)(byte)v) << (8 * i); } decoder.SetDecoderProperties(properties2); long compressedSize = newInStream.Length - newInStream.Position; decoder.Code(newInStream, newOutStream, compressedSize, outSize, null); byte[] b = newOutStream.ToArray(); return(b); }