~do-core/do/fix-the-shitstorm

« back to all changes in this revision

Viewing changes to Archive/src/CreateArchiveAction.cs

Adding missing files and Archive plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// CreateArchiveAction.cs
 
2
//
 
3
//  Copyright (C) 2008 [name of author]
 
4
//
 
5
// This program is free software; you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation; either version 2 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
13
// GNU General Public License for more details.
 
14
// 
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program; if not, write to the Free Software
 
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
//
 
19
 
 
20
 
 
21
using System;
 
22
using System.IO;
 
23
using System.Collections.Generic;
 
24
using System.Diagnostics;
 
25
 
 
26
using Do.Universe;
 
27
 
 
28
namespace Archive {
 
29
        
 
30
        public enum ArchiveType { GZIP, BZIP2, TAR, ZIP };
 
31
        
 
32
        public class ArchiveAction : AbstractAction {
 
33
                
 
34
                public ArchiveAction()
 
35
                {
 
36
                }
 
37
                
 
38
                public override string Name {
 
39
                        get { return "Create archive"; }
 
40
                }
 
41
                
 
42
                public override string Description {
 
43
                        get { return "Create an archive with the selected item"; }
 
44
                }
 
45
        
 
46
                public override string Icon {
 
47
                        get { return "file-roller"; }
 
48
                }
 
49
                
 
50
                public override Type[] SupportedItemTypes {
 
51
                        get {
 
52
                                return new Type[] {                             
 
53
                                        typeof (IFileItem),
 
54
                                };
 
55
                        }
 
56
                }
 
57
                
 
58
                public override bool SupportsItem (IItem item) 
 
59
                {
 
60
                        return true;
 
61
                }
 
62
                                
 
63
                public override Type[] SupportedModifierItemTypes {
 
64
                        get {
 
65
                                return new Type[] {
 
66
                                        typeof(ArchiveTypeItem),
 
67
                                };
 
68
                        }
 
69
                }
 
70
                
 
71
                public override bool ModifierItemsOptional {
 
72
                        get { return true; }
 
73
                }
 
74
                
 
75
                public override IItem[] DynamicModifierItemsForItem (IItem item)
 
76
                {
 
77
                        List<IItem> items = new List<IItem> ();
 
78
        
 
79
                        try {
 
80
                                items.Add(new ArchiveTypeItem (ArchiveType.GZIP));
 
81
                                items.Add(new ArchiveTypeItem (ArchiveType.BZIP2));
 
82
                                items.Add(new ArchiveTypeItem (ArchiveType.TAR));
 
83
                                //items.Add(new ArchiveTypeItem (ArchiveType.ZIP));
 
84
                                return items.ToArray();
 
85
                        } catch {
 
86
                                return null;
 
87
                        }
 
88
                        
 
89
                }
 
90
                
 
91
                public override IItem[] Perform (IItem[] items, IItem[] modItems)
 
92
                {
 
93
                        if ( modItems.Length > 0 )
 
94
                                Archive ((items[0] as IFileItem), (modItems[0] as ArchiveTypeItem).ArchiveType);
 
95
                        else
 
96
                                Archive ((items[0] as IFileItem), 0);
 
97
                        return null;
 
98
                }
 
99
                
 
100
                private void Archive ( IFileItem item, int archiveType)
 
101
                {
 
102
                        string path = null;
 
103
                        string file = null;
 
104
                               
 
105
                        if ( Directory.Exists(item.Path) ) {
 
106
                                path = item.Path.Replace(item.Name,"");
 
107
                                file = item.Name;
 
108
                        }
 
109
                        else {
 
110
                                path = item.Path.Replace(item.Name,"");
 
111
                                file = String.Concat (System.IO.Path.GetFileName (path.Substring (0, path.Length -1)),
 
112
                                                        "/",
 
113
                                                        item.Name);
 
114
                               
 
115
                                path = item.Path.Replace(file, "");
 
116
                        }                       
 
117
                
 
118
                        path = EscapeString (path);
 
119
                        file = EscapeString (file);
 
120
 
 
121
                        switch (archiveType) {
 
122
                                case (int)ArchiveType.GZIP:
 
123
                                        Process.Start (string.Format ("tar -czf {0} -C {1} {2}", String.Concat (item.Name ,".tar.gz"), path, file));  
 
124
                                        break;
 
125
                                case (int)ArchiveType.BZIP2:
 
126
                                        Process.Start (string.Format ("tar -cjf {0} -C {1} {2}", String.Concat (item.Name ,".tar.bz2"), path, file));
 
127
                                        break;
 
128
                                case (int)ArchiveType.TAR:
 
129
                                        Process.Start (string.Format ("tar -cf {0} -C {1} {2}", String.Concat (item.Name ,".tar"), path, file));
 
130
                                        break;
 
131
                                case (int)ArchiveType.ZIP:
 
132
                                        Process.Start (string.Format ("zip {0} {1} ", file , path));
 
133
                                        break;
 
134
                                default:
 
135
                                        Process.Start (string.Format ("tar -czf {0} {1} ", String.Concat (file,".tar.gz"), file));
 
136
                                        break;
 
137
                        }
 
138
                }
 
139
 
 
140
                private string EscapeString (string str)
 
141
                {
 
142
                        return str.Replace (" ", "\\ ")
 
143
                                  .Replace ("'", "\\'");
 
144
                }
 
145
        }
 
146
}