Example #1
0
        /// <summary>
        /// 创建压缩包
        /// </summary>
        /// <param name="zipFile"></param>
        /// <param name="e"></param>
        public void CreateZip(string title, string zipFile, string pwd, Wrapper.RunworkEventArgs e, KeyValuePair <string, FileInfo>[] files)
        {
            using (var fs = new System.IO.FileStream(zipFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
                using (var zip = new ICCEmbedded.SharpZipLib.Zip.ZipOutputStream(fs))
                {
                    zip.Password = pwd;
                    zip.SetLevel(9);

                    var entryFactory = new ICCEmbedded.SharpZipLib.Zip.ZipEntryFactory();

                    //合并路径
                    var fileGroups = files.GroupBy(s => System.IO.Path.GetDirectoryName(s.Key)).ToDictionary(s => s.Key, s => s.ToArray());
                    var folders    = fileGroups.Keys.OrderBy(s => s, StringComparer.OrdinalIgnoreCase).ToArray();

                    folders.ForEach(s =>
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            var fe           = entryFactory.MakeDirectoryEntry(s);
                            fe.IsUnicodeText = true;
                            fe.DateTime      = DateTime.Now;
                            zip.PutNextEntry(fe);
                            zip.CloseEntry();
                        }

                        foreach (var f in fileGroups[s])
                        {
                            if (e != null)
                            {
                                e.ReportProgress(e.Progress.TaskCount, e.Progress.TaskProgress + 1, string.Format(title, f.Key));
                            }

                            var ent           = entryFactory.MakeFileEntry(f.Key);
                            ent.IsUnicodeText = true;
                            ent.DateTime      = f.Value.LastWriteTime;

                            //复制文件内容。简单起见,这里不返回进度显示。。。
                            using (var sou = f.Value.OpenRead())
                            {
                                ent.Size = sou.Length;
                                zip.PutNextEntry(ent);
                                ICCEmbedded.SharpZipLib.Core.StreamUtils.Copy(sou, zip, new byte[0x400]);
                            }
                            zip.CloseEntry();
                        }
                    });
                    zip.Flush();
                    zip.Close();
                }
        }
		/// <summary>
		/// 创建压缩包
		/// </summary>
		/// <param name="zipFile"></param>
		/// <param name="e"></param>
		public void CreateZip(string title, string zipFile, string pwd, Wrapper.RunworkEventArgs e, KeyValuePair<string, FileInfo>[] files)
		{
			using (var fs = new System.IO.FileStream(zipFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
			using (var zip = new ICCEmbedded.SharpZipLib.Zip.ZipOutputStream(fs))
			{
				zip.Password = pwd;
				zip.SetLevel(9);

				var entryFactory = new ICCEmbedded.SharpZipLib.Zip.ZipEntryFactory();

				//合并路径
				var fileGroups = files.GroupBy(s => System.IO.Path.GetDirectoryName(s.Key)).ToDictionary(s => s.Key, s => s.ToArray());
				var folders = fileGroups.Keys.OrderBy(s => s, StringComparer.OrdinalIgnoreCase).ToArray();

				folders.ForEach(s =>
				{
					if (!string.IsNullOrEmpty(s))
					{
						var fe = entryFactory.MakeDirectoryEntry(s);
						fe.IsUnicodeText = true;
						fe.DateTime = DateTime.Now;
						zip.PutNextEntry(fe);
						zip.CloseEntry();
					}

					foreach (var f in fileGroups[s])
					{
						if (e != null)
							e.ReportProgress(e.Progress.TaskCount, e.Progress.TaskProgress + 1, string.Format(title, f.Key));

						var ent = entryFactory.MakeFileEntry(f.Key);
						ent.IsUnicodeText = true;
						ent.DateTime = f.Value.LastWriteTime;

						//复制文件内容。简单起见,这里不返回进度显示。。。
						using (var sou = f.Value.OpenRead())
						{
							ent.Size = sou.Length;
							zip.PutNextEntry(ent);
							ICCEmbedded.SharpZipLib.Core.StreamUtils.Copy(sou, zip, new byte[0x400]);
						}
						zip.CloseEntry();
					}
				});
				zip.Flush();
				zip.Close();
			}
		}