/// <summary> /// Ensures the stream is zipped - or zips it if needed /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileStream">The file stream.</param> /// <returns>Task<Stream>.</returns> public static async Task <Stream> ZipTheStreamIfNeeded(string fileName, Stream fileStream) { if (fileName.EndsWith(".msi", StringComparison.InvariantCultureIgnoreCase)) { return(fileStream); } if (ZipHelpers.IsZipCompressedData(fileStream)) { return(fileStream); } else { string originalFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(), Path.GetFileNameWithoutExtension(fileName), fileName); DirectoryInfo directoryToCompress = Directory.CreateDirectory(Path.GetDirectoryName(originalFilePath)); using (Stream compressedStream = File.Create(originalFilePath)) { await fileStream.CopyToAsync(compressedStream).ConfigureAwait(false); } var zippedFilePath = directoryToCompress.FullName + ".zip"; ZipFile.CreateFromDirectory(directoryToCompress.FullName, zippedFilePath); return(File.OpenRead(zippedFilePath)); } }
public void complete_setup(String __path, SetupManager form) { var Helper = new DownloadHelpers(); var client = Helper.DownloadFfmpeg(__path); form.updateSetupLabel("Downloading FFmpeg"); this.client = client; client.DownloadProgressChanged += (t, k) => { form.updateProgress(k.ProgressPercentage); }; client.DownloadFileCompleted += (k, t) => { if (t.Error != null) { MessageBox.Show(t.Error.ToString()); } ; if (t.Cancelled) { return; } form.updateSetupLabel("Extracting FFmpeg"); ZipHelpers.ExtractFfmpeg($@"{__path}/ffmpeg", $@"{__path}/ffmpeg-zip.zip"); form.onComplete(); }; }
public void TestZipFile() { using (StreamReader sr = new StreamReader("Zip/ThisIsAZip.zip")) { Assert.IsTrue(ZipHelpers.IsZipCompressedData(sr.BaseStream)); Assert.IsTrue(ZipHelpers.IsCompressedData(sr.BaseStream)); Assert.AreEqual(0, sr.BaseStream.Position); } }
static void Main(string[] args) { //See another project in this solution for actual code Console.WriteLine("1Enter a path to directory"); string path = Console.ReadLine(); AnimateHelper.AnimateProgress(ZipHelpers.DecompressDirectoryContents(path)); Console.WriteLine("\nReady"); Console.ReadLine(); }
public async void DecompressArchiveToChildFolder() { var selectedItem = associatedInstance?.SlimContentPage?.SelectedItem; if (selectedItem == null) { return; } BaseStorageFile archive = await StorageHelpers.ToStorageItem <BaseStorageFile>(selectedItem.ItemPath); BaseStorageFolder currentFolder = await StorageHelpers.ToStorageItem <BaseStorageFolder>(associatedInstance.FilesystemViewModel.CurrentFolder.ItemPath); BaseStorageFolder destinationFolder = null; if (currentFolder != null) { destinationFolder = await FilesystemTasks.Wrap(() => currentFolder.CreateFolderAsync(Path.GetFileNameWithoutExtension(archive.Path), CreationCollisionOption.OpenIfExists).AsTask()); } if (archive != null && destinationFolder != null) { CancellationTokenSource extractCancellation = new CancellationTokenSource(); PostedStatusBanner banner = App.OngoingTasksViewModel.PostOperationBanner( string.Empty, "ExtractingArchiveText".GetLocalized(), 0, ReturnResult.InProgress, FileOperationType.Extract, extractCancellation); Stopwatch sw = new Stopwatch(); sw.Start(); await ZipHelpers.ExtractArchive(archive, destinationFolder, banner.Progress, extractCancellation.Token); sw.Stop(); banner.Remove(); if (sw.Elapsed.TotalSeconds >= 6) { App.OngoingTasksViewModel.PostBanner( "ExtractingCompleteText".GetLocalized(), "ArchiveExtractionCompletedSuccessfullyText".GetLocalized(), 0, ReturnResult.Success, FileOperationType.Extract); } } }
public async void DecompressArchiveToChildFolder() { StorageFile archive = await StorageItemHelpers.ToStorageItem <StorageFile>(associatedInstance.SlimContentPage.SelectedItem.ItemPath); StorageFolder currentFolder = await StorageItemHelpers.ToStorageItem <StorageFolder>(associatedInstance.FilesystemViewModel.CurrentFolder.ItemPath); StorageFolder destinationFolder = null; if (currentFolder != null) { destinationFolder = await currentFolder.CreateFolderAsync(Path.GetFileNameWithoutExtension(archive.Path), CreationCollisionOption.OpenIfExists); } if (archive != null && destinationFolder != null) { CancellationTokenSource extractCancellation = new CancellationTokenSource(); PostedStatusBanner banner = App.StatusCenterViewModel.PostOperationBanner( string.Empty, "Extracting archive", 0, ReturnResult.InProgress, FileOperationType.Extract, extractCancellation); Stopwatch sw = new Stopwatch(); sw.Start(); await ZipHelpers.ExtractArchive(archive, destinationFolder, banner.Progress, extractCancellation.Token); sw.Stop(); banner.Remove(); if (sw.Elapsed.TotalSeconds >= 6) { App.StatusCenterViewModel.PostBanner( "Extracting complete!", "The archive extraction completed successfully.", 0, ReturnResult.Success, FileOperationType.Extract); } } }
public async void DecompressArchiveHere() { StorageFile archive = await StorageItemHelpers.ToStorageItem <StorageFile>(associatedInstance.SlimContentPage.SelectedItem.ItemPath); StorageFolder currentFolder = await StorageItemHelpers.ToStorageItem <StorageFolder>(associatedInstance.FilesystemViewModel.CurrentFolder.ItemPath); if (archive != null && currentFolder != null) { CancellationTokenSource extractCancellation = new CancellationTokenSource(); PostedStatusBanner banner = App.StatusCenterViewModel.PostOperationBanner( string.Empty, "ExtractingArchiveText".GetLocalized(), 0, ReturnResult.InProgress, FileOperationType.Extract, extractCancellation); Stopwatch sw = new Stopwatch(); sw.Start(); await ZipHelpers.ExtractArchive(archive, currentFolder, banner.Progress, extractCancellation.Token); sw.Stop(); banner.Remove(); if (sw.Elapsed.TotalSeconds >= 6) { App.StatusCenterViewModel.PostBanner( "ExtractingCompleteText".GetLocalized(), "ArchiveExtractionCompletedSuccessfullyText".GetLocalized(), 0, ReturnResult.Success, FileOperationType.Extract); } } }
public async void DecompressArchive() { BaseStorageFile archive = await StorageHelpers.ToStorageItem <BaseStorageFile>(associatedInstance.SlimContentPage.SelectedItem.ItemPath); if (archive != null) { DecompressArchiveDialog decompressArchiveDialog = new DecompressArchiveDialog(); DecompressArchiveDialogViewModel decompressArchiveViewModel = new DecompressArchiveDialogViewModel(archive); decompressArchiveDialog.ViewModel = decompressArchiveViewModel; ContentDialogResult option = await decompressArchiveDialog.ShowAsync(); if (option == ContentDialogResult.Primary) { // Check if archive still exists if (!StorageHelpers.Exists(archive.Path)) { return; } CancellationTokenSource extractCancellation = new CancellationTokenSource(); PostedStatusBanner banner = App.OngoingTasksViewModel.PostOperationBanner( string.Empty, "ExtractingArchiveText".GetLocalized(), 0, ReturnResult.InProgress, FileOperationType.Extract, extractCancellation); BaseStorageFolder destinationFolder = decompressArchiveViewModel.DestinationFolder; string destinationFolderPath = decompressArchiveViewModel.DestinationFolderPath; if (destinationFolder == null) { BaseStorageFolder parentFolder = await StorageHelpers.ToStorageItem <BaseStorageFolder>(Path.GetDirectoryName(archive.Path)); destinationFolder = await FilesystemTasks.Wrap(() => parentFolder.CreateFolderAsync(Path.GetFileName(destinationFolderPath), CreationCollisionOption.GenerateUniqueName).AsTask()); } if (destinationFolder == null) { return; // Could not create dest folder } Stopwatch sw = new Stopwatch(); sw.Start(); await ZipHelpers.ExtractArchive(archive, destinationFolder, banner.Progress, extractCancellation.Token); sw.Stop(); banner.Remove(); if (sw.Elapsed.TotalSeconds >= 6) { App.OngoingTasksViewModel.PostBanner( "ExtractingCompleteText".GetLocalized(), "ArchiveExtractionCompletedSuccessfullyText".GetLocalized(), 0, ReturnResult.Success, FileOperationType.Extract); } if (decompressArchiveViewModel.OpenDestinationFolderOnCompletion) { await NavigationHelpers.OpenPath(destinationFolderPath, associatedInstance, FilesystemItemType.Directory); } } } }