asp.net 文件压缩



  protected void btnZip_Click(object sender, EventArgs e)
    {
        //string filePath1 = Server.MapPath("~/ZWB");//取路径
        string filePath1 = Path.GetDirectoryName(@"E:\ZWB\"); //取路径

         string filePath = filePath1 + @"\" + System.IO.Path.GetFileNameWithoutExtension(filePath1); //取压缩文件的名字
      
        if (filePath1 != string.Empty)
        {
            //压缩
            String myRar;
            RegistryKey myReg;
            Object myObj;
            String myInfo;
            ProcessStartInfo myStartInfo;
            Process myProcess;
            try
            {
                myReg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
                myObj = myReg.GetValue("");
                myRar = myObj.ToString();
                myReg.Close();
                myRar = myRar.Substring(1, myRar.Length - 7);
                myInfo = "a  -r  -ep1 " + filePath + ".zip " + filePath1;
                //1、这里是压缩整个目录
                // 2、“XXX.zip” 是压缩后的文件名称
                // 3、lbTree.Text + @"zip"是需要压缩的文件或文件夹,可多个
                myStartInfo = new ProcessStartInfo();
                myStartInfo.FileName = myRar;
                myStartInfo.Arguments = myInfo;
                myStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                myStartInfo.WorkingDirectory = filePath1 + "zip"; //获取或设置要启动的进程的初始目录,即压缩后的文件的路径。
                myProcess = new Process();
                myProcess.StartInfo = myStartInfo;
                myProcess.Start();
                Response.Write("<font color=red>压缩成功</font>");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        else
        {
            Response.Write("<font color=red>压缩文件不存在请上传要压缩的文件</font>");

        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET Core 提供了一些选项来打包和压缩静态资源,以提高应用程序的性能。下面是一些可用的选项: 1. Bundle and Minify:该选项使用 ASP.NET Core 的内置打包和压缩功能。你可以使用 Microsoft.AspNetCore.Mvc.BundleAndMinify NuGet 包将其添加到项目中。然后,你可以在 Startup.cs 文件中配置它,例如: ``` public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddWebOptimizer(options => { options.MinifyCssFiles(); options.MinifyJsFiles(); options.AddBundle("/bundle.css", "css/bootstrap.css", "css/site.css"); options.AddBundle("/bundle.js", "js/jquery.js", "js/bootstrap.js", "js/site.js"); }); } ``` 2. Gzip Compression:该选项使用 Gzip 压缩压缩静态资源。你可以在 Startup.cs 文件中配置它,例如: ``` public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStaticFiles(new StaticFileOptions { HttpsCompression = HttpsCompressionMode.Compress, OnPrepareResponse = context => { context.Context.Response.Headers.Append("Cache-Control", "public,max-age=604800"); } }); } ``` 3. Brotli Compression:该选项使用 Brotli 压缩压缩静态资源。你可以使用 Microsoft.AspNetCore.ResponseCompression NuGet 包将其添加到项目中。然后,你可以在 Startup.cs 文件中配置它,例如: ``` public void ConfigureServices(IServiceCollection services) { services.AddResponseCompression(options => { options.EnableForHttps = true; options.Providers.Add<BrotliCompressionProvider>(); }); } ``` 无论你选择哪个选项,都可以提高应用程序的性能。你可以根据自己的需要选择其中一个或多个选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值