zoukankan      html  css  js  c++  java
  • 使用C#下载网络文件

    下载

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="URL">下载文件地址</param>
    /// <param name="Filename">下载后另存为(全路径)</param>
    private bool DownloadFile(string URL, string filename)
    {
        try
        {
            System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(URL);
            System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse) Myrq.GetResponse();
            System.IO.Stream st = myrp.GetResponseStream();
            System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
            byte[] by = new byte[1024];
            int osize = st.Read(by, 0, (int) by.Length);
            while (osize > 0)
            {
                so.Write(by, 0, osize);
                osize = st.Read(by, 0, (int) by.Length);
            }
            so.Close();
            st.Close();
            myrp.Close();
            Myrq.Abort();
            return true;
        }
        catch (System.Exception e)
        {
            return false;
        }
    }

    解压Zip文件

    下面代码主要用到了SharpZipLib的库

    private string UnZip(string entryname, ZipFile zf)
    {
        var cover = zf.GetEntry(entryname);
        if (cover == null)
        {
            return null;
        }
        string filepath = System.AppDomain.CurrentDomain.BaseDirectory + @"pic";
        string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + RY.Common.DirectoryAndFile.GetFileExt(entryname);
    
        string name = filepath + filename;
        using (FileStream writer = System.IO.File.Create(name)) //解压后的文件
        {
            var stream = zf.GetInputStream(cover);
            int bufferSize = 1024 * 2; //缓冲区大小
            int readCount = 0; //读入缓冲区的实际字节
            byte[] buffer = new byte[bufferSize];
            readCount = stream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                writer.Write(buffer, 0, readCount);
                readCount = stream.Read(buffer, 0, bufferSize);
            }
            writer.Close();
        }
        return "pic/" + filename;
    }
  • 相关阅读:
    Android 入门到精通 (Index)
    负载平衡与冗余备份方案概述
    Android 程序组件交互分析
    复制时保留文件的目录结构
    notepad++中设置tab缩进的宽度
    scws
    php 将字符(包括汉字) 转换成16进制 (apache access log 中文显示16进制码)
    批量修改完整版本
    根据端口号查进程
    php性能优化
  • 原文地址:https://www.cnblogs.com/smallerpig/p/3646219.html
Copyright © 2011-2022 走看看