| | |
| | | #endif |
| | | |
| | | |
| | | public class lzma { |
| | | public class lzma |
| | | { |
| | | |
| | | //if you want to be able to call the functions: get7zinfo, get7zSize, decode2Buffer from a thread set this string before to the Application.persistentDataPath ! |
| | | public static string persitentDataPath=""; |
| | |
| | | //A simple usage of this function is to call it only with the 1st parameter that sets the compression level: setProps(9); |
| | | // |
| | | //Multithread safe advice: call this function before starting any thread operations !!! |
| | | public static void setProps(int level = 5, int dictSize = 16777216, int lc = 3, int lp = 0, int pb = 2, int fb = 32, int numThreads = 2) { |
| | | public static void setProps(int level = 5, int dictSize = 16777216, int lc = 3, int lp = 0, int pb = 2, int fb = 32, int numThreads = 2) |
| | | { |
| | | defaultsSet = true; |
| | | props[0] = level; |
| | | props[1] = dictSize; |
| | |
| | | #endif |
| | | |
| | | |
| | | #if (UNITY_IOS || UNITY_WEBGL || UNITY_IPHONE) && !UNITY_EDITOR |
| | | #if (UNITY_WEBGL) && !UNITY_EDITOR |
| | | |
| | | #if(!UNITY_WEBGL) |
| | | [DllImport("__Internal")] |
| | |
| | | #if UNITY_5_4_OR_NEWER |
| | | #if (UNITY_ANDROID || UNITY_STANDALONE_LINUX) && (!UNITY_EDITOR || UNITY_EDITOR_LINUX) |
| | | private const string libname = "lzma"; |
| | | #elif UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX |
| | | #elif UNITY_EDITOR || UNITY_STANDALONE_WIN |
| | | private const string libname = "liblzma"; |
| | | #endif |
| | | #else |
| | | #if (UNITY_ANDROID || UNITY_STANDALONE_LINUX) && !UNITY_EDITOR |
| | | private const string libname = "lzma"; |
| | | #endif |
| | | #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX |
| | | #if UNITY_EDITOR || UNITY_STANDALONE_WIN |
| | | private const string libname = "liblzma"; |
| | | #endif |
| | | #endif |
| | | |
| | | #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX |
| | | #if (UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX || UNITY_ANDROID || UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX)&& !UNITY_EDITOR_WIN |
| | | #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_ANDROID || UNITY_STANDALONE_LINUX |
| | | #if ( UNITY_STANDALONE_LINUX || UNITY_ANDROID || UNITY_EDITOR_LINUX) && !UNITY_EDITOR_WIN |
| | | //set permissions of a file in user, group, other. Each string should contain any or all chars of "rwx". |
| | | //returns 0 on success |
| | | [DllImport(libname, EntryPoint = "lsetPermissions")] |
| | |
| | | #endif |
| | | |
| | | #if (UNITY_WP_8_1 || UNITY_WSA) && !UNITY_EDITOR |
| | | #if (UNITY_IOS || UNITY_IPHONE) |
| | | [DllImport("__Internal")] |
| | | internal static extern int lsetPermissions(string filePath, string _user, string _group, string _other); |
| | | #endif |
| | | #if UNITY_WSA_10_0 |
| | | [DllImport("liblzma", EntryPoint = "decompress7zip")] |
| | | internal static extern int decompress7zip(string filePath, string exctractionPath, bool fullPaths, string entry, IntPtr progress, IntPtr FileBuffer, int FileBufferLength); |
| | |
| | | // set permissions of a file in user, group, other. |
| | | // Each string should contain any or all chars of "rwx". |
| | | // returns 0 on success |
| | | public static int setFilePermissions(string filePath, string _user, string _group, string _other){ |
| | | #if (UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX || UNITY_ANDROID || UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX || UNITY_IOS || UNITY_IPHONE) && !UNITY_EDITOR_WIN |
| | | public static int setFilePermissions(string filePath, string _user, string _group, string _other) |
| | | { |
| | | #if (UNITY_STANDALONE_LINUX || UNITY_ANDROID || UNITY_EDITOR_LINUX) && !UNITY_EDITOR_WIN |
| | | return lsetPermissions(filePath, _user, _group, _other); |
| | | #else |
| | | return -1; |
| | |
| | | //FileBuffer : A buffer that holds a 7zip file. When assigned the function will decompress from this buffer and will ignore the filePath. (Linux, iOS, Android, MacOSX) |
| | | //use this function from a separate thread to get the progress of the extracted files in the referenced 'progress' integer. |
| | | // |
| | | public static int doDecompress7zip(string filePath, string exctractionPath, int [] progress, bool largeFiles=false, bool fullPaths=false, string entry=null, byte[] FileBuffer=null){ |
| | | public static int doDecompress7zip(string filePath, string exctractionPath, int[] progress, bool largeFiles = false, bool fullPaths = false, string entry = null, byte[] FileBuffer = null) |
| | | { |
| | | |
| | | int res = 0; |
| | | GCHandle ibuf = GCHandle.Alloc(progress, GCHandleType.Pinned); |
| | | |
| | | #if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN |
| | | #if (UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN |
| | | |
| | | if(FileBuffer != null) { |
| | | GCHandle fbuf = GCHandle.Alloc(FileBuffer, GCHandleType.Pinned); |
| | |
| | | |
| | | #endif |
| | | |
| | | #if (!UNITY_WSA && !UNITY_WEBGL && !UNITY_EDITOR_OSX && !UNITY_STANDALONE_LINUX && !UNITY_ANDROID && !UNITY_IOS) || UNITY_EDITOR_WIN || UNITY_WSA_10_0 |
| | | if (largeFiles){ |
| | | #if (!UNITY_WSA && !UNITY_WEBGL && !UNITY_STANDALONE_LINUX && !UNITY_ANDROID) || UNITY_EDITOR_WIN || UNITY_WSA_10_0 |
| | | if (largeFiles) |
| | | { |
| | | res = decompress7zip(@filePath, @exctractionPath, fullPaths, entry, ibuf.AddrOfPinnedObject(), IntPtr.Zero, 0); |
| | | ibuf.Free(); return res; |
| | | }else{ |
| | | } |
| | | else |
| | | { |
| | | res = decompress7zip2(@filePath, @exctractionPath, fullPaths, entry, ibuf.AddrOfPinnedObject(), IntPtr.Zero, 0); |
| | | ibuf.Free(); return res; |
| | | } |
| | |
| | | GCHandle ibuf = GCHandle.Alloc(progress, GCHandleType.Pinned); |
| | | int res = 0; |
| | | |
| | | #if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN |
| | | #if (UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN |
| | | if(FileBuffer != null) { |
| | | GCHandle fbuf = GCHandle.Alloc(FileBuffer, GCHandleType.Pinned); |
| | | |
| | |
| | | } |
| | | #endif |
| | | |
| | | #if (!UNITY_WSA && !UNITY_WEBGL && !UNITY_EDITOR_OSX && !UNITY_STANDALONE_LINUX && !UNITY_ANDROID && !UNITY_IOS) || UNITY_EDITOR_WIN || UNITY_WSA_10_0 |
| | | if (largeFiles){ |
| | | #if (!UNITY_WSA && !UNITY_WEBGL && !UNITY_STANDALONE_LINUX && !UNITY_ANDROID) || UNITY_EDITOR_WIN || UNITY_WSA_10_0 |
| | | if (largeFiles) |
| | | { |
| | | res = decompress7zip(@filePath, @exctractionPath, fullPaths, entry, ibuf.AddrOfPinnedObject(), IntPtr.Zero, 0); |
| | | ibuf.Free(); return res; |
| | | } |
| | | else{ |
| | | else |
| | | { |
| | | res = decompress7zip2(@filePath, @exctractionPath, fullPaths, entry, ibuf.AddrOfPinnedObject(), IntPtr.Zero, 0); |
| | | ibuf.Free(); return res; |
| | | } |
| | |
| | | // |
| | | //You can set the compression properties by calling the setProps function before. |
| | | //setProps(9) for example will set compression evel to highest level. |
| | | public static int LzmaUtilEncode(string inPath, string outPath){ |
| | | public static int LzmaUtilEncode(string inPath, string outPath) |
| | | { |
| | | if (!defaultsSet) setProps(); |
| | | GCHandle prps = GCHandle.Alloc(props, GCHandleType.Pinned); |
| | | int res = lzmaUtil(true, @inPath, @outPath, prps.AddrOfPinnedObject()); |
| | |
| | | //This function decodes a single archive in lzma alone format. |
| | | //inPath : the .lzma file that will be decoded. (use full path + file name) |
| | | //outPath : the decoded file. (use full path + file name) |
| | | public static int LzmaUtilDecode(string inPath, string outPath){ |
| | | public static int LzmaUtilDecode(string inPath, string outPath) |
| | | { |
| | | return lzmaUtil(false, @inPath, @outPath, IntPtr.Zero); |
| | | } |
| | | |
| | |
| | | //FileBuffer : A buffer that holds a 7zip file. When assigned the function will read from this buffer and will ignore the filePath. (Linux, iOS, Android, MacOSX) |
| | | // |
| | | //trueTotalFiles is an integer variable to store the total number of files in a 7z archive, excluding the folders. |
| | | public static long get7zInfo(string filePath, string tempPath = null, byte[] FileBuffer = null){ |
| | | public static long get7zInfo(string filePath, string tempPath = null, byte[] FileBuffer = null) |
| | | { |
| | | |
| | | ninfo.Clear(); sinfo.Clear(); |
| | | trueTotalFiles = 0; |
| | |
| | | string logPath = ""; |
| | | |
| | | #if !NETFX_CORE |
| | | if (@tempPath == null) { |
| | | if (@tempPath == null) |
| | | { |
| | | if(persitentDataPath.Length>0) logPath = @persitentDataPath + "/sevenZip.log"; else logPath = @Application.persistentDataPath + "/sevenZip.log"; |
| | | }else { logPath = @tempPath; } |
| | | } |
| | | else { logPath = @tempPath; } |
| | | #endif |
| | | //for WSA, logPath should always be: Application.persistentDataPath + "/sevenZip.log"; |
| | | #if NETFX_CORE |
| | |
| | | |
| | | if (File.Exists(logPath + ".txt")) File.Delete(logPath + ".txt"); |
| | | |
| | | #if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN |
| | | #if (UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN |
| | | if(FileBuffer != null) { |
| | | GCHandle fbuf = GCHandle.Alloc(FileBuffer, GCHandleType.Pinned); |
| | | res = _getSize(null, logPath, fbuf.AddrOfPinnedObject(), FileBuffer.Length); |
| | |
| | | // : this is useful when your 7z archive resides in a read only location. |
| | | // : the tempPath should be in this form: 'dir/dir/myTempLog' with no slash in the end. The last name will be used as the log's filename. |
| | | //FileBuffer : A buffer that holds a 7zip file. When assigned the function will read from this buffer and will ignore the filePath. (Linux, iOS, Android, MacOSX) |
| | | public static long get7zSize( string filePath=null, string fileName=null, string tempPath=null, byte[] FileBuffer=null){ |
| | | public static long get7zSize(string filePath = null, string fileName = null, string tempPath = null, byte[] FileBuffer = null) |
| | | { |
| | | |
| | | if(filePath!=null){ |
| | | if (filePath != null) |
| | | { |
| | | if(get7zInfo(@filePath, @tempPath, FileBuffer) < 0){ return -1;} |
| | | } |
| | | |
| | | if(ninfo == null){ |
| | | if (ninfo == null) |
| | | { |
| | | if(ninfo.Count==0) { return -1; } |
| | | } |
| | | |
| | | long sum=0; |
| | | |
| | | if(fileName!=null){ |
| | | for(int i=0; i<ninfo.Count; i++){ |
| | | if(ninfo[i].ToString() == fileName){ |
| | | if (fileName != null) |
| | | { |
| | | for (int i = 0; i < ninfo.Count; i++) |
| | | { |
| | | if (ninfo[i].ToString() == fileName) |
| | | { |
| | | return (long)sinfo[i]; |
| | | } |
| | | } |
| | | }else{ |
| | | for(int i=0; i<ninfo.Count; i++){ |
| | | } |
| | | else |
| | | { |
| | | for (int i = 0; i < ninfo.Count; i++) |
| | | { |
| | | sum += (long)sinfo[i]; |
| | | } |
| | | return sum; |
| | |
| | | // : this is useful when your 7z archive resides in a read only location. |
| | | // : the tempPath should be in this form: 'dir/dir/myTempLog' with no slash in the end. The last name will be used as the log's filename. |
| | | //FileBuffer : A buffer that holds a 7zip file. When assigned the function will read from this buffer and will ignore the filePath. (Linux, iOS, Android, MacOSX) |
| | | public static byte[] decode2Buffer( string filePath, string entry, string tempPath=null, byte[] FileBuffer=null){ |
| | | |
| | | public static byte[] decode2Buffer(string filePath, string entry, string tempPath = null, byte[] FileBuffer = null) |
| | | { |
| | | int bufs = (int)get7zSize( @filePath, entry, @tempPath,FileBuffer ); |
| | | if (bufs <= 0) return null;//entry error or it does not exist |
| | | byte[] nb = new byte[bufs]; |
| | |
| | | |
| | | GCHandle dec2buf = GCHandle.Alloc(nb, GCHandleType.Pinned); |
| | | |
| | | #if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN |
| | | #if (UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN |
| | | if(FileBuffer != null) { |
| | | GCHandle fbuf = GCHandle.Alloc(FileBuffer, GCHandleType.Pinned); |
| | | res = decode2Buf(null, entry, dec2buf.AddrOfPinnedObject(), fbuf.AddrOfPinnedObject(), FileBuffer.Length); |
| | |
| | | //You can set the compression properties by calling the setProps function before. |
| | | //setProps(9) for example will set compression level to the highest level. |
| | | // |
| | | public static bool compressBuffer(byte[] inBuffer, ref byte[] outBuffer, bool makeHeader=true){ |
| | | public static bool compressBuffer(byte[] inBuffer, ref byte[] outBuffer, bool makeHeader = true) |
| | | { |
| | | |
| | | if (!defaultsSet) setProps(); |
| | | GCHandle prps = GCHandle.Alloc(props, GCHandleType.Pinned); |
| | |
| | | //1. then write only the data that fit in it. |
| | | //2. or we return 0. |
| | | //It depends on if we set the safe flag to true or not. |
| | | if(res>outBuffer.Length) { |
| | | if (res > outBuffer.Length) |
| | | { |
| | | if(safe){ _releaseBuffer(ptr); return 0; } else { res = outBuffer.Length; } |
| | | } |
| | | |
| | |
| | | |
| | | //same as the compressBuffer function, only this function will put the result in a fixed size buffer to avoid memory allocations. |
| | | //the compressed size is returned so you can manipulate it at will. |
| | | public static int compressBufferFixed(byte[] inBuffer, ref byte[] outBuffer, bool safe = true, bool makeHeader=true){ |
| | | public static int compressBufferFixed(byte[] inBuffer, ref byte[] outBuffer, bool safe = true, bool makeHeader = true) |
| | | { |
| | | |
| | | if (!defaultsSet) setProps(); |
| | | GCHandle prps = GCHandle.Alloc(props, GCHandleType.Pinned); |
| | |
| | | //1. then write only the data that fit in it. |
| | | //2. or we return 0. |
| | | //It depends on if we set the safe flag to true or not. |
| | | if(res>outBuffer.Length) { |
| | | if (res > outBuffer.Length) |
| | | { |
| | | if(safe){ _releaseBuffer(ptr); return 0; } else { res = outBuffer.Length; } |
| | | } |
| | | |
| | |
| | | ERROR_FAIL 11 |
| | | ERROR_THREAD 12 |
| | | */ |
| | | public static int decompressAssetBundle(byte[] inBuffer, ref byte[] outbuffer){ |
| | | public static int decompressAssetBundle(byte[] inBuffer, ref byte[] outbuffer) |
| | | { |
| | | |
| | | int offset = 0; |
| | | |
| | | for(int i=0; i<inBuffer.Length; i++) { |
| | | for (int i = 0; i < inBuffer.Length; i++) |
| | | { |
| | | if(i>1024) break; |
| | | if(inBuffer[i] == 0x5d) { |
| | | if(inBuffer[i+1] == 0x00) { |
| | | if(inBuffer[i+2] == 0x00) { |
| | | if(inBuffer[i+3] == 0x08) { |
| | | if (inBuffer[i] == 0x5d) |
| | | { |
| | | if (inBuffer[i + 1] == 0x00) |
| | | { |
| | | if (inBuffer[i + 2] == 0x00) |
| | | { |
| | | if (inBuffer[i + 3] == 0x08) |
| | | { |
| | | offset = i; break; |
| | | } |
| | | } |
| | |
| | | ERROR_FAIL 11 |
| | | ERROR_THREAD 12 |
| | | */ |
| | | public static int decompressBuffer(byte[] inBuffer, ref byte[] outbuffer, bool useHeader=true, int customLength=0){ |
| | | public static int decompressBuffer(byte[] inBuffer, ref byte[] outbuffer, bool useHeader = true, int customLength = 0) |
| | | { |
| | | |
| | | GCHandle cbuf = GCHandle.Alloc(inBuffer, GCHandleType.Pinned); |
| | | int uncompressedSize = 0; |
| | |
| | | return res; |
| | | } |
| | | |
| | | public static byte[] decompressBuffer(byte[] inBuffer, bool useHeader=true, int customLength=0){ |
| | | public static byte[] decompressBuffer(byte[] inBuffer, bool useHeader = true, int customLength = 0) |
| | | { |
| | | |
| | | GCHandle cbuf = GCHandle.Alloc(inBuffer, GCHandleType.Pinned); |
| | | int uncompressedSize = 0; |
| | |
| | | //same as above function. Only this one outputs to a buffer of fixed which size isn't resized to avoid memory allocations. |
| | | //The fixed buffer should have a size that will be able to hold the incoming decompressed data. |
| | | //returns the uncompressed size. |
| | | public static int decompressBufferFixed(byte[] inBuffer, ref byte[] outbuffer, bool safe = true, bool useHeader=true, int customLength=0){ |
| | | public static int decompressBufferFixed(byte[] inBuffer, ref byte[] outbuffer, bool safe = true, bool useHeader = true, int customLength = 0) |
| | | { |
| | | |
| | | int uncompressedSize = 0; |
| | | |
| | |
| | | //1. write only the data that fit in it. |
| | | //2. or return a negative number. |
| | | //It depends on if we set the safe flag to true or not. |
| | | if(uncompressedSize > outbuffer.Length) { |
| | | if (uncompressedSize > outbuffer.Length) |
| | | { |
| | | if(safe) return -101; else uncompressedSize = outbuffer.Length; |
| | | } |
| | | |