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); }