public async Task Unpack() { // Set the status Status.SetStatus("Unpacking " + new FileInfo(File).Name + "..."); // Create a new temp folder do { UnpackedFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); } while (Directory.Exists(UnpackedFolder)); // Unzip the contents await Task.Run(() => Compression.UnZIPFile(File, UnpackedFolder)); // Search the contents for embedded documents Status.SetStatus("Searching for embedded documents..."); string[] unpackedFiles = Directory.EnumerateFiles(UnpackedFolder, "*", SearchOption.AllDirectories).ToArray(); foreach (string unpackedFile in unpackedFiles) { string lcExtension = unpackedFile.Substring(unpackedFile.LastIndexOf('.')).ToLower(); if (officeDocExtensions.Contains(lcExtension)) { // Unpack the child document and add it to the list OfficeDocument embeddedDoc = new OfficeDocument(unpackedFile); EmbeddedDocuments.Add(embeddedDoc); await embeddedDoc.Unpack(); } } }
private async void btnOptimize_Click(object sender, EventArgs e) { try { // Ensure the file exists txtFile.Text = txtFile.Text.Trim(); if (!File.Exists(txtFile.Text)) { throw new FileNotFoundException("That file does not exist!", txtFile.Text); } // Okay, start by disabling the controls btnOptimize.Enabled = false; txtFile.Enabled = false; // Now unpack the document recursively workDocument = new OfficeDocument(txtFile.Text); await workDocument.Unpack(); Status.SetProgress(50); // Now recursively search for potentially-optimizable components await Task.Run(() => workDocument.SearchForOptimizationOpportunities()); // Add the controls to the layout imageWorkUIs = new List <ImageWorkUI>(); SuspendDrawing(pnlContents); AddImageWorkUIsToLayout(workDocument); pnlResults.Visible = true; ResumeDrawing(pnlContents); _updateResults(); Status.SetStatus("Finished.", 0); } catch (Exception ex) { btnOptimize.Enabled = true; txtFile.Enabled = true; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }