示例#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);
        }
示例#2
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
        /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using ( outputStream_ = new ZipOutputStream(outputStream) ) {

            #if !NETCF_1_0
                if ( password_ != null ) {
                    outputStream_.Password = password_;
                }
            #endif

                outputStream_.UseZip64 = UseZip64;
                FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += new ProcessFileHandler(ProcessFile);
                if ( this.CreateEmptyDirectories ) {
                    scanner.ProcessDirectory += new ProcessDirectoryHandler(ProcessDirectory);
                }

                if (events_ != null) {
                    if ( events_.FileFailure != null ) {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if ( events_.DirectoryFailure != null ) {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }