简体   繁体   中英

UWP broadFileSystemAccess using System.IO to read and extract rar files

I am targeting build 17134 and trying to use system.IO to get the files in a directory. I'm also using sharpcompress to extract .rar files. From my understanding/what I've read online the new restricted capability "broadFileSystemAccess" should allow my app full access to any directory. My code is below and ATM I pass in a hard coded directory on my E drive.

    public void DirSearch(string sDir)
    {
        var ext = new List<string> { "*.rar", "*.zip" };

        foreach (String fileExtension in ext)
        {
            foreach (String file in Directory.EnumerateFiles(sDir, fileExtension, SearchOption.AllDirectories))
            {
                var archive = ArchiveFactory.Open(file);
                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                    {
                        Console.WriteLine(entry.Key);
                        entry.WriteToDirectory(@"E:\temp\", new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
                    }
                }

                Debug.WriteLine(file);
            }
        }
    }

This does not seem to work for me as I'm always getting:

System.UnauthorizedAccessException: 'Access to the path 'E:\\rars' is denied.'

I've also tried using the folder picker + storageFile which does list the rar files but then the following line throws an UnauthorizedAccessException error:

var archive = ArchiveFactory.Open(file);

How can I read and write to my E drive using System.IO and Sharpcompress using the broadFileSystemAccess capability? What am I missing?

As the introduction in the document broadFileSystemAccess capability and Accessing additional locations ,

This capability works for APIs in the Windows.Storage namespace.

So you still can not get the file directly by the file's path like E:\\ using the sharpcompress Apis. But you can use the StorageFolder.GetFolderFromPathAsync Api to get the folder then use the ArchiveFactory.Open(Stream stream, ReaderOptions readerOptions = null) method to exact the file.

Here are some example code, you can put it as a reference.

public async void DirSearch(string sDir)
{
    var ext = new List<string> { ".rar", ".zip" };
    var folder = await StorageFolder.GetFolderFromPathAsync(sDir);
    var exactFile = await folder.CreateFileAsync("newFile", CreationCollisionOption.GenerateUniqueName);
    using (var writeStream = await exactFile.OpenStreamForWriteAsync())
    {
        foreach (var file in await folder.GetFilesAsync())
        {
            if (ext.Contains(file.FileType))
            {
                var stream = await file.OpenReadAsync();
                var archive = ArchiveFactory.Open(stream.AsStream());
                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                    {
                        Console.WriteLine(entry.Key);
                        entry.WriteTo(writeStream);
                    }
                }
                Debug.WriteLine(file);
            }
        }
    }
}

--- Update ---

Since UWP app have file access permission, you can not use none Windows.Storage namespace api to access the file path like E:\\foldre\\ directly, but your app have the full permission to the local folder, you can use IArchiveEntry.WriteToDirectory(string destinationDirectory) method to exact the files to the local folder, then you can copy the files to the folder location you want, try the following code,

public async void DirSearch(string sDir)
{
    var ext = new List<string> { ".rar", ".zip" };
    var folder = await StorageFolder.GetFolderFromPathAsync(sDir);
    var ExactLocalFolder =await ApplicationData.Current.LocalFolder.CreateFolderAsync("ExactLocalFolder");
    foreach (var file in await folder.GetFilesAsync())
    {
        if (ext.Contains(file.FileType))
        {
            var stream = await file.OpenReadAsync();
            var archive = ArchiveFactory.Open(stream.AsStream());
            foreach (var entry in archive.Entries)
            {
                if (!entry.IsDirectory)
                {
                    Console.WriteLine(entry.Key);
                    //extract the files to local folder
                    entry.WriteToDirectory(ExactLocalFolder.Path);
                }
            }
        }
    }
    //copy the files to the folder that you want to save the extract file into
    foreach (var file in await ExactLocalFolder.GetFilesAsync())
    {
        await file.CopyAsync(folder);
    }
    //delete the folder in the local folder
    await ExactLocalFolder.DeleteAsync();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM