Esempio n. 1
0
        void cmTree_GenerateThreadArchive_Click(object sender, EventArgs e)
        {
            if (this.treePostWindowMouseAt == null) return;
            __UpdateStatusText ust = new __UpdateStatusText(this.UpdateStatusText);

            Thread t = this._nodeToThread(this.treePostWindowMouseAt);
            if (t == null || t.Count == 0) return;

            // Gather filenames.
            List<string> l = new List<string>();
            string[] filenames;
            foreach(Post p in t)
                if(!p.ImagePath.Contains("http:"))
                    l.Add(p.ImagePath);
            filenames = new string[l.Count];
            l.CopyTo(filenames);

            DebugConsole.ShowInfo("Creating archive from " + filenames.Length + " files.");

            // Prompt for filename.
            SaveFileDialog fd = new SaveFileDialog();
            string filename = Path.GetDirectoryName(this._db.Filename) + @"\" + t.Id + ".zip";
            fd.AddExtension = true;
            fd.CheckPathExists = true;
            fd.DefaultExt = ".zip";
            fd.Filter = "ZIP Archives (*.zip)|*.zip|All files (*.*)|*.*";
            fd.FilterIndex = 0;
            fd.InitialDirectory = Path.GetDirectoryName(this._db.Filename);
            fd.OverwritePrompt = true;
            fd.RestoreDirectory = true;
            fd.Title = "Save Thread Archive";
            fd.FileName = t.Id + ".zip";

            if (fd.ShowDialog() == DialogResult.OK)
                filename = fd.FileName;

            // Epic totally thread-safe way to do stuff.
            string current = "";
            SysThread st = new SysThread(new ThreadStart(delegate()
            {
                try
                {
                    using (ZipOutputStream s = new ZipOutputStream(File.Create(filename)))
                    {
                        s.SetLevel(9);
                        byte[] buffer = new byte[4096];

                        foreach (string file in filenames)
                        {
                            current = file;

                            ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                            entry.DateTime = new FileInfo(file).LastWriteTime;
                            s.PutNextEntry(entry);

                            using (FileStream fs = File.OpenRead(file))
                            {
                                int sourceBytes;
                                do
                                {
                                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                    s.Write(buffer, 0, sourceBytes);
                                } while (sourceBytes > 0);
                            }

                            s.CloseEntry();
                            DebugConsole.ShowDebug("Added " + file + " to archive, original/compressed " + entry.Size + "/" + entry.CompressedSize);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Program._genericMessageBox("Error creating archive: " + ex.Message, MessageBoxIcon.Error);
                }
            }));
            st.Start();

            if (this._updatingStatus) return;
            try
            {
                this._updatingStatus = true;

                while (st.IsAlive)
                {
                    this.Invoke(ust, "Archiving: \"" + current + "\".");
                    Application.DoEvents();
                    SysThread.Sleep(50);
                }

                this.Invoke(ust, "Ready.");
            }
            finally { this._updatingStatus = false; }

            System.Diagnostics.Process.Start("explorer.exe", "/select," + filename);
        }