Example #1
0
        private static void DecompressWorkThread(object state)
        {
            BundleDownloader bdl = state as BundleDownloader;

            if (System.IO.File.Exists(bdl.mDestPath))
            {
                System.IO.File.Delete(bdl.mDestPath);
            }

            if (bdl.mBytes == null || bdl.mBytes.Length == 0)
            {
                EB.Debug.LogError("DecompressWorkThread: empty bytes for bundle {0}", bdl.mName);
                bdl.mError = "Decompress bundle failed, empty bytes";
                return;
            }

            try
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(bdl.mDestPath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write, System.IO.FileShare.Write))
                {
                    HTTP.Zlib.ZlibStream zo = new HTTP.Zlib.ZlibStream(fs, HTTP.Zlib.CompressionMode.Decompress);
                    zo.Write(bdl.mBytes, 0, bdl.mBytes.Length);

                    bdl.mUncompressedSize = zo.TotalOut;
                    EB.Debug.LogWarning("Saved asset bundle {0} to {1}, Original Size:{2}, Uncompressed Size:{3}", bdl.mName, bdl.mDestPath, bdl.mBytes.Length, bdl.mUncompressedSize);

                    zo.Flush();
                    zo.Close();
                    zo = null;

                    fs.Close();
                    fs.Dispose();

                    if (bdl.mBytes != null)
                    {
                        downloadBytes += bdl.mBytes.Length;

                        if (bdl.mBytes.Length > 10000000)
                        {
                            int gid = System.GC.GetGeneration(bdl.mBytes);
                            bdl.mBytes = null;
                            System.GC.Collect(gid);
                        }
                        else
                        {
                            bdl.mBytes = null;
                        }
                        if (downloadBytes > 30000000)
                        {
                            System.GC.Collect();
                            downloadBytes = 0;
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                bdl.mError = ex.Message;
                EB.Debug.LogError("Decompress bundle {0} error {1} stack {2}", bdl.mName, ex.Message, ex.StackTrace);
            }

            bdl.mDecompressDone = true;
        }
Example #2
0
    /// <summary>
    /// 压缩指定路径的Bundle文件为CZ格式
    /// </summary>
    /// <param name="bundlePath"></param>
    /// <param name="deleteSource"></param>
    /// <returns></returns>
    private static bool CompressBundle(ref string bundlePath, bool deleteSource = false)
    {
        string zipFilePath = Path.ChangeExtension(bundlePath, "cz");

        FileStream fin  = null;
        FileStream fout = null;

        //ComponentAce.Compression.Libs.zlib.ZOutputStream zout = null;
        HTTP.Zlib.ZlibStream zout = null;

        bool result = false;

        int BUFFER_SIZE = 8192;

        try
        {
            if (File.Exists(zipFilePath))
            {
                File.Delete(zipFilePath);
            }

            fin  = new FileStream(bundlePath, FileMode.Open, FileAccess.Read);
            fout = new FileStream(zipFilePath, FileMode.CreateNew, FileAccess.Write);
            // move Plugins/zlib to Plugins/Editor/zlib to reduce package size
            //zout = new ComponentAce.Compression.Libs.zlib.ZOutputStream(fout, -1);
            zout = new HTTP.Zlib.ZlibStream(fout, HTTP.Zlib.CompressionMode.Compress);

            int    n      = 0;
            byte[] buffer = new byte[BUFFER_SIZE];
            do
            {
                n = fin.Read(buffer, 0, BUFFER_SIZE);
                zout.Write(buffer, 0, n);
            } while (n > 0);

//			using (Ionic.Zip.ZipFile zp = new Ionic.Zip.ZipFile(zipFilePath))
//			{
//				zp.AddFile(bundlePath, ".");
//				zp.Save();
//			}

            if (deleteSource)
            {
                if (fin != null)
                {
                    fin.Close();
                }
                System.IO.File.Delete(bundlePath);
            }

            result = true;
        }
        catch (Exception eError)
        {
            Debug.LogException(eError);
            Debug.LogError(System.String.Format("Compress {0} to {1} failed. Fatal Error!", bundlePath, zipFilePath));
            result = false;
        }
        finally
        {
            if (zout != null)
            {
                zout.Flush();
                zout.Close();
            }
            if (fin != null)
            {
                fin.Close();
            }
            if (fout != null)
            {
                fout.Close();
            }

            bundlePath = zipFilePath;
        }

        return(result);
    }