Exemplo n.º 1
0
    //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());
    }