Reputation: 883
edit: About the path-problem. I will try to get there later. For now I just need help for zip a file. Could not find a way to do this yet.
Im currently going through a few basics and I don't know what I have to look for to get to where I want to be. So, what are my goals?:
I want to create a name.json
file here C:\Users\Username\Desktop
Then I want to compress name.json
to an zip file.
I also created another file Testfile.zip
on my Desktop. I want to unzip that file.
So far I created a name.json
file. But I cannot find a solution on how to create one on the desktop.
I could not find a solution on compressing name.json so far.
public MainPage()
{
this.InitializeComponent();
createJson();
UnzipFile();
}
public async void createJson()
{
string text = "This text";
string json = JsonConvert.SerializeObject(text);
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("name.json");
await FileIO.WriteTextAsync(file, json);
}
public async void UnzipFile()
{
var localFolder = ApplicationData.Current.LocalFolder;
var archive = await localFolder.GetFileAsync("Testfile.zip");
ZipFile.ExtractToDirectory(archive.Path, localFolder.Path);
}
Upvotes: 1
Views: 1561
Reputation: 39072
Working with .zip
files is possible using System.IO.Compression.ZipArchive
and related classes. You are already using ZipFile.ExtractToDirectory
which is the right approach here. However, the issue you are facing is rather related to permissions.
UWP apps are sandboxed which means they are not allowed to touch any filesystem location by default. This is to ensure better security and easier uninstallation of apps. This however means you cannot easily create a file on the user's desktop, as your app does not have access there. The only folders your app can freely access are those accessible by ApplicationData.Current
and then those it declares access to in application manifest. You can also declare broad filesystem access here to get access to all locations on the PC.
To further complicate this, there are two types of I/O APIs in UWP. The modern StorageFile
API which is async
enabled, but tad slower, and the classic file I/O APIs in C# which includes ZipFile
and ZipArchive
. The main disadvantage of the classic APIs is that they always have access only to application folders and you can never access any other system paths, even if you declare broad filesystem access.
However, even without declaring broad filesystem access capability you can manually get access to the folder/file of user's choosing using FolderPicker
, FileOpenPicker
and FileSavePicker
. Using these you can let the user choose the destination where you will save the file or open a file.
Finally - to circumvent the limitation of not being able to use the classic file I/O APIs, you can first unzip the .zip
file in a temporary folder inside ApplicationData.Current.LocalFolder
and then use the StorageFile.MoveAndReplaceAsync(IStorageFile)
or StorageFile.MoveAsync(IStorageFolder)
method to move the files to the location the user has chosen using FileSavePicker
.
For further info you can check out this blog post with a tutorial on using .zip
in UWP.
Upvotes: 4