解压我用的第三方库lonic.zip,不喜欢也可以使用unity自己的
[code]csharpcode:

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using UnityEngine;
using Ionic.Zip;

public class SynUpdateCourse : MonoBehaviour
{
    //文件名称(包含路径)
    public List<string> fileNames = new List<string>();
    void OnGUI() {
        if (GUI.Button(new Rect(0,0,200,60),"下载"))
        {
            fileNames = GetSpecificTypeFile("//ip/共享文件名","zip");
            td = new Thread(Download);
            td.Start();
        }
    }

   
    Thread td;
    //本地文件路径
    string localPath = @"E:/同步测试";
    //下载文件路径保存
    public List<string> newFile = new List<string>();
    //下载
    void Download() {
        List<string> localFile = Director(localPath);
        
        for (int i = 0; i < fileNames.Count; i++)
        {
            string fileName = Path.GetFileName(fileNames[i]);
            string[] str = fileName.Split('.');//提取文件名(去除后缀)
            if (!localFile.Contains(str[0]))//如果本地文件不包含此压缩包名称
            {
                UnityEngine.Debug.Log(str[0]);
                newFile.Add(localPath+"/"+ fileName);//添加需解压文件
                File.Copy(fileNames[i], localPath + "/" + fileName);//从服务器复制到本地
            }
        }
        UnityEngine.Debug.Log("下载完成");
        Uncompress(newFile);
        td.Abort();
    }
    int index=0;//递归下标
    /// <summary>
    /// 解压文件
    /// </summary>
    /// <param name="files">解压文件路径集合</param>
    void Uncompress(List<string> files) {
        using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(files[index]))
        {
            //zip.Password = "111111";//压缩文件密码没有则注视本行
            zip.ExtractProgress += (s, e) => {
                Zip_SaveProgress(s, e);
            };
            zip.ExtractAll(localPath, ExtractExistingFileAction.InvokeExtractProgressEvent);//解压至本地文件夹路径
            zip.Dispose();
        }
    }
    
    /// <summary>
    /// 解压进度监听事件,不是很好用,但是可以确认是否解压结束
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Zip_SaveProgress(object sender, ExtractProgressEventArgs e)
    {
        string progress = "";
        if (e.EventType == ZipProgressEventType.Extracting_AfterExtractAll)
            progress = e.EntriesExtracted.ToString() + "/" + e.EntriesTotal.ToString();
        UnityEngine.Debug.Log("解压中"+progress);
        if (e.EntriesExtracted!=0&& e.EntriesTotal!=0&&e.EntriesExtracted==e.EntriesTotal)
        {
            UnityEngine.Debug.Log("已解压"+(index+1)+"个");
            index++;
            Uncompress(newFile);
        }
    }
    /// <summary>
	/// 获取文件夹下的文件夹名称列表
	/// </summary>
	/// <param name="path">文件夹路径</param>
	/// <returns></returns>
	public static List<string> Director(string path)
    {
        List<string> fileName = new List<string>();
        DirectoryInfo d = new DirectoryInfo(path);

        DirectoryInfo[] directs = d.GetDirectories();//文件夹

        //获取子文件夹内的文件列表,递归遍历  
        foreach (DirectoryInfo dd in directs)
        {
            fileName.Add(dd.Name);
        }
        return fileName;
    }
    /// <summary>
    /// 获取特定类型文件
    /// </summary>
    /// <param name="path">查询目录路径</param>
    /// <param name="type">类型</param>
    public List<string> GetSpecificTypeFile(string path,string type) {
       return  Directory.GetFiles(path,"*."+type).ToList();
    }
  
}

Unity打包后解压失败的话可以去Unity安装目录找到几个I18开头的dll文件,放到工程里,就好了

posted on 2021-10-09 19:02  陌洛  阅读(10)  评论(0编辑  收藏  举报