C# 解压缩ZIP文件

复制代码
void Main()
{    
    var files = ExtractZip(@"云阅卷V5.0.4.1_Alpha_20201015.zip",@"程序包/firstelite/publish/OMS",@"E:\Desktop\test");
    files.Count.Dump();
    files.Dump();
}

List<string> ExtractZip(string zipFilePath, string relativePath, string destPath)
{
    var result = new List<string>();
    
    relativePath = relativePath.Replace(@"\",@"/");
    
    using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Open))
    {
        using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
        {
            var entry = archive.Entries.FirstOrDefault(x=>x.FullName.ToUpper() == relativePath.ToUpper());
            if(entry == null)
                entry = archive.Entries.FirstOrDefault(x => x.FullName.ToUpper() == (relativePath + "/").ToUpper());
                
            if (!string.IsNullOrWhiteSpace(entry.Name))
            {
                var path = Path.Combine(destPath,entry.Name);
                using (var file = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    entry.Open().CopyTo(file);
                    file.Close();
                }
                result.Add(path);
            }
            else
            {
                var items = archive.Entries.Where(x => x.FullName.StartsWith(entry.FullName)).ToList();
                foreach (var item in items.Where(x => string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Length))
                {
                    var path = Path.Combine(destPath, item.FullName.Substring(entry.FullName.Length));
                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                }

                foreach (var item in items.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Length))
                {
                    var path = new FileInfo( Path.Combine(destPath, item.FullName.Substring(entry.FullName.Length))).Directory.FullName;
                    path = Path.Combine(path, item.Name);
                    using (var file = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        item.Open().CopyTo(file);
                        file.Close();
                    }
                    result.Add(path);
                }
            }
        }
    }
    
    return result;
}

// Define other methods and classes here
复制代码

写入zip

复制代码
var files = new Dictionary<string, byte[]>();
            var utf8WithBom = new UTF8Encoding(true);
            var classPlanResult = string.Empty;
            using (var db = new DATAEntities())
            {
                db.Database.CommandTimeout = 3600;
                var classPlanID = this.CurrentClassPlanID;
                var classPlan = db.ClassPlan.FirstOrDefault(item => item.ID == classPlanID);
                classPlanResult = classPlan.FixedFormalDivideResult;
            }
            files.Add("教学班分班规划参数1.json", utf8WithBom.GetBytes(classPlanResult));

            var json = generateNonadministraviePredictClassPlanParameter(chkLessonHourOptimize1.Checked);
            files.Add("教学班分班规划参数2.json", utf8WithBom.GetBytes(json));

            using (var ms = new MemoryStream())
            {
                using (var zip = new ZipArchive(ms, ZipArchiveMode.Create,true))
                {
                    foreach (var file in files)
                    {
                        var item = zip.CreateEntry(file.Key);
                        using (var s = item.Open())
                        {
                            s.Write(file.Value, 0, file.Value.Length);
                        }
                    }
                }

                renderDownloadFile("教学班分班规划传入参数.zip", ms);
            }
复制代码

 

posted on   空明流光  阅读(2813)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2017-11-10 asp.net IE11 dopostback is null or undefined 为空或未定义
2015-11-10 asp.net httpmodule 访问页面控件 备忘

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示