Example #1
0
        /// <summary>
        /// 加载文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="Type"></param>
        /// <returns></returns>
        public static SerializeFileInfo LoadFile(string filepath, SerializeFileInfoType Type = SerializeFileInfoType.File)
        {
            var SerializeFileInfo = new SerializeFileInfo();

            SerializeFileInfo.Type = Type;
            if (Type == SerializeFileInfoType.Directory)
            {
                return(SerializeFileInfo);
            }
            SerializeFileInfo.FileBuffer = File.ReadAllBytes(filepath);
            SerializeFileInfo.FileName   = System.IO.Path.GetFileName(filepath);
            return(SerializeFileInfo);
        }
Example #2
0
        /// <summary>
        /// 对目标文件夹进行压缩,将压缩结果保存为指定文件
        /// </summary>
        public void Compress()
        {
            if (!Directory.Exists(DirPath))
            {
                Message?.Invoke("目标文件夹不存在!");
                return;
            }

            var files = Directory.GetFiles(DirPath, "*", SearchOption.AllDirectories);

            //if (files == null || files.Length <= 0)
            //{
            //    Message?.Invoke("目标文件夹内没有文件!");
            //    return;
            //}

            //准备文件
            Message?.Invoke("正在准备文件!");
            var list = new List <SerializeFileInfo>();

            foreach (string f in files)
            {
                SerializeFileInfo sfi = SerializeFileInfo.LoadFile(f);
                //Message?.Invoke("正在准备文件" + sfi.FileName + "!");
                sfi.Path = f.Substring(DirPath.Length, f.Length - DirPath.Length - sfi.FileName.Length);
                list.Add(sfi);
            }

            //解决文件夹问题-一般不会太多
            var dirs = Directory.GetDirectories(DirPath, "*", SearchOption.AllDirectories);

            if (dirs != null && dirs.Length > 0)
            {
                foreach (string d in dirs)
                {
                    if (Directory.GetFiles(d, "*", SearchOption.AllDirectories).Length == 0)//当文件夹内没用文件时启用
                    {
                        SerializeFileInfo sfi = SerializeFileInfo.LoadFile(d, SerializeFileInfo.SerializeFileInfoType.Directory);
                        sfi.Path = d.Substring(DirPath.Length, d.Length - DirPath.Length);
                        list.Add(sfi);
                    }
                }
            }
            var source = list.ToStream();

            //开始压缩
            Message?.Invoke("正在压缩文件!");
            using (Stream destination = new FileStream(ZipFileName, FileMode.Create, FileAccess.Write))
            {
                using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
                {
                    byte[] bytes = new byte[bufferSize];
                    int    n;
                    while ((n = source.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        Message?.Invoke("正在压缩文件!已完成" + (source.Position * 100 / source.Length).ToString("0.##") + "%");
                        output.Write(bytes, 0, n);
                    }
                }
                destination.Close();
            }

            Message?.Invoke("压缩已完成!");

            GC.Collect();
        }