Ejemplo n.º 1
0
        /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
        /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
        /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param>
        /// <param name="IsEncrypt">是否加密(默认 加密)</param>
        public static string ZipDirectory(string DirectoryToZip, string ZipedPath, string unEncryptKey = ".meta.delete", string ZipedFileName = "", bool IsEncrypt = true)
        {
            ZipConstants.DefaultCodePage = System.Text.Encoding.UTF8.CodePage;
            ZipedPath = FEPath.GetDirectoryName(ZipedPath);
            //如果目录不存在,则报错
            FEPath.CreateDirectory(DirectoryToZip);

            //文件名称(默认同源文件名称相同)
            string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "/" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "/" + ZipedFileName + ".zip";

            using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
            {
                using (ZipOutputStream s = new ZipOutputStream(ZipFile))
                {
                    if (IsEncrypt)
                    {
                        //压缩文件加密
                        s.Password = ResConfig.FZIPPASS;
                    }
                    string parentPath = FEPath.GetFileNameWithoutExtension(DirectoryToZip) + "/";
                    ZipSetp(DirectoryToZip, s, parentPath, unEncryptKey);
                }
            }
            return(ZipFileName);
        }