• 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);
        }
    }
  • 相关阅读:
    JAVA下使用 连接sqlserver 驱动包
    Windows 7 、Windows Server 2008 和 Windows Server 2008 R2 的支持结束
    VBoxManage命令详解
    端口扫描之王——nmap入门精讲
    rehat-server7常见服务安装与配置总结
    mysql的安装和密码管理、mysql初始密码查找、密码修改、mysql登录
    vim常用命令总结 (转)
    关于《Python绝技:运用Python成为顶级黑客》的学习笔记
    常用MySQL图形化管理工具
    Chrome谷歌浏览器离线安装包下载
  • 原文地址:https://www.cnblogs.com/JimmyCode/p/7536763.html
Copyright © 2020-2023  润新知