-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
399 lines (354 loc) · 15.2 KB
/
MainPage.xaml.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Documents;
using ListViewInteraction.DataSource;
using ListViewInteraction.Routines;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Data.Xml.Dom;
using Windows.UI.Xaml.Media.Imaging;
using System.IO;
using System.IO.Compression;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using ListViewInteraction.Common;
using Windows.UI.ApplicationSettings;
namespace ListViewInteraction
{
partial class MainPage : ListViewInteraction.Common.LayoutAwarePage
{
#region Properties
private string _rootNamespace;
public string RootNamespace
{
get { return _rootNamespace; }
set { _rootNamespace = value; }
}
#endregion
FolderData storeData = null;
static StorageFolder storfolder;
public MainPage()
{
InitializeComponent();
//SetFeatureName(FEATURE_NAME);
Loaded += new RoutedEventHandler(MainPage_Loaded);
Window.Current.SizeChanged += new WindowSizeChangedEventHandler(MainPage_SizeChanged);
DisplayProperties.LogicalDpiChanged += new DisplayPropertiesEventHandler(DisplayProperties_LogicalDpiChanged);
NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
SettingsPane.GetForCurrentView().CommandsRequested += onCommandsRequested;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// Figure out what resolution and orientation we are in and respond appropriately
//CheckResolutionAndViewState();
storeData = new FolderData();
//ItemGridView.ItemsSource = storeData.Collection;
}
#region Resolution and orientation code
void DisplayProperties_LogicalDpiChanged(object sender)
{
//CheckResolutionAndViewState();
}
void CheckResolutionAndViewState()
{
VisualStateManager.GoToState(this, ApplicationView.Value.ToString() + DisplayProperties.ResolutionScale.ToString(), false);
}
void MainPage_SizeChanged(Object sender, Windows.UI.Core.WindowSizeChangedEventArgs args)
{
//CheckResolutionAndViewState();
}
#endregion
private void SetFeatureName(string str)
{
//FeatureName.Text = str;
}
async void Footer_Click(object sender, RoutedEventArgs e)
{
await Windows.System.Launcher.LaunchUriAsync(new Uri(((HyperlinkButton)sender).Tag.ToString()));
}
public void NotifyUser(string strMessage, NotifyType type)
{
}
public void DoNavigation(Type inPageType, Frame inFrame, Type outPageType, Frame outFrame)
{
}
void onCommandsRequested(SettingsPane settingsPane, SettingsPaneCommandsRequestedEventArgs e)
{
SettingsCommand defaultsCommand = new SettingsCommand("privacy", "Privacy",
(handler) =>
{
Privacy sf = new Privacy();
sf.Show();
});
e.Request.ApplicationCommands.Add(defaultsCommand);
}
private async Task<StorageFolder> extractFiles(StorageFile zipfilename)
{
StorageFolder storfolder = null;
Uri _baseUri = new Uri("ms-appx:///");
try
{
// Create stream for compressed files in memory
using (MemoryStream zipMemoryStream = new MemoryStream())
{
using (Windows.Storage.Streams.IRandomAccessStream zipStream = await zipfilename.OpenAsync(FileAccessMode.Read))
{
// Read compressed data from file to memory stream
using (Stream instream = zipStream.AsStreamForRead())
{
byte[] buffer = new byte[1024];
while (instream.Read(buffer, 0, buffer.Length) > 0)
{
zipMemoryStream.Write(buffer, 0, buffer.Length);
}
}
}
storfolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(zipfilename.DisplayName, CreationCollisionOption.GenerateUniqueName);
// Create zip archive to access compressed files in memory stream
using (ZipArchive zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
{
int count = zipArchive.Entries.Count;
int i = 0;
Item item;
// For each compressed file...
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{
i++;
// ... read its uncompressed contents
using (Stream entryStream = entry.Open())
{
if (entry.Name != "")
{
item = new Item();
string fileName = entry.FullName.Replace("/", @"\");
byte[] buffer = new byte[entry.Length];
entryStream.Read(buffer, 0, buffer.Length);
// Create a file to store the contents
StorageFile uncompressedFile = await storfolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
// Store the contents
using (IRandomAccessStream uncompressedFileStream = await uncompressedFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (Stream outstream = uncompressedFileStream.AsStreamForWrite())
{
outstream.Write(buffer, 0, buffer.Length);
outstream.Flush();
}
}
item.Link = zipfilename.DisplayName + "/" + entry.FullName;
item.Title = entry.Name;
String[] ext = entry.Name.Split('.');
if (ext.Length > 1)
{
item.SetImage(_baseUri, Routines.Routines.getImageType("." + ext[1]));
item.FileType = ext[1] + " File";
}
else
{
item.SetImage(_baseUri, Routines.Routines.getImageType("."));
item.FileType = "Config File";
}
storeData.Collection.Add(item);
int x = ((i * 100) / count);
copy1.Text = "Extracting files " + x + "%";
}
}
}
}
}
}
catch (System.Exception ex1)
{
ProgressRing1.IsActive = false;
DisplayMsg("Error extracting files!!!");
if (storfolder != null) storfolder.DeleteAsync();
return null;
}
finally
{
ProgressRing1.IsActive = false;
ItemGridView.ItemsSource = storeData.Collection;
chzip.IsEnabled = false;
exall.IsEnabled = true;
exsel.IsEnabled = false;
clsel.IsEnabled = false;
clcon.IsEnabled = true;
zipfilename.DeleteAsync();
}
return storfolder;
}
public async Task extracter(StorageFile filename)
{
ProgressRing1.IsActive = true;
storfolder = await extractFiles(filename);
if (storfolder == null) return;
}
private async void chooseFile(object sender, RoutedEventArgs e)
{
copy1.Text = "Extracting files";
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(".zip");
StorageFile file = await openPicker.PickSingleFileAsync();
if (null != file)
{
chzip.IsEnabled = false;
try
{
StorageFile savedFile = await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder, file.Name, NameCollisionOption.ReplaceExisting);
await extracter(savedFile);
Windows.Storage.FileProperties.BasicProperties fileprop = await file.GetBasicPropertiesAsync();
this.copy1.Text = file.DisplayName + ".zip | " + ((fileprop.Size) / 1024).ToString() + " KB";
}
catch
{
DisplayMsg("File extraction failed!!!");
}
}
}
public async void ExtractAll_Click_1(object sender, RoutedEventArgs e)
{
StorageFolder newFolder = null;
copy1.Text = "Extracting all files";
try
{
Windows.Storage.Search.CommonFileQuery query = Windows.Storage.Search.CommonFileQuery.OrderByName;
IReadOnlyList<StorageFile> files = await storfolder.GetFilesAsync(query);
newFolder = await Windows.Storage.DownloadsFolder.CreateFolderAsync(storfolder.Name, CreationCollisionOption.GenerateUniqueName);
foreach (StorageFile file in files)
{
await file.CopyAsync(newFolder, file.Name, NameCollisionOption.GenerateUniqueName);
}
}
catch
{
DisplayMsg("Error copying files to Downloads!!!");
}
copy1.Text = "Copied to Downloads/ZipX/" + newFolder.DisplayName;
}
public static String[] parseString(String path)
{
String[] finalPath = new String[2];
String[] tempPath = null;
int i;
if (path.ToString().Contains("/"))
{
tempPath = path.Split('/');
for (i = 0; i < tempPath.Length - 1; i++)
{
finalPath[0] = finalPath[0] + tempPath[i] + "/";
}
finalPath[1] = tempPath[i];
}
else
{
finalPath[0] = "";
finalPath[1] = path;
}
return finalPath;
}
private async void Selection_Click_1(object sender, RoutedEventArgs e)
{
copy1.Text = "Copying....";
StorageFolder storeFolder = null;
try
{
storeFolder = await DownloadsFolder.CreateFolderAsync(storfolder.Name.ToString(), CreationCollisionOption.GenerateUniqueName);
if (ItemGridView.SelectedItems.Count != 0)
{
foreach (Item item in ItemGridView.SelectedItems)
{
//Uri uri = new Uri("ms-appdata:///local/" + item.Link);
String[] filepath = parseString(item.Link);
StorageFolder imageFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFolderAsync(filepath[0].Replace("/", "\\"));
StorageFile storageFile = await imageFolder.GetFileAsync(filepath[1]);
//StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
await storageFile.CopyAsync(storeFolder, item.Title, NameCollisionOption.GenerateUniqueName);
}
}
}
catch
{
DisplayMsg("Error copying selected files to Downloads!!!");
}
ItemGridView.SelectedItems.Clear();
copy1.Text = "Copied to Downloads/ZipX/" + storeFolder.DisplayName;
}
private void ClearSel_Click_1(object sender, RoutedEventArgs e)
{
ItemGridView.SelectedItems.Clear();
exsel.IsEnabled = false;
}
private async void Clear_Click_1(object sender, RoutedEventArgs e)
{
try
{
storeData.Collection.Clear();
ItemGridView.ItemsSource = null;
StorageFolder localfolder = ApplicationData.Current.LocalFolder;
var folders = await localfolder.GetFoldersAsync();
foreach (StorageFolder folder in folders)
{
await folder.DeleteAsync();
}
}
catch
{
DisplayMsg("Error deleting temporary files!!!");
}
chzip.IsEnabled = true;
exall.IsEnabled = false;
exsel.IsEnabled = false;
clsel.IsEnabled = false;
clcon.IsEnabled = false;
copy1.Text = "Select File";
}
private void ItemGridView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
copy1.Text = ItemGridView.SelectedItems.Count.ToString() + " files selected";
if (ItemGridView.SelectedItems.Count == 0)
{
exsel.IsEnabled = false;
clsel.IsEnabled = false;
}
else
{
exsel.IsEnabled = true;
clsel.IsEnabled = true;
}
}
public static async void DisplayMsg(String errormsg)
{
// Create the message dialog and set its content; it will get a default "Close" button since there aren't any other buttons being added
MessageDialog msg = new MessageDialog("Oops!!!!");
msg.Title = errormsg;
try
{
IUICommand result = await msg.ShowAsync();
}
catch
{ }
}
}
public enum NotifyType
{
StatusMessage,
ErrorMessage
};
}