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();
                }
            }
        }
Example #2
0
        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);
            }
        }
Example #3
0
        private List <ImageWorkUI> _getAllImageWorkUIs(OfficeDocument doc, List <ImageWorkUI> results = null)
        {
            if (results == null)
            {
                results = new List <ImageWorkUI>();
            }
            foreach (XmlRelsDoc xmlRelDoc in doc.XmlRelsDocs)
            {
                foreach (ImageWorkUI iwui in xmlRelDoc.ImageWorkUIs.Values)
                {
                    iwui.Dock = DockStyle.Top;
                    results.Add(iwui);
                }
            }

            // Recurse into embedded documents
            foreach (OfficeDocument embeddedDocument in doc.EmbeddedDocuments)
            {
                _getAllImageWorkUIs(embeddedDocument, results);
            }

            return(results);
        }
Example #4
0
        private void AddImageWorkUIsToLayout(OfficeDocument doc)
        {
            imageWorkUIs = _getAllImageWorkUIs(doc);
            imageWorkUIs = imageWorkUIs.OrderBy(x => x.Original.FileSize).ToList();

            // Add the results to the UI
            int count            = 0;
            int totalImagesFound = imageWorkUIs.Count;

            // Determine how much the progress bar advances for each image
            Status.SetProgress(0);
            float stepProgress  = 100 / (float)totalImagesFound;
            float floatProgress = 0;
            int   intProgress   = 0;

            foreach (ImageWorkUI iwui in imageWorkUIs)
            {
                count++;
                iwui.Dock = DockStyle.Top;
                pnlImageWorkUIs.Controls.Add(iwui);

                Status.SetStatus("Adding image optimizers to layout... " + count + " of " + totalImagesFound);
                statusStrip1.Invalidate();
                statusStrip1.Refresh();

                // Hook into their changed events
                iwui.OptimizedChanged += Iwui_OptimizedChanged;

                // Update progress
                floatProgress += stepProgress;
                if (intProgress != (int)floatProgress)
                {
                    intProgress = (int)floatProgress;
                    Status.SetProgress(intProgress);
                }
            }
        }