public ProcessStateChange(ProcessChange change, string name, int id, Process process) { Change = change; Name = name; Id = id; Process = process; }
/// <summary> /// unPack zipFile /// </summary> /// <param name="zipFilePath">the path of zipFile</param> /// <param name="unzipPath">to Directory</param> /// <param name="changedDG">report process delegate</param> public void UnpackAll(string zipFilePath, string unzipPath, ProcessChange changedDG) { //总需要压缩的文件数量 double totalCount = GetZipFileCount(zipFilePath); string key = System.Guid.NewGuid().ToString(); //Guid Key ProcessItems.Add(key, new ProcessItem(totalCount)); UnpackFiles(zipFilePath, unzipPath, changedDG, "*", key); ProcessItems.Remove(key); }
/// <summary> /// pack directory /// </summary> /// <param name="strDirectory">The directory path you want to zip</param> /// <param name="zipedFile">Target zipFile Path</param> /// <param name="changedDG">report process delegate</param> public void PackDirectory(string strDirectory, string zipedFile, ProcessChange changedDG) { if (!Directory.Exists(strDirectory)) { throw new ArgumentException("需要压缩的文件夹不存在"); } using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile)) { using (ZipOutputStream s = new ZipOutputStream(ZipFile)) { //总需要压缩的文件数量 double totalCount = Directory.GetFileSystemEntries(strDirectory, "*", SearchOption.AllDirectories).Count(); string key = System.Guid.NewGuid().ToString(); //Guid Key ProcessItems.Add(key, new ProcessItem(totalCount)); PackSetp(strDirectory, s, "", key, zipedFile, changedDG); ProcessItems.Remove(key); } } }
/// <summary> /// 压缩文件夹到指定zip包中 /// </summary> /// <param name="dicPath">需要压缩的文件夹</param> /// <param name="zipPath">zip包路径</param> /// <param name="dicPathInZip">压缩以后文件夹在zip包中的路径</param> public void AddDirectory(string dicPath,string zipPath,string dicPathInZip,ProcessChange changedDG) { dicPathInZip = dicPathInZip.EndsWith(Path.DirectorySeparatorChar.ToString()) ? dicPathInZip : dicPathInZip + Path.DirectorySeparatorChar; var files = Directory.GetFiles(dicPath, "*", SearchOption.AllDirectories); double totalCount = files.Count(); string key = System.Guid.NewGuid().ToString(); //Guid Key ProcessItems.Add(key, new ProcessItem(totalCount)); if(!Directory.Exists(dicPath)) { throw new ArgumentException("文件夹路径不存在"); } foreach (var file in files) { string filePathInZip=IsKeepPath?dicPathInZip+file.Remove(0,dicPath.Count()): dicPathInZip+Path.GetFileName(file); AddFile(file, zipPath, filePathInZip); changedDG(AddOneAndReport(key)); } ProcessItems.Remove(key); }
/// <summary> /// 压缩文件夹到指定zip包中 /// </summary> /// <param name="dicPath">需要压缩的文件夹</param> /// <param name="zipPath">zip包路径</param> /// <param name="dicPathInZip">压缩以后文件夹在zip包中的路径</param> public void AddDirectory(string dicPath, string zipPath, string dicPathInZip, ProcessChange changedDG) { dicPathInZip = dicPathInZip.EndsWith(Path.DirectorySeparatorChar.ToString()) ? dicPathInZip : dicPathInZip + Path.DirectorySeparatorChar; var files = Directory.GetFiles(dicPath, "*", SearchOption.AllDirectories); double totalCount = files.Count(); string key = System.Guid.NewGuid().ToString(); //Guid Key ProcessItems.Add(key, new ProcessItem(totalCount)); if (!Directory.Exists(dicPath)) { throw new ArgumentException("文件夹路径不存在"); } foreach (var file in files) { string filePathInZip = IsKeepPath?dicPathInZip + file.Remove(0, dicPath.Count()): dicPathInZip + Path.GetFileName(file); AddFile(file, zipPath, filePathInZip); changedDG(AddOneAndReport(key)); } ProcessItems.Remove(key); }
private void HandleFile(string filePath, ZipInputStream zipStream, string processKey, ProcessChange changedDG) { using (FileStream stream = File.Create(filePath)) { byte[] buffer = new byte[2048]; while (true) { int size = zipStream.Read(buffer, 0, buffer.Length); if (size > 0) { stream.Write(buffer, 0, size); } else { break; } } if (changedDG != null) { changedDG(AddOneAndReport(processKey)); } } }
/// <summary> /// 解压基类 /// </summary> /// <param name="zipFilePath">压缩包文件路径</param> /// <param name="unzipPath">解压到的文件路径</param> /// <param name="changedDG">进度反馈委托</param> /// <param name="directoryName">解压指定的文件或文件夹,默认为空(所有)</param> private void UnpackFiles(string zipFilePath, string unzipPath, ProcessChange changedDG = null, string directName = "*", string processKey = "") { #region unzip if (unzipPath[unzipPath.Length - 1] != Path.DirectorySeparatorChar) { unzipPath = unzipPath + Path.DirectorySeparatorChar; } if (!Directory.Exists(unzipPath)) { Directory.CreateDirectory(unzipPath); } using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry zipEntry = null; while ((zipEntry = zipStream.GetNextEntry()) != null) { zipEntry.CompressedSize = 9; string directoryName = Path.GetDirectoryName(zipEntry.Name); string fileName = Path.GetFileName(zipEntry.Name); if (!string.IsNullOrEmpty(directoryName)) { if (!Directory.Exists(unzipPath + directoryName)) { if (directName != "*" && !Path.HasExtension(directName) && !directoryName.StartsWith(directName)) { continue; } if (IsKeepPath) { Directory.CreateDirectory(unzipPath + directoryName); } } } if (!string.IsNullOrEmpty(fileName)) { if (zipEntry.CompressedSize == 0) { break; } if (zipEntry.IsDirectory) { directoryName = Path.GetDirectoryName(unzipPath + zipEntry.Name); } string zipFileName = !IsKeepPath?Path.GetFileName(zipEntry.Name) : zipEntry.Name; if (directName != "*" && !Path.HasExtension(directName) && !zipEntry.Name.StartsWith(directName)) { continue; } if (Path.HasExtension(directName) && directName == zipEntry.Name) { HandleFile(unzipPath + zipFileName, zipStream, processKey, changedDG); return; } else if (!string.IsNullOrEmpty(directName) && !Path.HasExtension(directName)) { HandleFile(unzipPath + zipFileName, zipStream, processKey, changedDG); } } } } #endregion }
/// <summary> /// 递归遍历目录 /// </summary> private void PackSetp(string strDirectory, ZipOutputStream s, string parentPath, string processItemKey, string zipPath, ProcessChange changedDG) { if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in filenames) // 遍历所有的文件和目录 { if (Directory.Exists(file)) // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 { string pPath = parentPath; pPath += file.Substring(file.LastIndexOf(Path.DirectorySeparatorChar) + 1); pPath += Path.DirectorySeparatorChar; PackSetp(file, s, pPath, processItemKey, zipPath, changedDG); if (changedDG != null) { changedDG(AddOneAndReport(processItemKey)); } } else // 否则直接压缩文件 { //打开压缩文件 using (FileStream fs = File.OpenRead(file)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1); if (!this.IsKeepPath && fileName.Contains("\\")) { fileName = Path.GetFileName(fileName); if (ProcessItems[processItemKey].HandledFiles.Count(p => p == fileName) > 0) { changedDG(AddOneAndReport(processItemKey)); continue; } } ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); if (changedDG != null) { changedDG(AddOneAndReport(processItemKey)); } ; ProcessItems[processItemKey].HandledFiles.Add(fileName); } } } }
private void HandleFile(string filePath, ZipInputStream zipStream, string processKey, ProcessChange changedDG) { using (FileStream stream = File.Create(filePath)) { byte[] buffer = new byte[2048]; while (true) { int size = zipStream.Read(buffer, 0, buffer.Length); if (size > 0) { stream.Write(buffer, 0, size); } else { break; } } if (changedDG != null) changedDG(AddOneAndReport(processKey)); } }
/// <summary> /// 解压基类 /// </summary> /// <param name="zipFilePath">压缩包文件路径</param> /// <param name="unzipPath">解压到的文件路径</param> /// <param name="changedDG">进度反馈委托</param> /// <param name="directoryName">解压指定的文件或文件夹,默认为空(所有)</param> private void UnpackFiles(string zipFilePath, string unzipPath, ProcessChange changedDG = null, string directName = "*", string processKey = "") { #region unzip if (unzipPath[unzipPath.Length - 1] != Path.DirectorySeparatorChar) { unzipPath = unzipPath + Path.DirectorySeparatorChar; } if(!Directory.Exists(unzipPath)) { Directory.CreateDirectory(unzipPath); } using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry zipEntry = null; while ((zipEntry = zipStream.GetNextEntry()) != null) { zipEntry.CompressedSize = 9; string directoryName = Path.GetDirectoryName(zipEntry.Name); string fileName = Path.GetFileName(zipEntry.Name); if (!string.IsNullOrEmpty(directoryName)) { if (!Directory.Exists(unzipPath + directoryName)) { if (directName != "*" && !Path.HasExtension(directName) && !directoryName.StartsWith(directName)) { continue; } if(IsKeepPath) { Directory.CreateDirectory(unzipPath + directoryName); } } } if (!string.IsNullOrEmpty(fileName)) { if (zipEntry.CompressedSize == 0) break; if (zipEntry.IsDirectory) { directoryName = Path.GetDirectoryName(unzipPath + zipEntry.Name); } string zipFileName = !IsKeepPath ? Path.GetFileName(zipEntry.Name) : zipEntry.Name; if (directName != "*" && !Path.HasExtension(directName) && !zipEntry.Name.StartsWith(directName)) { continue; } if (Path.HasExtension(directName) && directName == zipEntry.Name) { HandleFile(unzipPath + zipFileName, zipStream, processKey, changedDG); return; } else if (!string.IsNullOrEmpty(directName)&& !Path.HasExtension(directName)) { HandleFile(unzipPath + zipFileName, zipStream, processKey, changedDG); } } } } #endregion }
/// <summary> /// 递归遍历目录 /// </summary> private void PackSetp(string strDirectory, ZipOutputStream s, string parentPath, string processItemKey, string zipPath, ProcessChange changedDG) { if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in filenames)// 遍历所有的文件和目录 { if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 { string pPath = parentPath; pPath += file.Substring(file.LastIndexOf(Path.DirectorySeparatorChar) + 1); pPath += Path.DirectorySeparatorChar; PackSetp(file, s, pPath, processItemKey,zipPath, changedDG); if (changedDG != null) { changedDG(AddOneAndReport(processItemKey)); } } else // 否则直接压缩文件 { //打开压缩文件 using (FileStream fs = File.OpenRead(file)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1); if(!this.IsKeepPath&&fileName.Contains("\\")) { fileName=Path.GetFileName(fileName); if (ProcessItems[processItemKey].HandledFiles.Count(p=>p==fileName)>0) { changedDG(AddOneAndReport(processItemKey)); continue; } } ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); if (changedDG != null) { changedDG(AddOneAndReport(processItemKey)); }; ProcessItems[processItemKey].HandledFiles.Add(fileName); } } } }