1
// CreateArchiveAction.cs
3
// Copyright (C) 2008 [name of author]
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.
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.
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
23
using System.Collections.Generic;
24
using System.Diagnostics;
30
public enum ArchiveType { GZIP, BZIP2, TAR, ZIP };
32
public class ArchiveAction : AbstractAction {
34
public ArchiveAction()
38
public override string Name {
39
get { return "Create archive"; }
42
public override string Description {
43
get { return "Create an archive with the selected item"; }
46
public override string Icon {
47
get { return "file-roller"; }
50
public override Type[] SupportedItemTypes {
58
public override bool SupportsItem (IItem item)
63
public override Type[] SupportedModifierItemTypes {
66
typeof(ArchiveTypeItem),
71
public override bool ModifierItemsOptional {
75
public override IItem[] DynamicModifierItemsForItem (IItem item)
77
List<IItem> items = new List<IItem> ();
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();
91
public override IItem[] Perform (IItem[] items, IItem[] modItems)
93
if ( modItems.Length > 0 )
94
Archive ((items[0] as IFileItem), (modItems[0] as ArchiveTypeItem).ArchiveType);
96
Archive ((items[0] as IFileItem), 0);
100
private void Archive ( IFileItem item, int archiveType)
105
if ( Directory.Exists(item.Path) ) {
106
path = item.Path.Replace(item.Name,"");
110
path = item.Path.Replace(item.Name,"");
111
file = String.Concat (System.IO.Path.GetFileName (path.Substring (0, path.Length -1)),
115
path = item.Path.Replace(file, "");
118
path = EscapeString (path);
119
file = EscapeString (file);
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));
125
case (int)ArchiveType.BZIP2:
126
Process.Start (string.Format ("tar -cjf {0} -C {1} {2}", String.Concat (item.Name ,".tar.bz2"), path, file));
128
case (int)ArchiveType.TAR:
129
Process.Start (string.Format ("tar -cf {0} -C {1} {2}", String.Concat (item.Name ,".tar"), path, file));
131
case (int)ArchiveType.ZIP:
132
Process.Start (string.Format ("zip {0} {1} ", file , path));
135
Process.Start (string.Format ("tar -czf {0} {1} ", String.Concat (file,".tar.gz"), file));
140
private string EscapeString (string str)
142
return str.Replace (" ", "\\ ")
143
.Replace ("'", "\\'");