zoukankan      html  css  js  c++  java
  • Unity游戏资源解压

    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);
        }
    }
  • 相关阅读:
    eclipse如何设置多个字符的智能提示
    19.面向对象的三大特征 之封装
    18代码块
    成员变量和局部变量的区别
    类与对象
    Python压缩脚本编辑
    字符串内容
    参考
    序列
    元组
  • 原文地址:https://www.cnblogs.com/JimmyCode/p/7536763.html
Copyright © 2011-2022 走看看