直接提取压缩包里的文件

一 使用背景:

通过Http 请求下载一个压缩的文件到服务器内存中(重点:不用保存到本地),然后通过代码直接提取压缩包的文件

二 实现思路:(注:需要提前安装 ICSharpCode.SharpZipLib.dll)

直接提取压缩包里的文件

1 通过Http请求下载压缩文件到服务器的内存中

2 读取内存中压缩的包的流(注意先将:Stream 转换成MemoryStream)

3 通过ICSharpCode.SharpZipLib.Zip.dll的ZipFile方法将压缩包的MemoryStream 注入

4 通过文件索引提取压缩包里的文件流

5 保存上传文件到指定位置

三 参考代码:

public string HttpDownloadFile(int tenantId, RequestHeaadModel heaadModel)
        {
            var dfsResultPath = string.Empty;
            var listDfsPath = new List<string>();
            try
            {
                #region Http请求参数设置
                ServicePointManager.Expect100Continue = false;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
                ServicePointManager.ServerCertificateValidationCallback += (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(heaadModel.ResquestParamUrl);
                request.Headers.Add("x-qys-accesstoken", heaadModel.Accesstoken);
                request.Headers.Add("x-qys-timestamp", "0");
                request.Headers.Add("x-qys-signature", heaadModel.Searect);
                request.ContentType = heaadModel.ContentType;
                request.Method = "GET";
                request.Timeout = 100000;
                request.Accept = "application/octet-stream";
                #endregion

                #region 对响应的压缩包解析
                using (WebResponse webRes = request.GetResponse())
                {
                    #region  1 获取响应的压缩包文件流
                    var length = (long)webRes.ContentLength;
                    var response = webRes as HttpWebResponse;
                    var stream = response.GetResponseStream();
                    #endregion

                    #region 2   将压缩包文件流程读取到内存中
                    var stmMemory = new MemoryStream();
                    if (length == -1)
                    {
                        length = 1024;
                    }
                    var buffer = new byte[length];
                    int i;
                    while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        stmMemory.Write(buffer, 0, i);
                    }
                    #endregion

                    #region 3 循环读取压缩包的文件
                    var zipFile_ = new ICSharpCode.SharpZipLib.Zip.ZipFile(stmMemory);
                    int count = int.Parse(zipFile_.Count.ToString());//获取文件个数
                    for (int j = 0; j < count; j++)
                    {
                        var tempSteam = zipFile_.GetInputStream(long.Parse($"{i}"));//压缩包里的文件索引
                        var stmMemory2 = new MemoryStream();
                        var buffer2 = new byte[zipFile_[i].Size];
                        int m;
                        //将单个文件的文件流读取到内存中
                        while ((m = tempSteam.Read(buffer2, 0, buffer2.Length)) > 0)
                        {
                            stmMemory2.Write(buffer2, 0, m);
                        }
                        stmMemory2.Seek(0, SeekOrigin.Begin);
                        var dfsItem = new DfsItem("TenantBaseFile", zipFile_[i].Name,
                            stmMemory2, tenantId);
                        var dfsPath = Dfs.Store(dfsItem);

                        Logger.Debug($"下载背调文件地址:{dfsPath.ToString()}");
                        listDfsPath.Add(dfsPath.ToString());
                        stmMemory2.Close();
                        stmMemory2.Flush();
                    }
                    #endregion
                    stmMemory.Close();
                    stmMemory.Flush();
                }
                #endregion
            }
            catch (Exception ex)
            {
                Logger.Debug($"下载报告异常:异常信息:{ex.Message},堆栈信息:{ex.StackTrace}");
            }

            return string.Join(",", listDfsPath);
        }

原文出处:博客园【卖身也卖艺 】

原文链接:https://www.cnblogs.com/HF520134/p/13390685.html

本文观点不代表Dotnet9立场,转载请联系原作者。

发表评论

登录后才能评论