命名空间:namespace System.IO.Compression

 

压缩:

//目标文件夹
            string fileDirPath = "/Downloads/试题" + userId + "_" + courseId;
            string downPath = Server.MapPath(fileDirPath);
            if (!Directory.Exists(downPath))
            {
                Directory.CreateDirectory(downPath);
            }
            System.IO.File.SetAttributes(downPath, FileAttributes.Normal);

//压缩文件
            string filePathZIP = Server.MapPath(fileDirPath) + ".zip";  //压缩文件夹
            if (System.IO.File.Exists(filePathZIP))
            {
                System.IO.File.Delete(filePathZIP);
            }
            System.IO.File.SetAttributes(downPath, FileAttributes.Normal);
            ZipFile.CreateFromDirectory(downPath, filePathZIP, CompressionLevel.Optimal, true);//压缩文件
            if (System.IO.Directory.Exists(downPath))
            {
                System.IO.Directory.Delete(downPath, true);    //删除原文件夹
            }
            return File(filePathZIP, "application/octet-stream", "试题" + "_" + userId + "_" + courseId + ".zip");
View Code

相关文章: