public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, IProgress <ZipProgress> progress, bool overwrite, CancellationTokenSource cancelSource) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (destinationDirectoryName == null) { throw new ArgumentNullException(nameof(destinationDirectoryName)); } // Rely on Directory.CreateDirectory for validation of destinationDirectoryName. // Note that this will give us a good DirectoryInfo even if destinationDirectoryName exists: DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName); string destinationDirectoryFullPath = di.FullName; int count = 0; foreach (ZipArchiveEntry entry in source.Entries) { if (cancelSource.IsCancellationRequested) { throw new TaskCanceledException(nameof(source)); } count++; string fileDestinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, entry.FullName)); if (!fileDestinationPath.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase)) { throw new IOException("File is extracting to outside of the folder specified."); } var zipProgress = new ZipProgress(source.Entries.Count, count, entry.FullName); progress.Report(zipProgress); if (Path.GetFileName(fileDestinationPath).Length == 0) { // If it is a directory: if (entry.Length != 0) { throw new IOException("Directory entry with data."); } Directory.CreateDirectory(fileDestinationPath); } else { // If it is a file: // Create containing directory: Directory.CreateDirectory(Path.GetDirectoryName(fileDestinationPath)); entry.ExtractToFile(fileDestinationPath, overwrite: overwrite); } } }
public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, IProgress <ZipProgress> progress, bool overwrite) { if (source == null) { throw new ArgumentNullException("source"); } if (destinationDirectoryName == null) { throw new ArgumentNullException("destinationDirectoryName"); } DirectoryInfo directoryInfo = Directory.CreateDirectory(destinationDirectoryName); string fullName = directoryInfo.FullName; int num = 0; foreach (ZipArchiveEntry entry in source.Entries) { num++; string fullPath = Path.GetFullPath(Path.Combine(fullName, entry.FullName)); if (!fullPath.StartsWith(fullName, StringComparison.OrdinalIgnoreCase)) { throw new IOException("File is extracting to outside of the folder specified."); } try { ZipProgress value = new ZipProgress(source.Entries.Count, num, entry.FullName); progress.Report(value); } catch (Exception) { } if (Path.GetFileName(fullPath).Length == 0) { if (entry.Length != 0) { throw new IOException("Directory entry with data."); } Directory.CreateDirectory(fullPath); } else { Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); try { entry.ExtractToFile(fullPath, overwrite: true); } catch (Exception) { } } } }
private void Report(object sender, ZipProgress zipProgress) { progressPer = ((float)zipProgress.Processed / (float)zipProgress.Total) * 100f; var p = progressPer.ToString("F0"); Debug.Log("Extract " + unzipNum + "/2 : " + p + " %"); lastReportPer = int.Parse(p); SetProgressBarValue((int)((float)lastReportPer / 4f) + (25 * unzipNum)); if (zipProgress.Total == zipProgress.Processed) { unzipNum++; Debug.Log("Extract is done"); } }
private static void Report(object sender, ZipProgress zipProgress) { int value = (int)((float)zipProgress.Processed / (float)zipProgress.Total * 100f); form1.progressBar.Invoke((MethodInvoker) delegate { // Running on the UI thread form1.progressBar.Value = value; }); string text = StringProcessing.getMessage(form2.Lang, " Распаковка: ", " Unpacking: "); string customText = StringProcessing.getMessage(form2.Lang, " Распаковка завершена", " File has been unzipped"); form1.progressBar.Invoke((MethodInvoker) delegate { // Running on the UI thread form1.progressBar.CustomText = " " + text + zipProgress.Processed + " из " + zipProgress.Total; }); if (zipProgress.Processed == zipProgress.Total) { form1.progressBar.Invoke((MethodInvoker) delegate { // Running on the UI thread form1.progressBar.CustomText = customText; }); _progress = null; } if (updateCanceled) { disposeHere(); updateCanceled = false; } }
private void Report(object sender, ZipProgress zipProgress) { //Use zipProgress here to update the UI on the progress. }