C # Unity www download assetbundle. ZIP file and unzip to local

Source: Internet
Author: User
Tags int size
  #region www download assetbundle resources///<summary>///www download assetbundle resources///</summary> <param name= "url" ></param>///<returns></returns> public static bool UW
                Wwload (string url) {try {var assetbundlename = GetFileName (URL); 
                Directory string path = Assetbundleloger.applicationpersistentassets + Assetbundleloger.getpathgap ();
                String FilePath = path + assetbundlename +. zip; No directory generates an if (!
                Directory.Exists (path)) directory.createdirectory (path);
                    Directory if there is no download if (file.exists (path + Assetbundlename + ". Assetbundle")) {
                return true;
                www www = new www (URL);
                stopwatch SW = new Stopwatch (); Sw.
                Start ();
           byte[] ty = new byte[0];     while (!www.isdone) {if (sw).
                        Elapsedmilliseconds >= 1000) {if (Www.error!= null)
                            {MessageBoxWnd.Instance.Show ("Connection Timeout-----please later"); Sw.
                            Stop ();
                        return false; else {if (sw.
                                Elapsedmilliseconds >= 4500) {ty = www.bytes; if (Ty.  Length = = 0) {MessageBoxWnd.Instance.Show ("Connection Timeout-----
                                    Please later, no return data "); Sw.
                                    Stop ();
                                return false; (}}}}} TRy {System.IO.File.WriteAllBytes (FilePath, www.bytes);
                    catch (Exception e) {MessageBoxWnd.Instance.Show (e.message);
                Throw
                //Extract file Decompress (FilePath, path); Sw.
                Stop (); Debug.Log (SW.
                Elapsedmilliseconds);
            System.IO.File.Delete (FilePath);
                catch (Exception e) {MessageBoxWnd.Instance.Show (e.message);
            Throw
        return true; #endregion///<summary>///remove name///</summary>///<param name= 
        ' Path ' ></param>///<returns></returns> public static string GetFileName (string path) {var Urlarray = path.
            Split ('/'); var urlarrayc = Urlarray[urlarRay.
            LENGTH-1]; var name = Urlarrayc.
            Split ('. ');
        return name[0]; #region Extract Files///<summary>///Extract Files///</summary>///<param Name= "Gzipfile" > Zip package file name </param>///<param name= "TargetPath" > Decompression target path </param> PU
            Blic static void Decompress (string gzipfile, String TargetPath) {string directoryname = TargetPath; if (! Directory.Exists (directoryname)) directory.createdirectory (directoryname);//Generate decompression directory string currentdirectory = d
            Irectoryname;
            byte[] data = new BYTE[1];
            int size = 0;
            ZipEntry theentry = null; using (zipinputstream s = new Zipinputstream (File.openread (gzipfile))) {while (Theentry = S.
                        Getnextentry ())!= null) {if (theentry.isdirectory) {//The node is a directory If(!
                    Directory.Exists (CurrentDirectory + theentry.name)) directory.createdirectory (CurrentDirectory + theEntry.Name);
                        } else {if (theentry.name!= String.Empty) {//check if the multilevel directory exists if (theEntry.Name.Contains "//") {string parentdirpath = TheEntry.Name.Remove (theentry).
                                Name.lastindexof ("//") + 1); if (! Directory.Exists (Parentdirpath)) {Directory.createdir
                                Ectory (CurrentDirectory + parentdirpath); }//Unzip the file to the specified directory using (FileStream str Eamwriter = file.create (CurrentDirectory + theentry.name)) {W Hile(true)
                                    {data = new byte[theentry.compressedsize]; Size = s.read (data, 0, data.)
                                    Length);

                                    if (size <= 0) break;
                                StreamWriter.Write (data, 0, size);
                            } streamwriter.close ();
            }}} s.close (); }} #endregion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.