private StreamedFileDataRequestedHandler ZipDataStreamingHandler(string name) { return(async request => { try { // If called from here it fails with Access Denied?! //var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); var hFile = await NativeFileOperationsHelper.OpenProtectedFileForRead(ContainerPath); if (hFile.IsInvalid) { request.FailAndClose(StreamedFileFailureMode.CurrentlyUnavailable); return; } using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read))) { zipFile.IsStreamOwner = true; var znt = new ZipNameTransform(ContainerPath); var entry = zipFile.GetEntry(znt.TransformFile(name)); if (entry != null) { using (var inStream = zipFile.GetInputStream(entry)) using (var outStream = request.AsStreamForWrite()) { await inStream.CopyToAsync(outStream); await outStream.FlushAsync(); } request.Dispose(); } else { request.FailAndClose(StreamedFileFailureMode.CurrentlyUnavailable); } } } catch { request.FailAndClose(StreamedFileFailureMode.Failed); } }); }
private IAsyncOperation <ZipFile> OpenZipFileAsync(FileAccessMode accessMode, bool openProtected = false) { return(AsyncInfo.Run <ZipFile>(async(cancellationToken) => { bool readWrite = accessMode == FileAccessMode.ReadWrite; if (BackingFile != null) { return new ZipFile((await BackingFile.OpenAsync(accessMode)).AsStream()); } else { var hFile = openProtected ? await NativeFileOperationsHelper.OpenProtectedFileForRead(ContainerPath) : NativeFileOperationsHelper.OpenFileForRead(ContainerPath, readWrite); if (hFile.IsInvalid) { return null; } return new ZipFile((Stream) new FileStream(hFile, readWrite ? FileAccess.ReadWrite : FileAccess.Read)); } })); }