.net 多文件压缩下载

 /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="tartgetFile">目标文件路径</param>
        /// <param name="sourceFile">源文件夹路径</param>
        private string Zip(string tartgetFile,string sourceFile)
        {
            using (ZipOutputStream ComStream = new ZipOutputStream(File.Create(tartgetFile)))
            {
                ComStream.SetLevel(6);      //压缩等级
                if (!Directory.Exists(sourceFile))
                    return "不存在目录";
                //压缩文件 有同名压缩文件会自动替换                    
                if (ZipFloder(sourceFile, ComStream, sourceFile))
                {
                    return Download(tartgetFile);
                }
            }
            return "失败";
        }

/// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="_OfloderPath"></param>
        /// <param name="zos"></param>
        /// <param name="_floderPath"></param>
        private bool ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)
        {
            try
            {
                foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())
                {
                    if (Directory.Exists(item.FullName))
                    {
                        ZipFloder(_OfloderPath, zos, item.FullName);
                    }
                    else if (File.Exists(item.FullName))//如果是文件  
                    {
                        DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
                        string fullName2 = new FileInfo(item.FullName).FullName;
                        string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录  
                        FileStream fs = File.OpenRead(fullName2);
                        byte[] bts = new byte[fs.Length];
                        fs.Read(bts, 0, bts.Length);
                        ZipEntry ze = new ZipEntry(path);
                        zos.PutNextEntry(ze);             //为压缩文件流提供一个容器  
                        zos.Write(bts, 0, bts.Length);  //写入字节 
                        fs.Dispose();
                        fs.Close();
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }           
        }

/// <summary>
        /// 文件下载  
        /// </summary>
        /// <param name="fileRpath">文件路径</param>
        /// <returns></returns>
        public string Download(string fileRpath)
        {
            try
            {
                HttpContext context = HttpContext.Current;
                string name = Path.GetFileName(fileRpath);
                FileStream files = new FileStream(fileRpath, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] byteFile = null;
                if (files.Length == 0)
                    byteFile = new byte[1];
                else
                    byteFile = new byte[files.Length];
                files.Read(byteFile, 0, byteFile.Length);
                files.Close();
                context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(name, Encoding.UTF8));
                //context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, Encoding.UTF8));
                context.Response.ContentType = "application/octet-stream;charset=gbk";
                context.Response.BinaryWrite(byteFile);
                context.Response.End();
            }
            catch (Exception e)
            {
                return  e.Message ;
            }
            return  "成功" ;
        }

猜你喜欢

转载自blog.csdn.net/cy520ta/article/details/82688749