当前位置: 移动技术网 > IT编程>开发语言>.net > 文件的批量打包下载

文件的批量打包下载

2019年04月25日  | 移动技术网IT编程  | 我要评论

南山网,四氯化硅水解,徐明 温如春

注意:首先引入dll文件icsharpcode.sharpziplib.dll

压缩文件

    /// <summary>
    /// 压缩文件
    /// </summary>
    /// <param name="filename">要压缩的所有文件(完全路径)</param>
    /// <param name="filename">文件名称</param>
    /// <param name="name">压缩后文件路径</param>
    /// <param name="level">压缩级别</param>
    public void zipfilemain(string[] filenames, string[] filename, string name, int level)
    {
        zipoutputstream s = new zipoutputstream(file.create(name));
        crc32 crc = new crc32();
        //压缩级别
        s.setlevel(level); // 0 - store only to 9 - means best compression
        try
        {
            int m = 0;
            foreach (string file in filenames)
            {
                //打开压缩文件
                filestream fs = file.openread(file);//文件地址
                byte[] buffer = new byte[fs.length];
                fs.read(buffer, 0, buffer.length);
                //建立压缩实体
                zipentry entry = new zipentry(filename[m].tostring());//原文件名
                //时间
                entry.datetime = datetime.now;
                //空间大小
                entry.size = fs.length;
                fs.close();
                crc.reset();
                crc.update(buffer);
                entry.crc = crc.value;
                s.putnextentry(entry);
                s.write(buffer, 0, buffer.length);
                m++;
            }
        }
        catch
        {
            throw;
        }
        finally
        {
            s.finish();
            s.close();
        }
    }

文件下载

//下载打包文件
    private void downloadfile(string filename, string filepath)
    {
        fileinfo fileinfo = new fileinfo(filepath);
        response.clear();
        response.clearcontent();
        response.clearheaders();
        response.addheader("content-disposition", "attachment;filename=" + filename);
        response.addheader("content-length", fileinfo.length.tostring());
        response.addheader("content-transfer-encoding", "binary");
        response.contenttype = "application/octet-stream";
        response.contentencoding = system.text.encoding.getencoding("gb2312");
        response.writefile(fileinfo.fullname);
        response.flush();
        file.delete(filepath);//删除已下载文件
        response.end();
    }

具体使用

protected void btndownloadfiles_click(object sender, eventargs e)
    {
        list<string> listfj = new list<string>();//保存附件路径
        list<string> listfjname = new list<string>();//保存附件名字
        for (int i = 0; i < gridview.rows.count; i++)
        {
            htmlinputcheckbox chk = (page.master.findcontrol("contentplaceholder1").findcontrol("gridview") as gridview).rows[i].findcontrol("checkboxname") as htmlinputcheckbox;
            if (chk != null && chk.checked)
            {
                string temp = gridview.rows[i].cells[14].text.tostring().trim();
                if (temp != " ")
                {
                    listfj.add(server.mappath("~") + temp);
                    listfjname.add(temp);
                }
            }
        }
        string time = datetime.now.ticks.tostring();
        zipfilemain(listfj.toarray(), listfjname.toarray(), server.mappath("~/uploadfiles/" + time + ".zip"), 9);//压缩文件
        downloadfile(server.urlencode("附件.zip"), server.mappath("~/uploadfiles/" + time + ".zip"));//下载文件
    }

  

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网