Lz4ArchiveBase.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using K4os.Compression.LZ4;
  3. namespace SongCore.Arcache
  4. {
  5. // idx: [[len:2] [u8str:len] [offset:8] [compressedSize:8] [originalSize:8]] ...
  6. // dat: [block] ...
  7. public abstract class Lz4ArchiveBase
  8. {
  9. public static byte[] Compress(byte[] source)
  10. {
  11. var target = new byte[LZ4Codec.MaximumOutputSize(source.Length)];
  12. var len = LZ4Codec.Encode(source, target, LZ4Level.L12_MAX);
  13. var compressed = new byte[len];
  14. Buffer.BlockCopy(target, 0, compressed, 0, len);
  15. return compressed;
  16. }
  17. public static byte[] Decompress(byte[] source, int originalSize)
  18. {
  19. var decompressed = new byte[originalSize];
  20. LZ4Codec.Decode(source, decompressed);
  21. return decompressed;
  22. }
  23. public static byte[] CompressPickle(byte[] source)
  24. {
  25. return LZ4Pickler.Pickle(source,LZ4Level.L12_MAX);
  26. }
  27. public static byte[] DecompressPickle(byte[] source)
  28. {
  29. return LZ4Pickler.Unpickle(source);
  30. }
  31. protected internal string BlobFilePath;
  32. protected internal string IndexFilePath;
  33. protected internal Lz4ArchiveBase(string pathAndBaseName)
  34. {
  35. BlobFilePath = pathAndBaseName + ".lz4a";
  36. IndexFilePath = pathAndBaseName + ".lz4i";
  37. }
  38. }
  39. }