C# 下载Zip

            string ZipPath = ConfigurationManager.AppSettings["ZipPath"];
            string FilePath = ConfigurationManager.AppSettings["FilePath"];
            string ZipUrl = ZipPath + owner_id + ".zip";
            string FolderUrl = ZipPath + owner_id;
if (!File.Exists(ZipUrl))
            {
                //创建文件夹  因为文件夹必定不存在所以不判断
                Directory.CreateDirectory(FolderUrl);
                foreach (var value in fileList)
                {
                    FileInfo f = new FileInfo(FilePath + value);
                    File.Copy(f.FullName, FolderUrl + "\\" + f.Name, true);
                }
                zip.AddZip(FolderUrl, ZipUrl);
                zip.DeleteFolder(FolderUrl);
                Response.ContentType = "image/JPEG";
                Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", Server.UrlPathEncode(owner_id + ".zip")));
                Response.WriteFile(ZipUrl);
            }

        /// 
        /// 创建压缩文件
        /// 
        /// 压缩前文件夹路径
        /// 压缩后文件路径(包括文件本身)
        public void AddZip(string FolderUrl, string ZipUrl)
        {
            //获得要压缩的文件夹
            string[] filenames = Directory.GetFiles(FolderUrl);
            Crc32 crc = new Crc32();
            //创建压缩后的ZIP文件
            ZipOutputStream s = new ZipOutputStream(File.Create(ZipUrl));
            //压缩级别  0-9
            s.SetLevel(6);
            //防止中文乱码
            Encoding gbk = Encoding.GetEncoding("gbk");
            ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gbk.CodePage;

            foreach (string file in filenames)
            {
                //打开压缩文件
                FileStream fs = File.OpenRead(file);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string fileName = Path.GetFileName(file);      // 文件名  
                ZipEntry entry = new ZipEntry(fileName);
                Debug.Write(buffer.Length + "---" + file);     // 文件大小和文件名  
                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);
            }
            s.Finish();
            s.Close();
        }

        /// 
        /// 删除文件夹
        /// 
        /// 文件夹路径
        public void DeleteFolder(string FolderUrl)
        {
            foreach (string d in Directory.GetFileSystemEntries(FolderUrl))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件   
                }
                else
                    DeleteFolder(d);//递归删除子文件夹   
            }
            Directory.Delete(FolderUrl);//删除已空文件夹   
        }


你可能感兴趣的:(C#附件上传)