internal static byte[] DecompressLZMAFile(byte[] lzmaFile) { int len = (int)BitConverter.ToInt32(lzmaFile, 5); //this is technically a 32-bit but since MEM code can't handle 64 bit sizes we are just going to use 32bit. if (len >= 0) { byte[] strippedData = new byte[lzmaFile.Length - 8]; //Non-Streamed (made from disk) Buffer.BlockCopy(lzmaFile, 0, strippedData, 0, 5); Buffer.BlockCopy(lzmaFile, 13, strippedData, 5, lzmaFile.Length - 13); return(Decompress(strippedData, (uint)len)); } else if (len == -1) { //Streamed. MEM code can't handle streamed so we have to fallback var lzmads = new LzmaDecodeStream(new MemoryStream(lzmaFile)); using var decompressedStream = new MemoryStream(); int bufSize = 24576, count; byte[] buf = new byte[bufSize]; while (/*lzmads.Position < lzmaFile.Length && */ (count = lzmads.Read(buf, 0, bufSize)) > 0) { decompressedStream.Write(buf, 0, count); } return(decompressedStream.ToArray()); } else { Debug.WriteLine("Cannot decompress LZMA array: Length is not positive or -1 (" + len + ")! This is not an LZMA array"); return(null); //Not LZMA! } }
/// <summary> /// Decompresses LZMA. Uses native code if non-streamed (produced by lzma.exe), uses managed code if streamed (which can be done by things such as PHP) /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] DecompressLZMA(MemoryStream input) { input.Position = 5; var lzmaLen = input.ReadInt32(); input.Position = 0; if (lzmaLen > 0) { // Non streamed LZMA return(LZMA.DecompressLZMAFile(input.ToArray())); } else { // It's streaming lzma. MEM code can't handle streamed so we have to fallback var lzmads = new LzmaDecodeStream(input); using var decompressedStream = new MemoryStream(); int bufSize = 24576, count; byte[] buf = new byte[bufSize]; while (/*lzmads.Position < lzmaFile.Length && */ (count = lzmads.Read(buf, 0, bufSize)) > 0) { decompressedStream.Write(buf, 0, count); } return(decompressedStream.ToArray()); } }
/// <summary> /// 使用Lzma算法从字节流解压缩为集合 /// </summary> /// <typeparam name="Data"></typeparam> /// <param name="buffer"></param> /// <returns></returns> public static List <Data> Decodes <Data>(byte[] buffer) where Data : class { if (buffer == null || buffer.Length == 0) { return(null); } List <Data> lstData = new List <Data>(); using (var input = new MemoryStream()) { input.Write(buffer, 0, buffer.Length); input.Position = 0; var decoder = new LzmaDecodeStream(input); using (MemoryStream output = new MemoryStream()) { int bufSize = 24576, count; byte[] buf = new byte[bufSize]; while ((count = decoder.Read(buf, 0, bufSize)) > 0) { output.Write(buf, 0, count); } output.Position = 0; BinaryFormatter formatter = new BinaryFormatter(); while (true) { if (output.Position == output.Length) { break; } Data data = null; try { data = formatter.Deserialize(output) as Data; } catch (SerializationException ex) { LogHelper.GetInstance().Write("反序列化失败!"); } if (data == null) { break; } lstData.Add(data); } } decoder.Close(); } return(lstData); }
public void LzmaEncodeDecodeTest() { using (var output = new FileStream(TemporaryFile, FileMode.Create)) { var encoder = new LzmaEncodeStream(output); using (var inputSample = new FileStream(@"TestData\zip.zip", FileMode.Open)) { int bufSize = 24576, count; var buf = new byte[bufSize]; while ((count = inputSample.Read(buf, 0, bufSize)) > 0) { encoder.Write(buf, 0, count); } } encoder.Close(); } var newZip = Path.Combine(OutputDirectory, "new.zip"); using (var input = new FileStream(TemporaryFile, FileMode.Open)) { var decoder = new LzmaDecodeStream(input); using (var output = new FileStream(newZip, FileMode.Create)) { int bufSize = 24576, count; var buf = new byte[bufSize]; while ((count = decoder.Read(buf, 0, bufSize)) > 0) { output.Write(buf, 0, count); } } } Assert.IsTrue(File.Exists(newZip)); using (var extractor = new SevenZipExtractor(newZip)) { Assert.AreEqual(1, extractor.FilesCount); Assert.AreEqual("zip.txt", extractor.ArchiveFileNames[0]); } }
public bool unzipFile(string filePath, out string result) { FileInfo fileInfo = new FileInfo(filePath); // Step 1. Fix file fixFile(filePath, fileInfo); // Step 2. Unzip file string outputPath = fileInfo.FullName.Replace(".csv", "_r.csv"); try { using (var input = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite)) { var decoder = new LzmaDecodeStream(input); using (var output = new FileStream(outputPath, FileMode.Create)) { int bufSize = 24576, count; byte[] buf = new byte[bufSize]; while ((count = decoder.Read(buf, 0, bufSize)) > 0) { output.Write(buf, 0, count); } } decoder.Close(); } } catch (Exception) { result = "[Broken file]\t" + fileInfo.Name; File.Delete(outputPath); return(false); } result = "[Unzip file]\t" + fileInfo.Name; return(true); }