下载

/// <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;
}
posted on 2014-02-24 17:25  smallerpig  阅读(557)  评论(0编辑  收藏  举报