~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to Archive/src/ExtractArchiveAction.cs

Adding missing files and Archive plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ExtractArchiveAction.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
        
 
31
        public class ExtractAction : AbstractAction {
 
32
                
 
33
                public ExtractAction()
 
34
                {
 
35
                }
 
36
                
 
37
                public override string Name {
 
38
                        get { return "Extract archive"; }
 
39
                }
 
40
                
 
41
                public override string Description {
 
42
                        get { return "Extract an archive to a given folder"; }
 
43
                }
 
44
        
 
45
                public override string Icon {
 
46
                        get { return "file-roller"; }
 
47
                }
 
48
                
 
49
                public override Type[] SupportedItemTypes {
 
50
                        get {
 
51
                                return new Type[] {                             
 
52
                                        typeof (IFileItem),
 
53
                                };
 
54
                        }
 
55
                }
 
56
                
 
57
                public override bool SupportsItem (IItem item) 
 
58
                {
 
59
                        return IsArchive (item as IFileItem);
 
60
                }
 
61
                        
 
62
                public override bool SupportsModifierItemForItems (IItem [] items, IItem modItem)
 
63
                {
 
64
                        return FileItem.IsDirectory (modItem as IFileItem);
 
65
                }
 
66
 
 
67
                public override Type[] SupportedModifierItemTypes {
 
68
                        get {
 
69
                                return new Type[] {
 
70
                                        typeof(IFileItem),
 
71
                                };
 
72
                        }
 
73
                }
 
74
                
 
75
                public override bool ModifierItemsOptional {
 
76
                        get { return false; }
 
77
                }
 
78
 
 
79
                public override IItem[] DynamicModifierItemsForItem (IItem item)
 
80
                {
 
81
                        return null;       
 
82
                }
 
83
                
 
84
                public override IItem[] Perform (IItem[] items, IItem[] modItems)
 
85
                {
 
86
                        ExtractArchive ( (items[0] as IFileItem), (modItems[0] as IFileItem));
 
87
                        return null;
 
88
                }
 
89
 
 
90
                private bool IsArchive (IFileItem item)
 
91
                {
 
92
                        return item.Path.EndsWith(".tar.gz") ||
 
93
                               item.Path.EndsWith (".tar.bz2") ||
 
94
                               item.Path.EndsWith (".tar") ;
 
95
                
 
96
                }
 
97
 
 
98
                private void ExtractArchive ( IFileItem archive, IFileItem where)
 
99
                {
 
100
                        if ( archive.Name.EndsWith ("tar.gz"))
 
101
                                Process.Start (String.Format ("tar -xzf {0} -C {1}", EscapeString(archive.Path), EscapeString(where.Path)));  
 
102
                        else if ( archive.Name.EndsWith ("tar.bz2"))
 
103
                                Process.Start (String.Format ("tar -xjf {0} -C {1}", EscapeString(archive.Path), EscapeString(where.Path)));
 
104
                        else if (archive.Name.EndsWith ("tar"))
 
105
                                Process.Start (String.Format ("tar -xf {0} -C {1}", EscapeString (archive.Path), EscapeString(where.Path)));
 
106
                                               
 
107
                }
 
108
 
 
109
                private string EscapeString (string str)
 
110
                {
 
111
                        return str.Replace (" ", "\\ ")
 
112
                                  .Replace ("'", "\\'");
 
113
                }
 
114
        }
 
115
}