-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompactor.cs
166 lines (152 loc) · 7.26 KB
/
Compactor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using System.Web.Script.Serialization;
using Ionic.Zip;
using System.Drawing;
using Vip.Notification;
namespace Holtz_Compactador
{
public class Compactor
{
private static List<string> ExtensionsExceptions = new List<string>();
private static List<string> FoldersExceptions = new List<string>();
private static string BaseDirectory = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
private static string PathExtensionsJson = Directory.GetParent(Directory.GetParent(BaseDirectory).FullName).FullName + @"\Extensoes.json";
private static string PathFoldersJson = Directory.GetParent(Directory.GetParent(BaseDirectory).FullName).FullName + @"\Pastas.json";
private static string WindowsFolderTemp = Path.GetTempPath();
private static char separator = Path.DirectorySeparatorChar;
public static void Compact(string sourceFolder, string targetFolder, string targetFileName, string extension)
{
string FinishTotalName = targetFolder + @"\" + targetFileName + extension.ToLower();
try
{
if (!Directory.Exists(sourceFolder)) { throw new CompactorException("Diretório de ORIGEM não existe. Verifique!"); }
if (File.Exists(FinishTotalName)) { throw new CompactorException("Já existe um arquivo compactado com este nome e extensão nesta pasta de destino. Verifique!"); }
if (!File.Exists(PathExtensionsJson)) { File.Create(PathExtensionsJson); }
if (!File.Exists(PathFoldersJson)) { File.Create(PathFoldersJson); }
if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); }
GetExtensionsAndFoldersExceptions();
LoadExcecoes.Start();
// Structure Folders and Files
IEnumerable<string> folders = Directory.EnumerateDirectories(sourceFolder, "*.*", SearchOption.AllDirectories);
IEnumerable<string> files = Directory.EnumerateFiles(sourceFolder, "*.*", SearchOption.AllDirectories);
List<string> ListFiles = files.ToList(); //Para conseguir excluir
//Excluindo conforme exceções de PASTAS
foreach (string folder in folders)
{
//string folderName = Path.GetDirectoryName(folder); //pega primeira pasta exemplo web de c:\web
string[] temp = folder.Split(separator);
IEnumerable<string> tempEnum = temp.AsEnumerable();
string LastFolderName = tempEnum.Last(); //pega ultimo pq vai ser o nome da pasta
foreach (string item in LoadExcecoes.ParPastasN)
{
if (item.ToLower() == LastFolderName.ToLower())
{
foreach (string itemList in files) //ou AuxListFiles
{
if (itemList.Contains(folder))
{
ListFiles.Remove(itemList);
//break;
}
}
}
}
}
//Excluindo conforme exceções de EXTENSÕES
foreach (string file in files)
{
string ext = Path.GetExtension(file);
foreach (string item in LoadExcecoes.ParExtensoesN)
{
if (item.ToLower() == ext.ToLower())
{
ListFiles.Remove(file);
}
}
}
using (ZipFile FileCompacted = new ZipFile(FinishTotalName))
{
//compactando
foreach (string file in ListFiles)
{
int lengthSource = sourceFolder.Length + 1; //Pega o caminho + a '\'
int lengthItem = file.Length -1; //Pega o caminho - a '\'
string directoryPathInArqchive = file.Substring(lengthSource, lengthItem - lengthSource);
if (!directoryPathInArqchive.Contains(separator)) //dignifica que este "file" está na raiz
{
directoryPathInArqchive = "";
}
else
{ //significa que tem folder, precisa deixar só a pasta, e não o arquivo junto
string[] v = file.Split(separator);
string s = v.LastOrDefault(); //aqui pega o nome do arquivo "exemplo.dll"
directoryPathInArqchive = directoryPathInArqchive.Substring(0, directoryPathInArqchive.Length - s.Length);
//TESTE
}
FileCompacted.AddItem(file, directoryPathInArqchive);
}
//Save File compacted
try
{
FileCompacted.Save();
Alert.ShowSucess("Arquivo compactado com sucesso!");
}
catch
{
throw;
}
}
}
catch (CompactorException e)
{
MessageBox.Show(e.Message);
}
catch (IOException e) //capturar exceções mais genéricas
{
MessageBox.Show(e.Message);
}
catch (Exception e) //capturar exceções mais genéricas
{
MessageBox.Show(e.Message);
}
}
private static void GetExtensionsAndFoldersExceptions()
{
//Load extensions exceptions
//ExtensionsExceptions.Clear();
JavaScriptSerializer serializer = new JavaScriptSerializer();
string Extensionsjson = File.ReadAllText(PathExtensionsJson);
if (Extensionsjson.Length > 2)
{
dynamic resultado = serializer.DeserializeObject(Extensionsjson);
foreach (KeyValuePair<string, object> entry in resultado)
{
var key = entry.Key;
var value = entry.Value as string;
ExtensionsExceptions.Add(value);
}
}
serializer = null;
//Load folders exceptions
//FoldersExceptions.Clear();
serializer = new JavaScriptSerializer();
string Foldersjson = File.ReadAllText(PathFoldersJson);
if (Foldersjson.Length > 2)
{
dynamic resultado = serializer.DeserializeObject(Foldersjson);
foreach (KeyValuePair<string, object> entry in resultado)
{
var key = entry.Key;
var value = entry.Value as string;
FoldersExceptions.Add(value);
}
}
}
}
}