asp.net实现文件夹及文件压缩,并实现下载(三)——文件超过150M

来源:互联网 发布:软件测试压力测试 编辑:程序博客网 时间:2024/06/07 00:16

asp.net实现文件夹及文件压缩,并实现下载(三)——文件超过150M

添加引用:ICSharpCode.SharpZipLib.dll

代码:
 protected void Button1_Click(object sender, EventArgs e)
    {
       string filePath = Server.MapPath("files");
        string strFileName = HttpUtility.UrlEncode("files").Replace('+', ' ');
        string fileName = "files.zip";//客户端保存的文件名
        string Path = Server.MapPath(fileName);//路径
        System.IO.FileStream iStream = null;
        try
        {
            zos = new ZipOutputStream(File.Create(filePath + ".zip"));//创建压缩文件,保存的服务器中
            strBaseDir = filePath + "\\";
            addZipEntry(strBaseDir);//压缩文件
            zos.Finish();
            zos.Close();
            //分块下载文件
            iStream = System.IO.File.OpenRead(Path);
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(Path);
            if (fileInfo.Exists == true)
            {
                const long ChunkSize = 1048576;//1M 每次读取文件,只读取1M,这样可以缓解服务器的压力
                byte[] buffer = new byte[ChunkSize];
                Response.Clear();
                long dataLengthToRead = iStream.Length;//获取下载的文件总大小
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
                while (dataLengthToRead > 0 && Response.IsClientConnected)
                {
                    int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
                    Response.OutputStream.Write(buffer, 0, lengthRead);
                    Response.Flush();
                    dataLengthToRead = dataLengthToRead - lengthRead;
                }
                Response.Close();
            }
            zos.Close();
            iStream.Close();
            Response.End();
            Response.Clear();
            Response.End();
        }
        catch (Exception ex)
        {
            if (iStream != null)
                iStream.Close();
            ////zos.Close();
            ScriptManager.RegisterStartupScript(this, GetType(), "alert", "<script>alert('下载有问题!');</script>", false);
        }
    }


    private void addZipEntry(string path)
    {
        Crc32 crc = new Crc32();
        DirectoryInfo di = new DirectoryInfo(path);
        FileStream fs = null;
        try
        {
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                fs = File.OpenRead(item.FullName);
                if (fs.Length != 0)
                {
                    long fileLength = fs.Length;
                    long pai = 1024 * 1024 * 20;//每M写一次
                    long forint = fs.Length / pai + 1;
                    byte[] buffer = null;
                    ZipEntry entry = null;
                    if (fileLength < 104857600)//150 * 1024 * 1024
                    {
                        buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string strEntryName = item.FullName.Replace(strBaseDir, "");
                        entry = new ZipEntry(strEntryName);
                        zos.PutNextEntry(entry);
                        zos.Write(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        entry = new ZipEntry(item.FullName.Replace(strBaseDir, ""));
                        entry.Size = fs.Length;
                        entry.DateTime = DateTime.Now;
                        zos.PutNextEntry(entry);
                        for (long i = 1; i <= forint; i++)
                        {
                            if (pai * i < fs.Length)
                            {
                                buffer = new byte[pai];
                                fs.Seek(pai * (i - 1), System.IO.SeekOrigin.Begin);
                            }
                            else
                            {
                                if (fs.Length < pai)
                                {
                                    buffer = new byte[fs.Length];
                                }
                                else
                                {
                                    buffer = new byte[fs.Length - pai * (i - 1)];
                                    fs.Seek(pai * (i - 1), System.IO.SeekOrigin.Begin);
                                }
                            }
                            fs.Read(buffer, 0, buffer.Length);
                            crc.Reset();
                            crc.Update(buffer);
                            zos.Write(buffer, 0, buffer.Length);
                            zos.Flush();
                        }
                    }
                }
                fs.Close();
            }
        }
        catch (Exception ex)
        {
            if (fs != null)
                fs.Close();
            ScriptManager.RegisterStartupScript(this, GetType(), "alert", "<script>alert('压缩有问题!');</script>", false);
        }
    }

这个方法先是将生成的压缩包存放到服务器中,然后再下载

原创粉丝点击