码迷,mamicode.com
首页 > 编程语言 > 详细

Unity游戏资源解压

时间:2017-09-17 20:13:09      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:call   ntp   write   called   done   密码   rtc   overwrite   open   

Unity2017;ICSharpCode.SharpZipLib.Zip;

using UnityEngine;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

public class ZipTest : MonoBehaviour
{

    private WWW www;
    private bool isUnzipped = false;

    // Use this for initialization
    void Start()
    {
        string url = "http://54.193.6.32:8080/U3dFileToServer/upLoad/WFJ/StreamingAssets.zip";
        StartCoroutine(DownLoadZip(url));
    }

    // Update is called once per frame
    IEnumerator DownLoadZip(string url)
    {
        WWW www = new WWW(url);
        yield return www;

        if (www.isDone && !isUnzipped)
        {
            Debug.Log("Load zip complete");
            byte[] data = www.bytes;

            string persistentPath = Application.persistentDataPath;
            string zipName = "StreamingAssets.zip";
            string docPath;
            docPath = persistentPath + "/" + zipName;
            Debug.Log("docPath=" + docPath);

            System.IO.File.WriteAllBytes(docPath, data);

            UnZip(docPath, persistentPath, "");
        }
    }

    /// <summary>
    /// ZIP:解压一个zip文件
    /// add yuangang by 2016-06-13
    /// </summary>
    /// <param name="ZipFile">需要解压的Zip文件(绝对路径)</param>
    /// <param name="TargetDirectory">解压到的目录</param>
    /// <param name="Password">解压密码</param>
    /// <param name="OverWrite">是否覆盖已存在的文件</param>
    void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
    {
        //如果解压到的目录不存在,则报错
        if (!System.IO.Directory.Exists(TargetDirectory))
        {
            throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
        }
        //目录结尾
        if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }

        using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
        {
            zipfiles.Password = Password;
            ZipEntry theEntry;

            while ((theEntry = zipfiles.GetNextEntry()) != null)
            {
                string directoryName = "";
                string pathToZip = "";
                pathToZip = theEntry.Name;

                if (pathToZip != "")
                    directoryName = Path.GetDirectoryName(pathToZip) + "\\";

                string fileName = Path.GetFileName(pathToZip);

                Directory.CreateDirectory(TargetDirectory + directoryName);

                if (fileName != "")
                {
                    if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
                    {
                        using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = zipfiles.Read(data, 0, data.Length);

                                if (size > 0)
                                    streamWriter.Write(data, 0, size);
                                else
                                    break;
                            }
                            streamWriter.Close();
                        }
                    }
                }
            }
            zipfiles.Close();
        }
        //
        File.Delete(ZipFile);
    }
}

 

Unity游戏资源解压

标签:call   ntp   write   called   done   密码   rtc   overwrite   open   

原文地址:http://www.cnblogs.com/JimmyCode/p/7536763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!