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

news/2024/10/25 12:22:17/

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 initializationvoid Start(){xiazaichenggong = false;zhuomianlujing();StartCoroutine(Wait_LoadDown("UnityWWW下载压缩包并压缩到桌面", url));}// Update is called once per framevoid 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/// <summary>/// 下载压缩包/// </summary>/// <param name="ZipID"></param>/// <param name="url"></param>/// <returns></returns>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);}}}/// <summary> /// 解压功能(下载后直接解压压缩文件到指定目录) /// </summary> /// <param name="wwwStream">www下载转换而来的Stream</param> /// <param name="zipedFolder">指定解压目标目录(每一个Obj对应一个Folder)</param> /// <param name="password">密码</param> /// <returns>解压结果</returns> 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      AndroidfileName = fileName.Replace('\\', '/');if (fileName.EndsWith("/")){Directory.CreateDirectory(fileName);continue;}#endregionfs = 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);//解决读取不完整情况}elsebreak;}}}}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;}

http://www.ppmy.cn/news/319187.html

相关文章

【解压zip.00**压缩包】

【解压zip.00**压缩包】 1 解压 .zip.001 .zip.002 .zip.0031.1 方法一【网上的方法&#xff0c;亲测未成功】1.2 方法二【win11系统下&#xff0c;亲测成功】 2 解压 ***.part1.rar3 解压****.tar.gz.0 ****.tar.gz.&#xff11; ****.tar.gz.3 … 1 解压 .zip.001 .zip.002 …

7z制作自解压安装包(转载)

像7z和winRAR这样的压缩工具都支持制作自解压的文件。所谓自解压的文件就是不需要目标机器上安装解压工具&#xff0c;通过运行压缩包自己即可解压出压缩包中的文件。下面我们就介绍一下如何利用7z的自解压功能制作应用程序安装包。 熟悉应用程序安装的朋友应该清楚&#xff0…

第一章 OAuth2.0规范(史上最详细解释)——介绍

目录 一、简介 二、角色 二、协议流程 三、授权许可 1、授权码 2、隐式许可 3、资源所有者密码凭据 4、客户端凭据 四、访问令牌 五、刷新令牌 六、TLS版本 七、HTTP重定向 八、互操作性 九、符号约定 一、简介 在传统的客户端-服务器身份验证模式中&#xff0c;客…

什么是响应式设计

也叫响应式布局&#xff0c;响应式开发 实现不同屏幕分辨率的终端上浏览网页的不同展示方式。 响应式布局是根据设备屏幕宽度不同适当调整标签显示的布局&#xff0c;从而在每种设备屏幕宽度下呈现的界面是不同的 响应式布局 原理&#xff1a; 一个网站适配所有终端&#x…

【Redis总结-基础入门】

第一章 Redis基础 Redis总结-基础入门 第一章 Redis基础学习目标&#xff1a;1. Redis 简介1.1 NoSQL概念1.1.1 问题现象1.1.2 NoSQL的概念 1.2 Redis概念1.2.1 redis概念1.2.2 redis的应用场景 1.3 Redis 的下载与安装1.3.1 Redis 的下载与安装 1.4 Redis服务器启动1.4.1 Redi…

电脑常用快捷键大全(含Visual Studio快捷键操作)

目录 一、系统快捷键 二、常用系统命令 三、QQ快捷键 四、IE浏览器快捷键使用大全 五、Word 中的快捷键 六、Excel 中的快捷键 七、PowerPoint 中的快捷键 八、Visual Studio 九、Eclipse 十、Photoshop 十一、批处理指令 十二、安装操作系统 &#xff08;此快捷操作对我们程…

python爬虫用什么电脑好_【Python】【爬虫】最近想买电脑,用Python爬取京东评论做个参考...

最近想换电脑&#xff0c;又有点不确定买哪一款。所以决定爬取京东上电脑评论&#xff0c;做个参考&#xff0c;并把最终结果绘制成词云图。 一、先来看下三款电脑评价的词云图 1 用Python爬取京东自营ThinkPad T490s的评论绘制成词云图 2 用Python爬取京东自营MacBook Pro的…

Volatility2安装使用以及CTF比赛题目(复现)

Volatility2安装使用以及CTF比赛题目&#xff08;复现&#xff09; 一 、简介二 、安装Volatility三 、安装插件四 、工具介绍五 、使用方法1.系统基本信息&#xff08;imageinfo&#xff09;2. 用户名密码信息&#xff08;hashdump&#xff09;3. 查看进程或隐藏或解链的进程&…