zoukankan      html  css  js  c++  java
  • C#使用SharpZipLib解压多文件的zip压缩文件数据流,保存到本地

    代码:

    public async Task<ReturnModel<List<string>>> UploadModel()
            {
                var task = new Task<ReturnModel<List<string>>>(() =>
                {
                    var rData = new ReturnModel<List<string>>() { code=101};
                    var uploadDir = HttpContext.Current.Server.MapPath("/Content/Resource/ObjModel/");
                    var newFileName = "";
                    var sp = new MultipartMemoryStreamProvider();
                    Task.Run(async () => await Request.Content.ReadAsMultipartAsync(sp)).Wait();
                    foreach (var item in sp.Contents)
                    {
                        if (item.Headers.ContentDisposition.FileName != null)
                        {
                            var filename = item.Headers.ContentDisposition.FileName.Replace(""", "");
                            newFileName = uploadDir + "\" + filename;
                            var fStream = item.ReadAsStreamAsync().Result;//File.OpenRead(zipFilePath)打开本地压缩文件解压
                            using (var s = new ZipInputStream(fStream))
                            {
                                ZipEntry theEntry;
                                while ((theEntry = s.GetNextEntry()) != null)
                                {
                                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                                    string fileName = Path.GetFileName(theEntry.Name);
                                    if (!string.IsNullOrEmpty(directoryName))
                                    {
                                        Directory.CreateDirectory(uploadDir + directoryName);
                                    }
                                    
                                    if (fileName != String.Empty)
                                    {
                                        using (System.IO.FileStream streamWriter = File.Create(uploadDir + theEntry.Name))
                                        {
    
                                            int size;
                                            byte[] data = new byte[2048];
                                            while (true)
                                            {
                                                size = s.Read(data, 0, data.Length);
                                                if (size > 0)
                                                {
                                                    streamWriter.Write(data, 0, size);
                                                }
                                                else
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
    
                    return rData;
                });
                task.Start();
                return await task.ContinueWith(t => t.Result);
            }
  • 相关阅读:
    HDU 3537
    POJ 1175
    POJ 1021 人品题
    POJ 2068
    POJ 2608
    POJ 2960
    poj 1635
    ustc 1117
    ural 1468
    数字游戏
  • 原文地址:https://www.cnblogs.com/xiaojitui/p/11906161.html
Copyright © 2011-2022 走看看