C# 下载文件2

从服务下载压缩包 过程

发起请求 HttpWebRequest

断点续传 HttpWebRequest.AddRange()

获取服务资源的响应 HttpWebResponse

设置下载进度条 

解压压缩包 ZipFile

using System;
using System.IO;
using System.IO.Compression;
using System.Net;

namespace Test01
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream localFileStream = null;
            Stream responseStream = null;

            string url = "http://DotNet.test/Dome.zip";

            //下载到本地文件的路径
            string localpath = System.AppDomain.CurrentDomain.BaseDirectory + "Demo.zip";

            try
            {
                //想服务发出请求。
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

                webRequest.Timeout = 10000;

                #region 断点续传

                //断点续传:如果文件存在,就获取文件的大小 N 单位(字节)
                long localFileLength = 0;

                //检查本地文件信息
                FileInfo info = new FileInfo(localpath);
                if (info.Exists)
                {
                    localFileLength = info.Length;
                    webRequest.AddRange(localFileLength);
                    localFileStream = info.Open(FileMode.Append, FileAccess.Write);
                }
                else
                {
                    localFileStream = info.Create();
                }

                #endregion

                //获取服务器资源的响应
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                //获取请求返回的内容长度(压缩包的总体积)
                long totalBytes = webResponse.ContentLength;

                //获取响应流
                webResponse.GetResponseStream();

                byte[] by = new byte[1024];

                //获取字节数量
                int osize = responseStream.Read(by, 0, by.Length);

                //已下载数量 
                long tocalDownloadedByte = 0;

                //下载进度
                double percent = 0;

                while (osize > 0)
                {
                    先检查是否有按键请求,如果有,判断是否为回车键,如果是则退出循环                
                    //if (Console.KeyAvailable && System.Console.ReadKey(true).Key == ConsoleKey.Enter)
                    //{
                    //isBreak = true;
                    //break;
                    //}

                    tocalDownloadedByte += osize;

                    //项文件写入
                    localFileStream.Write(by, 0, osize);

                    percent = tocalDownloadedByte / (float)totalBytes * 100;

                    osize = responseStream.Read(by, 0, by.Length);

                    Console.SetCursorPosition(0, 0);
                    Console.WriteLine($"下载进度:{ (percent).ToString("F2") }% ");
                }
                localFileStream.Close();
                responseStream.Close();
                Console.SetCursorPosition(0, 1);

                Console.WriteLine("下载完成!");


                #region 解压文件

                string targetfolderName = localpath.Replace("Demo.zip", string.Empty);

                ZipFile.ExtractToDirectory(localpath, targetfolderName);//解压

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine($"错误:{ ex.Message }");
            }
            finally
            {
                if (localFileStream != null)
                {
                    localFileStream.Close();
                }
                if (responseStream != null)
                {
                    responseStream.Close();
                }
            }

            Console.ReadKey();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值