SevenZipHelper.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.IO;
  3. using SevenZip.Compression.LZMA;
  4. namespace UnityFS
  5. {
  6. public static class ComparessHelper
  7. {
  8. public static MemoryStream Decompress7Zip(MemoryStream inStream)
  9. {
  10. var decoder = new Decoder();
  11. inStream.Seek(0, SeekOrigin.Begin);
  12. var newOutStream = new MemoryStream();
  13. var properties = new byte[5];
  14. if (inStream.Read(properties, 0, 5) != 5)
  15. throw new Exception("input .lzma is too short");
  16. long outSize = 0;
  17. for (var i = 0; i < 8; i++)
  18. {
  19. var v = inStream.ReadByte();
  20. if (v < 0)
  21. throw new Exception("Can't Read 1");
  22. outSize |= ((long)(byte)v) << (8 * i);
  23. }
  24. decoder.SetDecoderProperties(properties);
  25. var compressedSize = inStream.Length - inStream.Position;
  26. decoder.Code(inStream, newOutStream, compressedSize, outSize, null);
  27. newOutStream.Position = 0;
  28. return newOutStream;
  29. }
  30. public static void Decompress7Zip(Stream compressedStream, Stream decompressedStream, long compressedSize, long decompressedSize)
  31. {
  32. var basePosition = compressedStream.Position;
  33. var decoder = new Decoder();
  34. var properties = new byte[5];
  35. if (compressedStream.Read(properties, 0, 5) != 5)
  36. throw new Exception("input .lzma is too short");
  37. decoder.SetDecoderProperties(properties);
  38. decoder.Code(compressedStream, decompressedStream, compressedSize - 5, decompressedSize, null);
  39. compressedStream.Position = basePosition + compressedSize;
  40. }
  41. }
  42. }