I want to unzip a .zip file in removable sd card. I am selecting the .zip file using file picker. After selecting the file I get a path like this:
"content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip" and after selecting the folder to unzip that file on selected location it gives some path like this "content://com.android.externalstorage.documents/tree/13E7-3B0A%3ALOST.DIR". After that i am using a method to unzip .zip file System.IO.Compression.ZipFile.ExtractToDirectory("content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip", "content://com.android.externalstorage.documents/tree/13E7-3B0A%3ALOST.DIR");
but it gives the error which is mentioned below:
Unhandled Exception: System.UnauthorizedAccessException: Access to the path "/content:" is denied. occurred
I have also tried to give runtime permission but still, I am getting the same error. Please help me because I have given too much time on this
code for picking a file and getting a path
private async void SelectZipFile(object sender, EventArgs e)
{
string[] allowedTypes = { "application/zip" };//i use allowedType in PickFile for selecting only .zip file
FileData file = await CrossFilePicker.Current.PickFile(allowedTypes);
if (file != null)
{
//string filename = file.FileName;
_zipFile = file.FilePath;//"content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip"
txt1.Text = file.FileName;
//int indx=_zipFile.IndexOf(filename);
//string ss=_zipFile.Remove(indx);
//_zipFile = ss + "/" + filename;
}
}
code for unzip file
public static void Unzip(String _location)
{
if (_location != null)
{
Label lbl1 = new Label();
lbl1.Text = "Extracting files.......";
System.IO.Compression.ZipFile.ExtractToDirectory(_zipFile, _location);
lbl1.IsVisible = false;
}
}
code for getting location
public void SelectDirectory()
{
try
{
activity = Xamarin.Forms.Forms.Context as MainActivity;
activity.Intent = new Intent();
activity.Intent.SetAction(Intent.ActionOpenDocumentTree);
activity.StartActivityForResult(activity.Intent, REQUEST_CODE_OPEN_DIRECTORY);
activity.ActivityResult += (object sender, ActivityResultEventArgs e) =>
{
FolderPath = e.Intent.Data;
//string location = FolderPath.ToString();
//string[] arr = FolderPath.LastPathSegment.Split(":");
//int indx = location.IndexOf(arr[1]);
//string ss = location.Remove(indx);
//location = ss + "/" + arr[1];
MainPage.Unzip(FolderPath.ToString());
};
}
catch(Exception ex)
{
string str = ex.ToString();
}
}