写了一个代码,上传文件然后压缩成rar,调试下可以压缩,网站发布了就不能压缩~

哩个名字肯定无人用 2017-07-24 04:12:03
RT!比较急,在线等。
可能是打开winrar的路径问题,我写的代码是绝对路径 如 @"C:\Program Files \WinRAR\WinRAR.exe"
...全文
200 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
压缩几个文件无所谓的,params string[] files这个就是要压缩的文件路径,最终都会压缩到一个zip文件里
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
压缩干嘛要调用exe工具呢?ICSharpCode.SharpZipLib可以直接用啊
有压缩两个文件的库吗?
  • 打赏
  • 举报
回复
        public static void Zip(string zipFilePath, string passWord = null, params string[] files)
        {
            using (var fStream = File.Create(zipFilePath))
            {
                using (var zoStream = new ZipOutputStream(fStream))
                {
                    if (!string.IsNullOrWhiteSpace(passWord))
                    {
                        zoStream.Password = passWord;
                    }
                    var buffer = new byte[4096];
                    foreach (var file in files)
                    {
                        var entry = new ZipEntry(Path.GetFileName(file));
                        zoStream.PutNextEntry(entry);
                        using (var fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                zoStream.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }
                    zoStream.Finish();
                }
            }
        }

        public static IList<string> UnZip(string zipFilePath, string unZipDirectory, string passWord = null)
        {
            IList<string> list = new List<string>();
            using (var ziStream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                if (!string.IsNullOrWhiteSpace(passWord))
                {
                    ziStream.Password = passWord;
                }
                ZipEntry theEntry;
                while ((theEntry = ziStream.GetNextEntry()) != null)
                {
                    if (!Directory.Exists(unZipDirectory))
                    {
                        Directory.CreateDirectory(unZipDirectory);
                    }
                    string path = Path.Combine(unZipDirectory, theEntry.Name);
                    using (var streamWriter = File.Create(path))
                    {
                        list.Add(path);
                        var data = new byte[2048];
                        while (true)
                        {
                            var size = ziStream.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return list;
        }
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
压缩干嘛要调用exe工具呢?ICSharpCode.SharpZipLib可以直接用啊
好,我先去了解怎么用!谢谢
  • 打赏
  • 举报
回复
引用 1 楼 xdashewan 的回复:
HttpServerUtility.MapPath 参考https://msdn.microsoft.com/zh-cn/library/system.web.httpserverutility.mappath(v=vs.110).aspx
ProcessStartInfo startinfo = new ProcessStartInfo(); ; Process process = new Process(); string rarName = "1.rar"; //压缩后文件名 string path = @"C:\images"; //待压缩打包文件夹 string rarPath = @"C:\zip"; //压缩后存放文件夹 string rarexe = @"c:\Program Files\WinRAR\WinRAR.exe"; //WinRAR安装位置 try { //压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName) string cmd = string.Format("a {0} {1} -r", rarName, path); startinfo.FileName = rarexe; startinfo.Arguments = cmd; //设置命令参数 startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口 startinfo.WorkingDirectory = rarPath; process.StartInfo = startinfo; process.Start(); process.WaitForExit(); //无限期等待进程 winrar.exe 退出 if (process.HasExited) { MSCL.JsHelper.Alert("压缩成功!", Page); } } catch (Exception ex) { MSCL.JsHelper.Alert(ex.Message, Page); } finally { process.Dispose(); process.Close(); } 代码例如这样,是个例子!调试下可以的,网站发布了不行了,string rarexe = @"c:\Program Files\WinRAR\WinRAR.exe"; //WinRAR安装位置------》这里应该没问题吧
  • 打赏
  • 举报
回复
压缩干嘛要调用exe工具呢?ICSharpCode.SharpZipLib可以直接用啊
xdashewan 2017-07-24
  • 打赏
  • 举报
回复
HttpServerUtility.MapPath 参考https://msdn.microsoft.com/zh-cn/library/system.web.httpserverutility.mappath(v=vs.110).aspx

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧