当前位置: 首页 > news >正文

承德市网站开发北京最新疫情情况

承德市网站开发,北京最新疫情情况,wordpress 美女图片,2022年中国电商行业分析报告1、简单介绍: 用的是一个插件SharpZipLib,在vs的Nuget下载,也可以去github下载https://github.com/icsharpcode/SharpZipLib 用这个最主要的是因为,这个不用请求windows的文件读写权限,关于这个权限我搞了好久&#…
1、简单介绍:

用的是一个插件SharpZipLib,在vs的Nuget下载,也可以去github下载https://github.com/icsharpcode/SharpZipLib

用这个最主要的是因为,这个不用请求windows的文件读写权限,关于这个权限我搞了好久,到最后还不如这个好用 

2、开始安装 

这里介绍一下vs里的下载,首先在unity中随便创建一个脚本,然后双击打开进入vs,在上方工具栏中选“项目-管理NuGet程序包

 然后会出现一个界面

 点击上方的浏览-在搜索框中直接搜索SharpZipLib,然后会出现一堆的东西,不要犹豫,就是第一个(应该是吧),点击安装

 安装好后进入unity,你会发现我们安装的.dll并不在unity工程中,那是因为vs给我们下载到了Packages中,我们进入Packages,找插件名称的文件夹,进去后选lib文件夹,然后根据自己用的API版本,拖不同的SharpZipLib.dll进unity中,拿我的举例,我的是.net2.1,那我就拿net2.1进去

 

 至此,引用完成

3、简单使用

我们在unity中创建zip.cs脚本

using ICSharpCode.SharpZipLib.Checksum;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;namespace VRS.Util
{public class Zip{/// <summary>/// 压缩文件夹/// </summary>/// <param name="dirPath">压缩文件夹的路径</param>/// <param name="fileName">生成的zip文件路径</param>/// <param name="level">压缩级别 0 - 9 0是存储级别 9是最大压缩</param>/// <param name="bufferSize">读取文件的缓冲区大小</param>public void CompressDirectory(string dirPath, string fileName, int level, int bufferSize){byte[] buffer = new byte[bufferSize];using (ZipOutputStream s = new ZipOutputStream(File.Create(fileName))){s.SetLevel(level);CompressDirectory(dirPath, dirPath, s, buffer);s.Finish();s.Close();}}/// <summary>/// 压缩目录/// </summary>/// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>/// <param name="ZipedFile">压缩后的文件名,全路径格式</param>/// <returns></returns>public bool ZipFileDictory(string FolderToZip, string ZipedFile, string Password = ""){bool res;if (!Directory.Exists(FolderToZip))return false;ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));s.SetLevel(6);if (!string.IsNullOrEmpty(Password.Trim()))s.Password = Password.Trim();res = ZipFileDictory(FolderToZip, s, "");s.Finish();s.Close();return res;}/// <summary>/// 递归压缩文件夹方法/// </summary>/// <param name="FolderToZip"></param>/// <param name="s"></param>/// <param name="ParentFolderName"></param>private bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName){bool res = true;string[] folders, filenames;ZipEntry entry = null;FileStream fs = null;Crc32 crc = new Crc32();try{//创建当前文件夹entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));  //加上 “/” 才会当成是文件夹创建s.PutNextEntry(entry);s.Flush();//先压缩文件,再递归压缩文件夹filenames = Directory.GetFiles(FolderToZip);foreach (string file in filenames){//打开压缩文件fs = File.OpenRead(file);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));entry.DateTime = DateTime.Now;entry.Size = fs.Length;fs.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;s.PutNextEntry(entry);s.Write(buffer, 0, buffer.Length);}}catch{res = false;}finally{if (fs != null){fs.Close();fs = null;}if (entry != null)entry = null;GC.Collect();GC.Collect(1);}folders = Directory.GetDirectories(FolderToZip);foreach (string folder in folders){if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))return false;}return res;}/// <summary>/// 压缩文件夹/// </summary>/// <param name="root">压缩文件夹路径</param>/// <param name="path">压缩文件夹内当前要压缩的文件夹路径</param>/// <param name="s"></param>/// <param name="buffer">读取文件的缓冲区大小</param>private void CompressDirectory(string root, string path, ZipOutputStream s, byte[] buffer){//root = root.TrimEnd('//') + "//";string[] fileNames = Directory.GetFiles(path);string[] dirNames = Directory.GetDirectories(path);string relativePath = path.Replace(root, "");if (relativePath != ""){relativePath = relativePath.Replace("//", "/") + "/";}int sourceBytes;foreach (string file in fileNames){ZipEntry entry = new ZipEntry(relativePath + Path.GetFileName(file));entry.DateTime = DateTime.Now;s.PutNextEntry(entry);using (FileStream fs = File.OpenRead(file)){do{sourceBytes = fs.Read(buffer, 0, buffer.Length);s.Write(buffer, 0, sourceBytes);} while (sourceBytes > 0);}}foreach (string dirName in dirNames){string relativeDirPath = dirName.Replace(root, "");ZipEntry entry = new ZipEntry(relativeDirPath.Replace("//", "/") + "/");s.PutNextEntry(entry);CompressDirectory(root, dirName, s, buffer);}}/// <summary>/// 解压缩zip文件/// </summary>/// <param name="zipFilePath">解压的zip文件路径</param>/// <param name="extractPath">解压到的文件夹路径</param>/// <param name="bufferSize">读取文件的缓冲区大小</param>public void Extract(string zipFilePath, string extractPath, int bufferSize){//extractPath = extractPath.TrimEnd('//') + "//";byte[] data = new byte[bufferSize];int size;using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath))){ZipEntry entry;while ((entry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(entry.Name);string fileName = Path.GetFileName(entry.Name);//先创建目录if (directoryName.Length > 0){Directory.CreateDirectory(extractPath + directoryName);}if (fileName != String.Empty){using (FileStream streamWriter = File.Create(extractPath + entry.Name.Replace("/", "//"))){while (true){size = s.Read(data, 0, data.Length);if (size > 0){streamWriter.Write(data, 0, size);}else{break;}}}}}}}public void CreateZip(string sourceFilePath, string destinationZipFilePath){if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)sourceFilePath += System.IO.Path.DirectorySeparatorChar;ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));zipStream.SetLevel(6);  // 压缩级别 0-9CreateZipFiles(sourceFilePath, zipStream, sourceFilePath);zipStream.Finish();zipStream.Close();}/// <summary>/// 递归压缩文件/// </summary>/// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>/// <param name="zipStream">打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名</param>/// <param name="staticFile"></param>private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile){Crc32 crc = new Crc32();string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);foreach (string file in filesArray){if (Directory.Exists(file))                     //如果当前是文件夹,递归{CreateZipFiles(file, zipStream, staticFile);}else                                            //如果是文件,开始压缩{FileStream fileStream = File.OpenRead(file);byte[] buffer = new byte[fileStream.Length];fileStream.Read(buffer, 0, buffer.Length);string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);ZipEntry entry = new ZipEntry(tempFile);entry.DateTime = DateTime.Now;entry.Size = fileStream.Length;fileStream.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;zipStream.PutNextEntry(entry);zipStream.Write(buffer, 0, buffer.Length);}}}}
}

然后我们随便创建一个脚本测试

public class Test_ZipWrapper : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){Zip zip = new Zip();//zip.CompressDirectory(, "D:/PowerModel/新建文件夹", 5,2048);zip.ZipFileDictory("D:/PowerModel/新建文件夹", "D:/PowerModel/新建文件夹.zip");//zip.Extract("D:/PowerModel/新建文件夹.zip", "D:/PowerModel/",2048);}}

在这里我们主要调用的是ZipFileDictory方法,两个固定参数和一个可选参数,一个是要打压缩包的路径,一个是压缩包保存的路径,可选参数就是压缩包是否要上密码,该代码随便挂一个物体上运行,可以看到我们成功了

 解压的话是用Extract,还是三个参数,要解压的压缩包路径、解压后的路径、和缓冲区,这个缓冲区和我们的TCP读流的缓冲区差不多,随便填,我这里填的是2048,你可以填别的数字,运行后,我们成功了

 大家快去试试吧!

来源:Unity 工具 之 (SharpZipLib) 实现文件Zip的压缩和解压((可代密码)可一次压缩多个文件/文件夹)-蒲公英云

http://www.yidumall.com/news/69519.html

相关文章:

  • 基于b s结构做的网站百度搜索下载app
  • 做网站作业什么主题做seo用哪种建站程序最好
  • 房产信息门户网站建设方案做一个个人网站
  • 东莞微网站十大网站排行榜
  • 怎么制作网站源码怎么去推广自己的店铺
  • 建做网站百度网盘官网登录入口
  • 电子商务网站推广的方式有哪些如何联系百度平台客服
  • 微信分销网站开发郑州seo优化外包顾问
  • 如何建立和设置公司网站搜索优化指的是什么
  • 麦包包网站建设特点怎么网络推广自己业务
  • 山西公司网站建设百度小程序入口
  • 义乌做网站的公司简述网络营销的主要方法
  • 天津网站建设公司关键词搜索方法
  • 呼伦贝尔旅游包车网站咋做百度大全免费下载
  • jsp网站制作详细教程网站源码建站
  • mip网站案例怎样在百度打广告
  • 慧聪网怎样做网站友情链接专业seo推广
  • 做软件的软件广东seo快速排名
  • 做网站首页图片微信广告投放收费标准
  • 影响网站权重的因素代码编程教学入门
  • 建站软件免费模板竞价推广教程
  • 微信h5广西seo
  • 继续访问这个网站怎么快速推广自己的产品
  • 网站前台框架下载百度app免费下载安装
  • 深圳做网站好的公司网站推广和优化的原因网络营销
  • 品牌建设文案seo推广方式是什么呢
  • 网站建设完工报告泉州百度seo公司
  • 做网站的公司名字互联网推广怎么找客户
  • 加密的网站使用jmeter做压测seo技术外包公司
  • 手机做服务器建网站常州网站建设制作