Unity3D_Util_文件管理_Zip

用ICSharpCode.SharpZipLib类库实现压缩并且换回进度:

创建进度参数:

 public class ZipProcessArgs : IEventArgs, IReference
    {
        public void ClearnReference()
        {

        }
        private int id;

        public int Id
        {
            get { return id; }
        }

        public ZipProcessArgs()
        {
            id = RandomUtil.RandomId();
        }
        public float Data;
    }
View Code
public class ZipEndArgs : IEventArgs, IReference
    {
        public void ClearnReference()
        {

        }
        private int id;

        public int Id
        {
            get { return id; }
        }

        public ZipEndArgs()
        {
            id = RandomUtil.RandomId();
        }
        public string Data;
    }
创建完成参数:
public class ZipUtil : IFactory
    {
        private static Dictionary<string, ZipProcessArgs> eventArgs;
        public ZipUtil()
        {
            eventArgs = new Dictionary<string, ZipProcessArgs>();
        }
        [ClassInject]
        private EventManager eventManager { get; set; }
        [ClassInject]
        private TheadPoolUtil theadPoolUtil { get; set; }
        public void ZipProcess(UnityAction<float> zipProcess, string inStr)
        {
            ZipProcessArgs process = FactoryManager.CreatEventClass<ZipProcessArgs>(inStr);
            eventManager.Subscribe(process.Id, (obj, args) =>
            {
                if (args is ZipProcessArgs)
                {
                    var pargs = args as ZipProcessArgs;
                    zipProcess.DefaultAction(pargs.Data);
                }
            });
        }
        public void ZipEnd(UnityAction<string> zipEnd, string inStr)
        {
            ZipEndArgs zipEndArgs = FactoryManager.CreatEventClass<ZipEndArgs>(inStr);
            eventManager.Subscribe(zipEndArgs.Id, (obj, args) =>
            {
                if (args is ZipEndArgs)
                {
                    var pargs = args as ZipEndArgs;
                    zipEnd.DefaultAction(pargs.Data);
                }
            });
        }
        public void ZipFile(string inFile, string outFile, int CompressionLevel = 6, string password = null, string comment = null, int BlockSize = 1024)
        {
            if (!outFile.EndsWith(".zip"))
            {
                outFile += ".zip";
            }
            theadPoolUtil.Run(() =>
            {
                using (var outStream = File.Create(outFile))
                {
                    using (var StreamToZip = new FileStream(inFile, FileMode.Open, FileAccess.Read))
                    {
                        using (ZipOutputStream ZipStream = new ZipOutputStream(outStream))
                        {
                            ZipCore(StreamToZip, ZipStream, CompressionLevel, password, comment, Path.GetFileName(inFile), BlockSize, true);
                            ZipStream.Finish();
                        }
                    }
                }
                ZipProcessArgs process = FactoryManager.CreatEventClass<ZipProcessArgs>(inFile);
                FactoryManager.ClearEventReference(process, inFile);
            }, () =>
            {
                ZipProcessArgs process = FactoryManager.CreatEventClass<ZipProcessArgs>(inFile);
                FactoryManager.ClearEventReference(process, inFile);
                ZipEndArgs zipEndArgs = FactoryManager.CreatEventClass<ZipEndArgs>(inFile);
                zipEndArgs.Data = outFile;
                eventManager.Fire(this, zipEndArgs);
                FactoryManager.ClearEventReference(zipEndArgs, inFile);

            });

        }
        public void ZipDirectory(string inDirectory, string outFile, int CompressionLevel = 6, string password = null, string comment = null, int BlockSize = 1024)
        {
            if (!outFile.EndsWith(".zip"))
            {
                outFile += ".zip";
            }
            theadPoolUtil.Run(() =>
            {
                ZipProcessArgs process = FactoryManager.CreatEventClass<ZipProcessArgs>(inDirectory);
                FileInfo[] fileList = inDirectory.GetDirectoryFile().ToArray();
                using (var outStream = File.Create(outFile))
                {
                    using (ZipOutputStream ZipStream = new ZipOutputStream(outStream))
                    {
                        for (int i = 0; i < fileList.Length; i++)
                        {
                            if (theadPoolUtil.ApplicationRun)
                            {
                                using (var inStream = new FileStream(fileList[i].FullName, FileMode.Open, FileAccess.Read))
                                {
                                    ZipCore(inStream, ZipStream, CompressionLevel, password, comment, fileList[i].FullName.Replace(inDirectory, ""), BlockSize);
                                    SendProcess(process, ((i + 1) * 1f) / fileList.Length);
                                }
                            }
                            else
                            {
                                Debug.LogError("程序停止运行");
                                ZipStream.Finish();
                                return;
                            }

                        }
                        ZipStream.Finish();
                    }

                }
                FactoryManager.ClearEventReference(process, inDirectory);
            }, () =>
            {
                ZipProcessArgs process = FactoryManager.CreatEventClass<ZipProcessArgs>(inDirectory);
                FactoryManager.ClearEventReference(process, inDirectory);
                ZipEndArgs zipEndArgs = FactoryManager.CreatEventClass<ZipEndArgs>(inDirectory);
                zipEndArgs.Data = outFile;
                eventManager.Fire(this, zipEndArgs);
                FactoryManager.ClearEventReference(zipEndArgs, inDirectory);
            });

        }
        public void ZipCore(FileStream inStream, ZipOutputStream ZipStream, int CompressionLevel, string password, string comment, string staticPath, int BlockSize = 1024, bool needPrecoss = false)
        {
            try
            {
                ZipProcessArgs process = FactoryManager.CreatEventClass<ZipProcessArgs>(inStream.Name);
                if (!password.IsNullOrEmpty())
                    ZipStream.Password = password;
                if (!comment.IsNullOrEmpty())
                    ZipStream.SetComment(comment);
                ZipStream.SetLevel(CompressionLevel);
                string tempfile = "";
                if (!staticPath.IsNullOrEmpty())
                    tempfile = staticPath;
                ZipEntry ZipEntry = new ZipEntry(tempfile);
                ZipEntry.DateTime = DateTime.Now;
                ZipEntry.Size = inStream.Length;
                ZipStream.PutNextEntry(ZipEntry);
                byte[] buffer = new byte[BlockSize];
                var size = inStream.Read(buffer, 0, buffer.Length);
                ZipStream.Write(buffer, 0, size);
                if (needPrecoss)
                {
                    SendProcess(process, (size * 1f) / inStream.Length);
                }
                while (size < inStream.Length)
                {
                    if (theadPoolUtil.ApplicationRun)
                    {
                        int sizeRead = inStream.Read(buffer, 0, buffer.Length);
                        ZipStream.Write(buffer, 0, sizeRead);
                        size += sizeRead;
                        if (needPrecoss)
                        {
                            SendProcess(process, (size * 1f) / inStream.Length);
                        }
                    }
                    else
                    {
                        Debug.LogError("程序停止运行");
                        return;
                    }

                }

            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }

        }
        private List<ZipEntry> GetAllZipEntry(string zipFilePath, string unZipDir = null, string password = null)
        {
            List<ZipEntry> list = new List<ZipEntry>();
            ZipEntry theEntry;
            using (FileStream zip = File.OpenRead(zipFilePath))
            {
                using (ZipInputStream s = new ZipInputStream(zip))
                {
                    if (!password.IsNullOrEmpty())
                        s.Password = password;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        list.Add(theEntry);
                    }
                }
            }
            return list;
        }
        public void UnZipFile(string zipFilePath, string unZipDir = null, string password = null, int BlockSize = 1024)
        {
            if (unZipDir.IsNullOrEmpty())
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
            if (!unZipDir.EndsWith("\\"))
                unZipDir += "\\";
            theadPoolUtil.Run(() =>
            {
                ZipProcessArgs process = FactoryManager.CreatEventClass<ZipProcessArgs>(zipFilePath);
                if (!Directory.Exists(unZipDir))
                    Directory.CreateDirectory(unZipDir);
                try
                {
                    using (FileStream zip = File.OpenRead(zipFilePath))
                    {
                        using (ZipInputStream s = new ZipInputStream(zip))
                        {

                            ZipEntry theEntry;
                            List<ZipEntry> list = GetAllZipEntry(zipFilePath, unZipDir, password);
                            int index = 1;
                            while ((theEntry = s.GetNextEntry()) != null)
                            {
                                if (theadPoolUtil.ApplicationRun)
                                {
                                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                                    string fileName = Path.GetFileName(theEntry.Name);
                                    if (directoryName.Length > 0)
                                    {
                                        Directory.CreateDirectory(unZipDir + directoryName);
                                    }
                                    if (!directoryName.EndsWith("\\"))
                                        directoryName += "\\";
                                    if (!fileName.IsNullOrEmpty())
                                    {
                                        using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                                        {

                                            int size = BlockSize;
                                            byte[] data = new byte[BlockSize];
                                            while (true)
                                            {
                                                size = s.Read(data, 0, data.Length);
                                                if (size > 0)
                                                {
                                                    streamWriter.Write(data, 0, size);
                                                }
                                                else
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    SendProcess(process, ((index++) * 1f) / list.Count);
                                }
                                else
                                {
                                    Debug.LogError("程序停止运行");
                                    return;
                                }
                            }
                        }
                    }

                }
                catch (Exception e)
                {
                    Debug.LogError(e);
                    return;
                }
            }, () =>
             {
                 ZipProcessArgs process = FactoryManager.CreatEventClass<ZipProcessArgs>(zipFilePath);
                 FactoryManager.ClearEventReference(process, zipFilePath);
                 ZipEndArgs zipEndArgs = FactoryManager.CreatEventClass<ZipEndArgs>(zipFilePath);
                 zipEndArgs.Data = unZipDir;
                 eventManager.Fire(this, zipEndArgs);
                 FactoryManager.ClearEventReference(zipEndArgs, zipFilePath);
             });

        }
        private void SendProcess(ZipProcessArgs arg, float process)
        {
            if (null != eventManager && null != arg)
            {
                arg.Data = process;
                eventManager.FireOnMainThread(this, arg);
            }
        }
        public void InjectionBind()
        {
        }


    }
主体代码
void Test()
    {
      
        var inPath ="输入文件";
        var outPath ="输出文件" ;
        var zip = FactoryManager.CreatClass<ZipUtil>();
        zip.ZipProcess(process =>
        {
            slider.value = process;
        }, outPath);
        stopwatch = new Stopwatch();
        stopwatch.Start();
        zip.UnZipFile(inPath, outPath);
        zip.ZipEnd(str =>
        {
            UnityEditor.EditorUtility.OpenWithDefaultApp(str);
        }, inPath);
    }
调用实例

 

 

 

创建进度参数:

转载于:https://www.cnblogs.com/PandaHome/p/10948673.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值