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

14 篇文章 0 订阅
6 篇文章 0 订阅

 asp.net实现文件夹及文件压缩,并实现下载(二)

添加引用:ICSharpCode.SharpZipLib.dll

代码:
#region 压缩并下载代码
    private void dlZipDir(string strPath, string strFileName)
    {
        MemoryStream ms = null;
        try
        {
            Response.ContentType = "application/octet-stream";
            strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
            Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
            ms = new MemoryStream();
            zos = new ZipOutputStream(ms);
            strBaseDir = strPath + "\\";
            addZipEntry(strBaseDir);
            zos.Finish();
            zos.Close();
            Response.Clear();
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }
        catch (Exception ex)
        {
            if (ms != null)
                ms.Close();
            if (zos != null)
                zos.Close();
        }
    }

    private void addZipEntry(string path)
    {
        Crc32 crc = new Crc32();
        FileStream fs = null;
        try
        {
            DirectoryInfo di = new DirectoryInfo(path);
            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;//每20M写一次
                    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  //超过150M则会分块处理
                    {
                        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();
        }
    }
调用 
protected void Button1_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("files");
        dlZipDir(filePath, "files");
    }
该方法不会在服务器中保存压缩文件
文件若超过150M则会分块压缩文件。虽然能压缩大型的文件,但大型文件过大或者整体的文件过大 ,也会报错,如:引发类型为“System.OutOfMemoryException”的异常。
其解决方案请看asp.net实现文件夹及文件压缩,并实现下载(三)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值