BundlesManager.cs
9.82 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Net;
using System.IO;
using System.IO.Compression;
namespace AtualizadorVLibras{
public class TimeoutWebClient : WebClient{
protected override WebRequest GetWebRequest(Uri address){
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.Timeout = BundlesManager.timeout; //5 min
return request;
}
}
static class BundlesManager{
//timeout para verificação de existência e data de última verificação dos pacotes
public static int timeout = 5 * 60 * 1000; //5 min
public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite){
if (!overwrite){
archive.ExtractToDirectory(destinationDirectoryName);
return;
}
foreach (ZipArchiveEntry file in archive.Entries){
try{
string completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
string directory = Path.GetDirectoryName(completeFileName);
Console.WriteLine("Extraindo "+file.FullName+".");
if (!Directory.Exists(directory)){
Directory.CreateDirectory(directory);
}
if (file.Name != ""){
file.ExtractToFile(completeFileName, true);
}
}catch(Exception e){
Console.WriteLine("Erro na extração: {0}",e);
continue;
}
}
}
//File.Exists(path);
private static bool RemoteFileExists(string url){
bool result = false;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
request.Timeout = timeout;
HttpWebResponse response = null;
try{
response = (HttpWebResponse)request.GetResponse();
result = (response.StatusCode == HttpStatusCode.OK);
}catch(Exception e){
//Console.WriteLine("Arquivo não existe no servidor: {0}",e);
}finally{
if(response != null){
response.Close();
}
}
return result;
}
private static bool RemoteIsNewerThanLocal(string url, string path){
bool result = false;//resultado padrão
//informações do arquivo
FileInfo sourceFile = new FileInfo(path);
//solicitação do arquivo remoto
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
request.Timeout = timeout;
HttpWebResponse response = null;
try{
response = (HttpWebResponse)request.GetResponse();
result = (response.LastModified > sourceFile.LastWriteTime);
}catch(Exception e){
//Console.WriteLine("Erro: {0}",e);
}finally{
if(response != null){
response.Close();
}
}
return result;
}
private static void DownloadFileToPath(string site, string path){
try{
TimeoutWebClient webClient = new TimeoutWebClient();
webClient.DownloadFile(site, path);
}catch(Exception e){
Console.WriteLine("Erro: {0}\nAperte alguma tecla para continuar.", e);
}
}
public static int DownloadBundlesPackages(string url, string rootPath, string extractPath){
//rootPath é o path dos arquivos .zip locais
//url é o path dos arquivos .zip remotos
//extractPath é o path da pasta Bundles
string ext = @".zip";
int i = 1;
string remote;
string local;
while(true){
Console.WriteLine("Verificando arquivo "+i+" zip dos pacotes de sinais.");
remote = url+i.ToString()+ext;
local = rootPath+i.ToString()+ext;
if(!RemoteFileExists(remote)){
//NECESSÁRIO PARA EVITAR IndexOutOfRange
i--;
break;
}else{
if(!File.Exists(local)){
Console.WriteLine("Baixando o "+i+"o pacote de sinais.");
DownloadFileToPath(remote, local);
}else{
if(RemoteIsNewerThanLocal(remote, local)){
Console.WriteLine("Houve um patch nesse pacote: baixando o "+i+"o pacote de sinais.");
DownloadFileToPath(remote, local);
//DELETA ARQUIVO DE VERIFICAÇÃO PARA QUE HAJA NOVA EXTRAÇÃO
File.Delete(extractPath + i);
}
}
}
i++;
}
return i;
}
public static void DownloadListaSinais(string url){
string sinais = "sinais.txt";
string local = @"..\Bundles\";
// DownloadFileIfNewer(url, local, sinais);
if(RemoteFileExists(url+sinais) && (!File.Exists(local+sinais) || RemoteIsNewerThanLocal(url+sinais, local+sinais))){
DownloadFileToPath(url+sinais, local+sinais);
}
}
/*public static void ExtractBundlesPackages(string rootPath, string extractPath, int qtd){
string ext = @".zip";
bool exists = System.IO.Directory.Exists(extractPath);
if(!exists){
System.IO.Directory.CreateDirectory(extractPath);
}
while(qtd > 0){
if(!File.Exists(extractPath+qtd.ToString())){
string zip = rootPath+qtd.ToString()+ext;
Console.WriteLine("Extraindo o arquivo: "+zip);
ZipFile.ExtractToDirectory(zip, extractPath);
//CRIA UM ARQUIVO 1(SEM EXTENSÃO), 2, 3... NA PASTA BUNDLES PARA VERIFICAÇÃO DE EXTRAÇÃO PRÉVIA
//SÓ É CRIADO APÓS EXTRAÇÃO DE TODOS OS ARQUIVOS DE CADA ZIP
Console.WriteLine("Extração do arquivo "+zip+" concluída.");
File.Create(extractPath+qtd).Close();
}
qtd--;
}
}*/
public static void DownloadFileIfNewer(string remote, string local, string fileName){
if(RemoteFileExists(remote+fileName) && (!File.Exists(local+fileName) || RemoteIsNewerThanLocal(remote+fileName, local+fileName))){
DownloadFileToPath(remote+fileName, local+fileName);
if(File.Exists(local+fileName.Remove(fileName.Length-4))){
File.Delete(local+fileName.Remove(fileName.Length-4));
}
}
}
public static void ExtractZip(string local, string extractPath, string fileName){
//if(!File.Exists(local+fileName.Remove(fileName.Length-4))){
bool dirExists = System.IO.Directory.Exists(extractPath);
if(!dirExists){
Directory.CreateDirectory(extractPath);
}
try{
//if(!File.Exists(extractPath+fileName.Remove(fileName.Length-4))){
ZipArchive zip = ZipFile.Open(Path.Combine(local,fileName), ZipArchiveMode.Read, System.Text.Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.OEMCodePage));
ExtractToDirectory(zip, extractPath, true);
// File.Create(extractPath+fileName.Remove(fileName.Length-4)).Close();
//}
}catch(Exception e){
Console.WriteLine("Ocorreu um erro durante a extraçao: {0}\nPressione uma tecla para continuar.",e);
Console.ReadKey();
}
//}
}
//ZipArchive archive = ZipFile.Open(fileAbsolute, ZipArchiveMode.Read, System.Text.Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.OEMCodePage));
public static void ExtractBundlesPackagesOverWrite(string url, string rootPath, string extractPath, int qtd){
string ext = @".zip";
bool exists = System.IO.Directory.Exists(extractPath);
if(!exists){
System.IO.Directory.CreateDirectory(extractPath);
}
while(qtd > 0){
try{
if(!File.Exists(extractPath+qtd.ToString())){
string fileAbsolute = rootPath+qtd.ToString()+ext;
ZipArchive zip = ZipFile.Open(fileAbsolute, ZipArchiveMode.Read, System.Text.Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.OEMCodePage));
Console.WriteLine("Extraindo o arquivo: "+zip);
//ZipFile.ExtractToDirectory(zip, extractPath);
ExtractToDirectory(zip, extractPath, true);
//CRIA UM ARQUIVO 1(SEM EXTENSÃO), 2, 3... NA PASTA BUNDLES PARA VERIFICAÇÃO DE EXTRAÇÃO PRÉVIA
//SÓ É CRIADO APÓS EXTRAÇÃO DE TODOS OS ARQUIVOS DE CADA ZIP
Console.WriteLine("Extração do arquivo "+zip+" concluída.");
File.Create(extractPath+qtd).Close();
}
qtd--;
}catch(Exception e){
Console.WriteLine("Erro na extração do "+qtd.ToString()+ext+"\nErro: {0}",e);
Console.WriteLine("Tentando baixar novamente o arquivo: "+qtd.ToString()+ext);
DownloadFileToPath(url+qtd.ToString()+ext, rootPath+qtd.ToString()+ext);
continue;
}
}
}
}
}