// ============================================================================================================================================================= IEnumerator DownloadZipFile() { Debug.Log("starting download"); myFile = "testZip.zip"; //make sure a previous zip file having the same name with the one we want to download does not exist in the ppath folder if (File.Exists(ppath + "/" + myFile)) { downloadDone = true; yield break; } //File.Delete(ppath + "/" + myFile); //replace the link to the zip file with your own (although this will work also) www = new WWW("https://dl.dropboxusercontent.com/s/xve34ldz3pqvmh1/" + myFile); yield return(www); if (www.error != null) { Debug.Log(www.error); } downloadDone = true; //write the downloaded zip file to the ppath directory so we can have access to it //depending on the Install Location you have set for your app, set the Write Access accordingly! File.WriteAllBytes(ppath + "/" + myFile, www.bytes); www.Dispose(); www = null; }
IEnumerator Download7ZFile() { Debug.Log("starting download"); //make sure a previous 7z file having the same name with the one we want to download does not exist in the ppath folder if (File.Exists(ppath + "/" + myFile)) { File.Delete(ppath + "/" + myFile); } www = new WWW(uri + myFile); yield return(www); if (www.error != null) { Debug.Log(www.error); } downloadDone = true; log = ""; //write the downloaded 7z file to the ppath directory so we can have access to it //depending on the Install Location you have set for your app, set the Write Access accordingly! File.WriteAllBytes(ppath + "/" + myFile, www.bytes); www.Dispose(); www = null; }
IEnumerator DoTestsWSA81() { yield return(true); //File tests //compress a file to flz with highest level of compression (2). lz1 = fLZ.compressFile(ppath + "/" + myFile, ppath + "/" + myFile + ".flz", 2, true, progress); //decompress the previously compressed archive lz2 = fLZ.decompressFile(ppath + "/" + myFile + ".flz", ppath + "/" + myFile + "B.tif", true, progress2); //Buffer tests if (File.Exists(ppath + "/" + myFile)) { byte[] bt = File.ReadAllBytes(ppath + "/" + myFile); //compress a byte buffer (we write the output buffer to a file for debug purposes.) if (fLZ.compressBuffer(bt, ref buff, 2, true)) { lz3 = 1; File.WriteAllBytes(ppath + "/buffer1.flzbuf", buff); } byte[] bt2 = File.ReadAllBytes(ppath + "/buffer1.flzbuf"); //decompress a byte buffer (we write the output buffer to a file for debug purposes.) if (fLZ.decompressBuffer(bt2, ref buff, true)) { lz4 = 1; File.WriteAllBytes(ppath + "/buffer1.tif", buff); } bt2 = null; bt = null; } }
IEnumerator DoTestsWebGL() { yield return(true); //File tests //compress a file to lz4 with highest level of compression (9). lz1 = LZ4.compress(ppath + "/" + myFile, ppath + "/" + myFile + ".lz4", 9, progress); //decompress the previously compressed archive lz2 = LZ4.decompress(ppath + "/" + myFile + ".lz4", ppath + "/" + myFile + "B.tif", bytes); //Buffer tests if (File.Exists(ppath + "/" + myFile)) { byte[] bt = File.ReadAllBytes(ppath + "/" + myFile); //compress a byte buffer (we write the output buffer to a file for debug purposes.) if (LZ4.compressBuffer(bt, ref buff, 9, true)) { lz3 = 1; File.WriteAllBytes(ppath + "/buffer1.lz4buf", buff); } byte[] bt2 = File.ReadAllBytes(ppath + "/buffer1.lz4buf"); //decompress a byte buffer (we write the output buffer to a file for debug purposes.) if (LZ4.decompressBuffer(bt2, ref buff, true)) { lz4 = 1; File.WriteAllBytes(ppath + "/buffer1.tif", buff); } bt2 = null; bt = null; } }
IEnumerator DownloadTestFile() { Debug.Log("starting download"); //make sure a previous flz file having the same name with the one we want to download does not exist in the ppath folder if (File.Exists(ppath + "/" + myFile)) { File.Delete(ppath + "/" + myFile); } //replace the link to the flz file with your own (although this will work also) // string esc = WWW.UnEscapeURL(uri + myFile); www = new WWW(uri + myFile); yield return(www); if (www.error != null) { Debug.Log(www.error); } downloadDone = true; //write the downloaded flz file to the ppath directory so we can have access to it //depending on the Install Location you have set for your app, set the Write Access accordingly! File.WriteAllBytes(ppath + "/" + myFile, www.bytes); www.Dispose(); www = null; }
IEnumerator DoTestsWSA81() { yield return(true); //File tests //compress a file to brotli format. lz1 = brotli.compressFile(ppath + "/" + myFile, ppath + "/" + myFile + ".br", progress); //decompress the previously compressed archive lz2 = brotli.decompressFile(ppath + "/" + myFile + ".br", ppath + "/" + myFile + "Br.tif", progress2); //Buffer tests if (File.Exists(ppath + "/" + myFile)) { byte[] bt = File.ReadAllBytes(ppath + "/" + myFile); //compress a byte buffer (we write the output buffer to a file for debug purposes.) if (brotli.compressBuffer(bt, ref buff, progress3)) { lz3 = 1; File.WriteAllBytes(ppath + "/buffer1.brbuf", buff); } byte[] bt2 = File.ReadAllBytes(ppath + "/buffer1.brbuf"); //decompress a byte buffer (we write the output buffer to a file for debug purposes.) if (brotli.decompressBuffer(bt2, ref buff)) { lz4 = 1; File.WriteAllBytes(ppath + "/buffer1.tif", buff); } bt2 = null; bt = null; } }
void DoTests() { //File tests //compress a file to brotli format. lz1 = brotli.compressFile(ppath + "/" + myFile, ppath + "/" + myFile + ".br", progress); //decompress the previously compressed archive lz2 = brotli.decompressFile(ppath + "/" + myFile + ".br", ppath + "/" + myFile + "Br.tif", progress2); //Buffer tests if (File.Exists(ppath + "/" + myFile)) { byte[] bt = File.ReadAllBytes(ppath + "/" + myFile); //compress a byte buffer (we write the output buffer to a file for debug purposes.) if (brotli.compressBuffer(bt, ref buff, progress3)) { lz3 = 1; File.WriteAllBytes(ppath + "/buffer1.brbuf", buff); } byte[] bt2 = File.ReadAllBytes(ppath + "/buffer1.brbuf"); //decompress a byte buffer (we write the output buffer to a file for debug purposes.) if (brotli.decompressBuffer(bt2, ref buff)) { lz4 = 1; File.WriteAllBytes(ppath + "/buffer1.tif", buff); } //FIXED BUFFER FUNCTION: int decompressedSize = brotli.decompressBuffer(bt2, fixedOutBuffer); if (decompressedSize > 0) { Debug.Log(" # Decompress Fixed size Buffer: " + decompressedSize); } //NEW BUFFER FUNCTION var newBuffer = brotli.decompressBuffer(bt2); if (newBuffer != null) { File.WriteAllBytes(ppath + "/buffer1NEW.tif", newBuffer); Debug.Log(" # new Buffer: " + newBuffer.Length); } bt2 = null; bt = null; newBuffer = null; } //make FileBuffer test on supported platfoms. #if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN //make a temp buffer to read a br file in. if (File.Exists(ppath + "/" + myFile + ".br")) { progress2[0] = 0; byte[] FileBuffer = File.ReadAllBytes(ppath + "/" + myFile + ".br"); fbuftest = brotli.decompressFile(ppath + "/" + myFile + ".br", ppath + "/" + myFile + "FB.tif", progress2, FileBuffer); } #endif }
public void ExportToFile() { var path = Path.Combine(Application.dataPath, $"{Guid.NewGuid().ToString()}"); var levelString = new TokenizedLevelMap(_builder.Build()).ToString(); File.WriteAllBytes(path, Encoding.UTF8.GetBytes(levelString)); Debug.Log($"Wrote Level to {path}"); }
public static void Write(string path, string contents) { #if UNITY_WINRT && !UNITY_EDITOR byte[] bytes = System.Text.Encoding.UTF8.GetBytes(contents); File.WriteAllBytes(path, bytes); #else File.WriteAllText(path, contents); #endif }
public void Save() { string[] scores = new string[3]; scores[0] = highScores.highscore1.ToString(); scores[1] = highScores.highscore2.ToString(); scores[2] = highScores.highscore3.ToString(); string scoresAsString = string.Join(",", scores); var bytes = System.Text.Encoding.UTF8.GetBytes(scoresAsString); File.WriteAllBytes(Application.persistentDataPath + "/hsdata.tm", bytes); }
void DoTests() { //File tests //compress a file to flz with highest level of compression (2). lz1 = fLZ.compressFile(ppath + "/" + myFile, ppath + "/" + myFile + ".flz", 2, true, progress); //decompress the previously compressed archive lz2 = fLZ.decompressFile(ppath + "/" + myFile + ".flz", ppath + "/" + myFile + "B.tif", true, progress2); //Buffer tests if (File.Exists(ppath + "/" + myFile)) { byte[] bt = File.ReadAllBytes(ppath + "/" + myFile); //compress a byte buffer (we write the output buffer to a file for debug purposes.) if (fLZ.compressBuffer(bt, ref buff, 2, true)) { lz3 = 1; File.WriteAllBytes(ppath + "/buffer1.flzbuf", buff); } byte[] bt2 = File.ReadAllBytes(ppath + "/buffer1.flzbuf"); //decompress a byte buffer (we write the output buffer to a file for debug purposes.) if (fLZ.decompressBuffer(bt2, ref buff, true)) { lz4 = 1; File.WriteAllBytes(ppath + "/buffer1.tif", buff); } //FIXED BUFFER FUNCTION: int decommpressedSize = fLZ.decompressBufferFixed(bt2, ref fixedOutBuffer); if (decommpressedSize > 0) { Debug.Log(" # Decompress Fixed size Buffer: " + decommpressedSize); } bt2 = null; bt = null; } //make FileBuffer test on supported platfoms. #if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN //make a temp buffer to read an flz file in. if (File.Exists(ppath + "/" + myFile + ".flz")) { byte[] FileBuffer = File.ReadAllBytes(ppath + "/" + myFile + ".flz"); fbuftest = fLZ.decompressFile(null, ppath + "/" + myFile + ".flzFILE_BUFF.tif", true, progress2, FileBuffer); } #endif }
void Start() { string filePath = Application.persistentDataPath + "/data"; if (File.Exists(filePath)) { string text = Encoding.ASCII.GetString(File.ReadAllBytes(filePath)); num = Int32.Parse(text); } num++; File.WriteAllBytes(filePath, Encoding.ASCII.GetBytes(num.ToString())); Text uiText = gameObject.GetComponent <Text>(); uiText.text = "You have launched this app " + num + " times."; }
void doFileBufferTest() { //For iOS, Android, MacOSX and Linux the plugins can handle a byte buffer as a file. (in this case the www.bytes) //This way you can extract the file or parts of it without writing it to disk. // // !!! Caution !!! Linux: While this works ok with unity 4.x.x, there is a bug introduced with unity5.x versions and might crash the app on // some Linux distributions. Use with caution! // #if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN if (File.Exists(ppath + "/" + myFile)) { byte[] www = File.ReadAllBytes(ppath + "/" + myFile); log = ""; lzres = lzma.doDecompress7zip(null, ppath + "/", true, true, null, www); log += lzres.ToString() + " | "; lzres = lzma.doDecompress7zip(null, ppath + "/", progress, false, true, null, www); log += lzres + " - prg: " + progress[0].ToString() + " | "; tsize = lzma.get7zInfo(null, null, www); log += "tsiz: " + tsize.ToString() + " #files: " + lzma.trueTotalFiles.ToString() + " | "; tsize = lzma.get7zSize(null, null, null, www); log += "tsiz: " + tsize.ToString() + " | "; var buffer = lzma.decode2Buffer(null, "2.txt", null, www); if (buffer != null) { log += "dec2buffer: ok" + "\n"; File.WriteAllBytes(ppath + "/2AAA_FILEBUFFER.txt", buffer); if (buffer.Length > 0) { Debug.Log("FileBuffer_Decode2Buffer: " + buffer.Length); } } } #endif }
void OnRenderImage(RenderTexture src, RenderTexture dest) { Graphics.Blit(src, dest, nothing); Texture2D outputTex = new Texture2D(width, height, TextureFormat.ARGB32, false); RenderTexture buffer = new RenderTexture( width, height, 0, // No depth/stencil buffer RenderTextureFormat.ARGB32, // Standard colour format RenderTextureReadWrite.Linear // No sRGB conversions ); Graphics.Blit(dest, buffer, nothing, -1); RenderTexture.active = new RenderTexture(src); // If not using a scene camera outputTex.ReadPixels( new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height), // Capture the whole texture 0, 0, // Write starting at the top-left texel false // No mipmaps ); File.WriteAllBytes(Application.persistentDataPath + "/renderedPNGs/" + fileName + i++ + ".png", outputTex.EncodeToPNG()); Debug.Log(Application.persistentDataPath + "/" + fileName + ".png"); }
void DoDecompression() { //Now decompress the 7z file (Note: you might decompress it to any folder you have in the ppath directory) //Parameters: lzma.doDecompress7zip(string filePath, string exctractionPath, int[] progress, bool largeFiles=false, bool fullPaths=false, string entry=null, byte[] FileBuffer=null); //filePath : the full path to the archive, including the archives name. (/myPath/myArchive.7z) //exctractionPath : the path in where you want your files to be extracted //progress : a referenced integer to get the progress of the extracted files (second function without the need of ref progress exists also. see examples.) //largeFiles : set this to true if you are extracting files larger then 30-40 Mb. It is slower though but prevents crashing your app when extracting large files! //fullPaths : set this to true if you want to keep the folder structure of the 7z file. //entry : set the name of a single file file you want to extract from your archive. If the file resides in a folder, the full path should be added. // (for examle game/meshes/small/table.mesh ) //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) t = Time.realtimeSinceStartup; //the referenced progress int is new. It will indicate the current index of file beeing decompressed. Use in a separate thread to show it realtime. lzres = lzma.doDecompress7zip(ppath + "/" + myFile, ppath + "/", true, true); //read file names and file sizes of the 7z archive, store them in the lzma.ninfo & lzma.sinfo ArrayLists and return the total uncompressed size of the included files. //On WSA call get7zInfo with 'Application.persistentDataPath + "/sevenZip.log"' as the 2nd parameter! tsize = lzma.get7zInfo(ppath + "/" + myFile); Debug.Log("Total Size: " + tsize + " trueTotalFiles: " + lzma.trueTotalFiles); //Look through the ninfo and info ArrayLists where the file names and sizes are stored. if (lzma.ninfo != null) { for (int i = 0; i < lzma.ninfo.Count; i++) { log += lzma.ninfo[i] + " - " + lzma.sinfo[i] + "\n"; //Debug.Log(i.ToString()+" " +lzma.ninfo[i]+"|"+lzma.sinfo[i].ToString()); } } //get size of a specific file. (if the file path is null it will look in the arraylists created by the get7zInfo function Debug.Log("--->" + lzma.get7zSize(ppath + "/" + myFile, "1.txt")); #if !(UNITY_WSA || UNITY_WSA_8_1 || UNITY_WP_8_1 || UNITY_WINRT_8_1) || UNITY_EDITOR //setup the lzma compression level. (see more at the function declaration at lzma.cs) //This function is not multiple threads safe so call it before starting multiple threads with lzma compress operations. lzma.setProps(9); //these functions do not work for now on windows phone or windows store apps //encode an archive to lzma alone format lzres2 = lzma.LzmaUtilEncode(ppath + "/1.txt", ppath + "/1.txt.lzma"); //decode an archive from lzma alone format lzres3 = lzma.LzmaUtilDecode(ppath + "/1.txt.lzma", ppath + "/1BCD.txt"); #endif //decode a specific file from a 7z archive to a byte buffer var buffer = lzma.decode2Buffer(ppath + "/" + myFile, "1.txt"); if (buffer != null) { File.WriteAllBytes(ppath + "/1AAA.txt", buffer); if (buffer.Length > 0) { Debug.Log("Decode2Buffer: " + buffer.Length); lzres4 = 1; } } //you might want to call this function in another thread to not halt the main thread and to get the progress of the extracted files. //for example: #if !(UNITY_WSA_8_1 || UNITY_WP_8_1 || UNITY_WINRT_8_1) || UNITY_EDITOR #if !NETFX_CORE th = new Thread(Decompress); th.Start(); // faster then coroutine #endif #if NETFX_CORE && UNITY_WSA_10_0 task = new Task(new Action(Decompress)); task.Start(); #endif #endif //calculate the time it took to decompress the file t1 = Time.realtimeSinceStartup - t; }
//Test all the plugin functions. // void DoDecompression() { //---------------------------------------------------------------------------------------------------------------- //commented out example on how to set the permissions of a MacOSX executbale that has been unzipped so it can run. // //lzip.setFilePermissions(ppath + "/Untitled.app", "rwx","rx","rx"); //lzip.setFilePermissions(ppath + "/Untitled.app/Contents/MacOS/Untitled", "rwx","rx","rx"); // //---------------------------------------------------------------------------------------------------------------- //Windows & WSA10 only (see lzip.cs for more info) #if (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_WSA) lzip.setEncoding(65001); //CP_UTF8 // CP_OEMCP/UNICODE = 1 #endif //validate sanity of a zip archive plog("Validate: " + lzip.validateFile(ppath + "/testZip.zip").ToString()); //decompress the downloaded file zres = lzip.decompress_File(ppath + "/testZip.zip", ppath + "/", progress, null, progress2); plog("decompress: " + zres.ToString()); plog(""); //get the true total files of the zip plog("true total files: " + lzip.getTotalFiles(ppath + "/testZip.zip")); //get the total entries of the zip plog("true total entries: " + lzip.getTotalEntries(ppath + "/testZip.zip")); //entry exists bool eres = lzip.entryExists(ppath + "/testZip.zip", "dir1/dir2/test2.bmp"); plog("entry exists: " + eres.ToString()); //get entry dateTime plog(""); plog("DateTime: " + lzip.entryDateTime(ppath + "/testZip.zip", "dir1/dir2/test2.bmp").ToString()); //extract an entry zres = lzip.extract_entry(ppath + "/testZip.zip", "dir1/dir2/test2.bmp", ppath + "/test22P.bmp", null, progress2); plog("extract entry: " + zres.ToString()); plog(""); //compress a file and add it to a new zip zres = lzip.compress_File(9, ppath + "/test2Zip.zip", ppath + "/dir1/dir2/test2.bmp", false, "dir1/dir2/test2.bmp"); plog("compress: " + zres.ToString()); //append a file to it zres = lzip.compress_File(9, ppath + "/test2Zip.zip", ppath + "/dir1/dir2/dir3/Unity_1.jpg", true, "dir1/dir2/dir3/Unity_1.jpg"); plog("append: " + zres.ToString()); plog(""); //compress multiple files added in some lists, and protect them with a password (disabled for WSA due to certification reasons) // #if !UNITY_WSA || UNITY_EDITOR //create a list of files to get compressed List <string> myFiles = new List <string>(); myFiles.Add(ppath + "/test22P.bmp"); myFiles.Add(ppath + "/dir1/dir2/test2.bmp"); //create an optional list with new names for the above listings List <string> myNames = new List <string>(); myNames.Add("NEW_test22P.bmp"); myNames.Add("dir13/dir23/New_test2.bmp"); //use password and bz2 method zres = lzip.compress_File_List(9, ppath + "/newTestZip.zip", myFiles.ToArray(), progress, false, myNames.ToArray(), "password"); plog("MultiFile Compress password: "******"/newTestZip.zip", ppath + "/", progress, null, progress2, "password"); plog("decompress password: "******""); #endif //compress a buffer to a file and name it. plog("Buffer2File: " + lzip.buffer2File(9, ppath + "/test3Zip.zip", "buffer.bin", reusableBuffer).ToString()); //compress a buffer, name it and append it to an existing zip archive plog("Buffer2File append: " + lzip.buffer2File(9, ppath + "/test3Zip.zip", "dir4/buffer.bin", reusableBuffer, true).ToString()); // Debug.Log(reusableBuffer.Length); plog(""); //get the uncompressed size of a specific file in the zip archive plog("get entry size: " + lzip.getEntrySize(ppath + "/testZip.zip", "dir1/dir2/test2.bmp").ToString()); plog(""); //extract a file in a zip archive to a byte buffer (referenced buffer method) plog("entry2Buffer1: " + lzip.entry2Buffer(ppath + "/testZip.zip", "dir1/dir2/test2.bmp", ref reusableBuffer2).ToString()); // File.WriteAllBytes(ppath + "/out.bmp", reusableBuffer2); plog(""); //extract an entry in a zip archive to a fixed size buffer plog("entry2FixedBuffer: " + lzip.entry2FixedBuffer(ppath + "/testZip.zip", "dir1/dir2/test2.bmp", ref fixedBuffer).ToString()); plog(""); //GZIP TESTS--------------------------------------------------------------------------------------------------------------- //create a buffer that will store the compressed gzip data. //it should be at least the size of the input buffer +18 bytes. var btt = new byte[reusableBuffer2.Length + 18]; //compress a buffer to gzip format and add gzip header and footer //returns total size of compressed buffer. int rr = lzip.gzip(reusableBuffer2, btt, 9, true, true); plog("gzip compressed size: " + rr); //create a buffer to store the compressed data (optional, if you want to write out a gzip file) var bt2 = new byte[rr]; //copy the data to the new buffer Buffer.BlockCopy(btt, 0, bt2, 0, rr); //write a .gz file File.WriteAllBytes(ppath + "/test2.bmp.gz", bt2); //create a buffer to decompress a gzip buffer var bt3 = new byte[lzip.gzipUncompressedSize(bt2)]; //decompress the gzip compressed buffer int gzres = lzip.unGzip(bt2, bt3); if (gzres > 0) { File.WriteAllBytes(ppath + "/test2GZIP.bmp", bt3); plog("gzip decompression: success " + gzres.ToString()); } else { plog("gzip decompression error: " + gzres.ToString()); } btt = null; bt2 = null; bt3 = null; plog(""); //END GZIP TESTS----------------------------------------------------------------------------------------------------------- //extract a file in a zip archive to a byte buffer (new buffer method) var newBuffer = lzip.entry2Buffer(ppath + "/testZip.zip", "dir1/dir2/test2.bmp"); zres = 0; if (newBuffer != null) { zres = 1; } plog("entry2Buffer2: " + zres.ToString()); // write a file out to confirm all was ok //File.WriteAllBytes(ppath + "/out.bmp", newBuffer); plog(""); //FIXED BUFFER FUNCTIONS: int compressedSize = lzip.compressBufferFixed(newBuffer, ref fixedInBuffer, 9); plog(" # Compress Fixed size Buffer: " + compressedSize.ToString()); if (compressedSize > 0) { int decommpressedSize = lzip.decompressBufferFixed(fixedInBuffer, ref fixedOutBuffer); if (decommpressedSize > 0) { plog(" # Decompress Fixed size Buffer: " + decommpressedSize.ToString()); } } plog(""); //compress a buffer into a referenced buffer pass = lzip.compressBuffer(reusableBuffer2, ref reusableBuffer3, 9); plog("compressBuffer1: " + pass.ToString()); // write a file out to confirm all was ok //File.WriteAllBytes(ppath + "/out.bin", reusableBuffer3); //compress a buffer and return a new buffer with the compresed data. newBuffer = lzip.compressBuffer(reusableBuffer2, 9); zres = 0; if (newBuffer != null) { zres = 1; } plog("compressBuffer2: " + zres.ToString()); plog(""); //decompress a previously compressed buffer into a referenced buffer pass = lzip.decompressBuffer(reusableBuffer3, ref reusableBuffer2); plog("decompressBuffer1: " + pass.ToString()); //Debug.Log(reusableBuffer2.Length); // write a file out to confirm all was ok //File.WriteAllBytes(ppath + "/out.bmp", reusableBuffer2); zres = 0; if (newBuffer != null) { zres = 1; } //decompress a previously compressed buffer into a new returned buffer newBuffer = lzip.decompressBuffer(reusableBuffer3); plog("decompressBuffer2: " + zres.ToString()); plog(""); //get file info of the zip file (names, uncompressed and compressed sizes) plog("total bytes: " + lzip.getFileInfo(ppath + "/testZip.zip").ToString()); //Look through the ninfo, uinfo and cinfo Lists where the file names and sizes are stored. if (lzip.ninfo != null) { for (int i = 0; i < lzip.ninfo.Count; i++) { log += lzip.ninfo[i] + " - " + lzip.uinfo[i] + " / " + lzip.cinfo[i] + "\n"; } } plog(""); #if !(UNITY_WSA_8_1 || UNITY_WP_8_1 || UNITY_WINRT_8_1) || UNITY_EDITOR //Until a replacement function is made for 'Directory.GetFiles' on WSA8.1 this function is disabled for it. //Recursively compress a directory lzip.compressDir(ppath + "/dir1", 9, ppath + "/recursive.zip", false, null); plog("recursive - no. of files: " + lzip.cProgress.ToString()); //decompress the above compressed zip to make sure all was ok. zres = lzip.decompress_File(ppath + "/recursive.zip", ppath + "/recursive/", progress, null, progress2); plog("decompress recursive: " + zres.ToString()); #endif //multithreading example to show progress of extraction, using the ref progress int //in this example it happens to fast, because I didn't want the user to download a big file with many entrie. #if !NETFX_CORE Thread th = new Thread(decompressFunc); th.Start(); #endif #if NETFX_CORE && UNITY_WSA_10_0 Task task = new Task(new Action(decompressFunc)); task.Start(); #endif //delete/replace entry example if (File.Exists(ppath + "/test-Zip.zip")) { File.Delete(ppath + "/test-Zip.zip"); } #if UNITY_WSA && !UNITY_EDITOR if (File.Exists(ppath + "/testZip.zip")) { lzip.fileCopy(ppath + "/testZip.zip", ppath + "/test-Zip.zip"); } #else if (File.Exists(ppath + "/testZip.zip")) { File.Copy(ppath + "/testZip.zip", ppath + "/test-Zip.zip"); } #endif //replace an entry with a byte buffer var newBuffer3 = lzip.entry2Buffer(ppath + "/testZip.zip", "dir1/dir2/test2.bmp"); plog("replace entry: " + lzip.replace_entry(ppath + "/test-Zip.zip", "dir1/dir2/test2.bmp", newBuffer3, 9, null).ToString()); //replace an entry with a file in the disk (ability to asign a password or bz2 compression) plog("replace entry 2: " + lzip.replace_entry(ppath + "/test-Zip.zip", "dir1/dir2/test2.bmp", ppath + "/dir1/dir2/test2.bmp", 9, null, null).ToString()); //delete an entry in the zip plog("delete entry: " + lzip.delete_entry(ppath + "/test-Zip.zip", "dir1/dir2/test2.bmp").ToString()); }
IEnumerator buff2buffTest() { //Example on how to decompress an lzma compressed asset bundle from the streaming assets folder. //(editor, standalone example) for iOS, Android or WSA use an apropriate method to get the bytes buffer from the file. #if (UNITY_STANDALONE_LINUX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WSA_10_0) && (!UNITY_IOS && !UNITY_ANDROID) || UNITY_EDITOR byte[] sa = File.ReadAllBytes(saPath + "/female_eyes.assetbundle"); if (lzma.decompressAssetBundle(sa, ref buff) == 0) { File.WriteAllBytes(ppath + "/female_eyes.assetbundle", buff); Debug.Log(" #-> AssetBundle Decompressed: ok"); } sa = null; #endif //BUFFER TO BUFFER lzma alone compression/decompression EXAMPLE // //An example on how to decompress an lzma alone file downloaded through www without storing it to disk //using just the www.bytes buffer. //Download a file. WWW w = new WWW("https://dl.dropboxusercontent.com/s/tzptxugk909pia1/google.jpg.lzma"); yield return(w); if (w.error == null) { //we decompress the lzma file in the buff buffer. if (lzma.decompressBuffer(w.bytes, ref buff) == 0) { pass1 = true; //we write it to disk just to check that the decompression was ok File.WriteAllBytes(ppath + "/google.jpg", buff); } else { Debug.Log("Error decompressing www.bytes to buffer"); pass1 = false; } } else { Debug.Log(w.error); } w.Dispose(); w = null; #if !(UNITY_WSA || UNITY_WSA_8_1 || UNITY_WP_8_1 || UNITY_WINRT_8_1) || UNITY_EDITOR //Example on how to compress a buffer. if (File.Exists(ppath + "/google.jpg")) { byte[] bt = File.ReadAllBytes(ppath + "/google.jpg"); //compress the data buffer into a compressed buffer if (lzma.compressBuffer(bt, ref buff)) { pass2 = true; //write it to disk just for checking purposes File.WriteAllBytes(ppath + "/google.jpg.lzma", buff); //print info Debug.Log("uncompressed size in lzma: " + BitConverter.ToUInt64(buff, 5)); Debug.Log("lzma size: " + buff.Length); } else { pass2 = false; Debug.Log("could not compress to buffer ..."); } //FIXED BUFFER FUNCTIONS: int compressedSize = lzma.compressBufferFixed(bt, ref fixedInBuffer); Debug.Log(" #-> Compress Fixed size Buffer: " + compressedSize); if (compressedSize > 0) { int decommpressedSize = lzma.decompressBufferFixed(fixedInBuffer, ref fixedOutBuffer); if (decommpressedSize > 0) { Debug.Log(" #-> Decompress Fixed size Buffer: " + decommpressedSize); } } bt = null; } #else yield return(false); #endif }