UnityWWW下载压缩包并解压到桌面文件夹中

https://download.csdn.net/download/qq_30928175/10877644工程下载

using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class ZipText : MonoBehaviour
{

    public Text text;

    private string url = "https://www.boxonline.com.cn/xiaopeng/321.zip";

    private bool xiazaichenggong = false;
    private static string xiazai = "";

    // Use this for initialization
    void Start()
    {
        xiazaichenggong = false;
        zhuomianlujing();
        StartCoroutine(Wait_LoadDown("UnityWWW下载压缩包并压缩到桌面", url));
    }

    // Update is called once per frame
    void Update()
    {
        if (xiazaichenggong)
        {
            if (xiazai == "")
            {
                text.text = "正在下载资源";
            }
            else
            {
                text.text = "下载完成";
                xiazaichenggong = false;
            }
        }
    }


    #region 获取桌面路径
    //桌面路径
    private static string desktopPath;

    //获取桌面路径
    private void zhuomianlujing()
    {
        RegistryKey folders;
        folders = OpenRegistryPath(Registry.CurrentUser, @"/software/microsoft/windows/currentversion/explorer/shell folders");
        // Windows用户桌面路径  
        desktopPath = folders.GetValue("Desktop").ToString();
    }
    private RegistryKey OpenRegistryPath(RegistryKey root, string s)
    {
        s = s.Remove(0, 1) + @"/";
        while (s.IndexOf(@"/") != -1)
        {
            root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/")));
            s = s.Remove(0, s.IndexOf(@"/") + 1);
        }
        return root;
    }
    #endregion


    /// 
    /// 下载压缩包
    /// 
    /// 
    /// 
    /// 
    IEnumerator Wait_LoadDown(string ZipID, string url)
    {
        xiazaichenggong = true;
        WWW www = new WWW(url);
        yield return www;
        while (!www.isDone)
        {
            Debug.Log("正在下载");
        }
        if (www.isDone)
        {
            if (www.error == null)
            {
                string dir = desktopPath;
                //Debug.Log(dir);
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                yield return new WaitForEndOfFrame();
                //直接使用 将byte转换为Stream,省去先保存到本地在解压的过程
                SaveZip(ZipID, url, www.bytes, null);

            }
            else
            {
                //Debug.Log(www.error);
            }
        }
    }

    ///  
    /// 解压功能(下载后直接解压压缩文件到指定目录) 
    ///  
    /// www下载转换而来的Stream 
    /// 指定解压目标目录(每一个Obj对应一个Folder) 
    /// 密码 
    /// 解压结果 
    public static bool SaveZip(string ZipID, string url, byte[] ZipByte, string password)
    {
        bool result = true;
        FileStream fs = null;
        ZipInputStream zipStream = null;
        ZipEntry ent = null;
        string fileName;

        ZipID = desktopPath + "/" + ZipID;

        xiazai = ZipID;

        if (!Directory.Exists(ZipID))
        {
            Directory.CreateDirectory(ZipID);
        }
        try
        {
            //直接使用 将byte转换为Stream,省去先保存到本地在解压的过程
            Stream stream = new MemoryStream(ZipByte);
            zipStream = new ZipInputStream(stream);

            if (!string.IsNullOrEmpty(password))
            {
                zipStream.Password = password;
            }
            while ((ent = zipStream.GetNextEntry()) != null)
            {
                if (!string.IsNullOrEmpty(ent.Name))
                {
                    fileName = Path.Combine(ZipID, ent.Name);

                    #region      Android
                    fileName = fileName.Replace('\\', '/');

                    if (fileName.EndsWith("/"))
                    {
                        Directory.CreateDirectory(fileName);
                        continue;
                    }
                    #endregion
                    fs = File.Create(fileName);

                    int size = 2048;
                    byte[] data = new byte[size];
                    while (true)
                    {
                        size = zipStream.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            //fs.Write(data, 0, data.Length);
                            fs.Write(data, 0, size);//解决读取不完整情况
                        }
                        else
                            break;
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
            result = false;
        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
                fs.Dispose();
            }
            if (zipStream != null)
            {
                zipStream.Close();
                zipStream.Dispose();
            }
            if (ent != null)
            {
                ent = null;
            }
            GC.Collect();
            GC.Collect(1);
        }
        return result;
    }

你可能感兴趣的:(UnityWWW下载压缩包)