Esempio n. 1
0
        private Func <IRandomAccessStream, IAsyncOperation <bool> > WriteZipEntry(ZipFile zipFile)
        {
            return((stream) => AsyncInfo.Run((cancellationToken) => Task.Run(() =>
            {
                var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, true);
                if (hFile.IsInvalid)
                {
                    return true;
                }
                try
                {
                    var znt = new ZipNameTransform(ContainerPath);
                    var zipDesiredName = znt.TransformFile(Path);
                    var entry = zipFile.GetEntry(zipDesiredName);

                    zipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct));
                    if (entry != null)
                    {
                        zipFile.Delete(entry);
                    }
                    zipFile.Add(new StreamDataSource(stream), zipDesiredName);
                    zipFile.CommitUpdate();
                }
                catch (Exception ex)
                {
                    App.Logger.Warn(ex, "Error writing zip file");
                }
                return true;
            })));
        }
Esempio n. 2
0
        public override IAsyncOperation <IRandomAccessStreamWithContentType> OpenReadAsync()
        {
            return(AsyncInfo.Run <IRandomAccessStreamWithContentType>(async(cancellationToken) =>
            {
                var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath);
                if (hFile.IsInvalid)
                {
                    return null;
                }
                if (Path == ContainerPath)
                {
                    return new StreamWithContentType(new FileStream(hFile, FileAccess.Read).AsRandomAccessStream());
                }

                ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read));
                zipFile.IsStreamOwner = true;
                var znt = new ZipNameTransform(ContainerPath);
                var entry = zipFile.GetEntry(znt.TransformFile(Path));
                if (entry != null)
                {
                    var nsStream = new NonSeekableRandomAccessStream(zipFile.GetInputStream(entry), (ulong)entry.Size)
                    {
                        DisposeCallback = () => zipFile.Close()
                    };
                    return new StreamWithContentType(nsStream);
                }
                return null;
            }));
        }
Esempio n. 3
0
 public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace)
 {
     return(AsyncInfo.Run(async(cancellationToken) =>
     {
         var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath);
         if (hFile.IsInvalid)
         {
             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(Path));
             if (entry != null)
             {
                 using var hDestFile = fileToReplace.CreateSafeFileHandle(FileAccess.ReadWrite);
                 using (var inStream = zipFile.GetInputStream(entry))
                     using (var outStream = new FileStream(hDestFile, FileAccess.Write))
                     {
                         await inStream.CopyToAsync(outStream);
                         await outStream.FlushAsync();
                     }
             }
         }
     }));
 }
Esempio n. 4
0
 public override IAsyncOperation <IStorageItem> GetItemAsync(string name)
 {
     return(AsyncInfo.Run <IStorageItem>(async(cancellationToken) =>
     {
         var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath);
         if (hFile.IsInvalid)
         {
             return null;
         }
         using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)))
         {
             zipFile.IsStreamOwner = true;
             ZipEncoding ??= DetectFileEncoding(zipFile);
             var entry = zipFile.GetEntry(name);
             if (entry != null)
             {
                 var wnt = new WindowsNameTransform(ContainerPath);
                 if (entry.IsDirectory)
                 {
                     return new ZipStorageFolder(wnt.TransformDirectory(DecodeEntryName(entry, ZipEncoding)), ContainerPath, entry)
                     {
                         ZipEncoding = ZipEncoding
                     };
                 }
                 else
                 {
                     return new ZipStorageFile(wnt.TransformFile(DecodeEntryName(entry, ZipEncoding)), ContainerPath, entry);
                 }
             }
             return null;
         }
     }));
 }
Esempio n. 5
0
        public override IAsyncOperation <IInputStream> OpenSequentialReadAsync()
        {
            return(AsyncInfo.Run <IInputStream>(async(cancellationToken) =>
            {
                var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath);
                if (hFile.IsInvalid)
                {
                    return null;
                }
                if (Path == ContainerPath)
                {
                    return new FileStream(hFile, FileAccess.Read).AsInputStream();
                }

                ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read));
                zipFile.IsStreamOwner = true;
                var znt = new ZipNameTransform(ContainerPath);
                var entry = zipFile.GetEntry(znt.TransformFile(Path));
                if (entry != null)
                {
                    return new InputStreamWithDisposeCallback(zipFile.GetInputStream(entry))
                    {
                        DisposeCallback = () => zipFile.Close()
                    };
                }
                return null;
            }));
        }
Esempio n. 6
0
 public override IAsyncOperation <BaseStorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option)
 {
     return(AsyncInfo.Run <BaseStorageFile>(async(cancellationToken) =>
     {
         var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath);
         if (hFile.IsInvalid)
         {
             return null;
         }
         using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)))
         {
             zipFile.IsStreamOwner = true;
             var znt = new ZipNameTransform(ContainerPath);
             var entry = zipFile.GetEntry(znt.TransformFile(Path));
             if (entry != null)
             {
                 var destFolder = destinationFolder.AsBaseStorageFolder();
                 var destFile = await destFolder.CreateFileAsync(desiredNewName, option.Convert());
                 using (var inStream = zipFile.GetInputStream(entry))
                     using (var outStream = await destFile.OpenStreamForWriteAsync())
                     {
                         await inStream.CopyToAsync(outStream);
                         await outStream.FlushAsync();
                     }
                 return destFile;
             }
             return null;
         }
     }));
 }
Esempio n. 7
0
 public override IAsyncOperation <IInputStream> OpenSequentialReadAsync()
 {
     return(AsyncInfo.Run(async(cancellationToken) =>
     {
         var hFile = NativeFileOperationsHelper.OpenFileForRead(Path);
         return new FileStream(hFile, FileAccess.Read).AsInputStream();
     }));
 }
Esempio n. 8
0
 public override IAsyncOperation <IRandomAccessStream> OpenAsync(FileAccessMode accessMode)
 {
     return(AsyncInfo.Run <IRandomAccessStream>(async(cancellationToken) =>
     {
         var hFile = NativeFileOperationsHelper.OpenFileForRead(Path, accessMode == FileAccessMode.ReadWrite);
         return new FileStream(hFile, accessMode == FileAccessMode.ReadWrite ? FileAccess.ReadWrite : FileAccess.Read).AsRandomAccessStream();
     }));
 }
Esempio n. 9
0
 private static bool CheckAccess(string path)
 {
     return(SafetyExtensions.IgnoreExceptions(() =>
     {
         var hFile = NativeFileOperationsHelper.OpenFileForRead(path);
         if (hFile.IsInvalid)
         {
             return false;
         }
         using var stream = new FileStream(hFile, FileAccess.Read);
         return CheckAccess(stream);
     }));
 }
Esempio n. 10
0
 public override IAsyncOperation <IReadOnlyList <IStorageItem> > GetItemsAsync()
 {
     return(AsyncInfo.Run <IReadOnlyList <IStorageItem> >(async(cancellationToken) =>
     {
         var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath);
         if (hFile.IsInvalid)
         {
             return null;
         }
         using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)))
         {
             zipFile.IsStreamOwner = true;
             ZipEncoding ??= DetectFileEncoding(zipFile);
             var wnt = new WindowsNameTransform(ContainerPath, true); // Check with Path.GetFullPath
             var items = new List <IStorageItem>();
             foreach (var entry in zipFile.OfType <ZipEntry>())       // Returns all items recursively
             {
                 string winPath = System.IO.Path.GetFullPath(entry.IsDirectory ? wnt.TransformDirectory(DecodeEntryName(entry, ZipEncoding)) : wnt.TransformFile(DecodeEntryName(entry, ZipEncoding)));
                 if (winPath.StartsWith(Path.WithEnding("\\"))) // Child of self
                 {
                     var split = winPath.Substring(Path.Length).Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                     if (split.Length > 0)
                     {
                         if (entry.IsDirectory || split.Length > 1) // Not all folders have a ZipEntry
                         {
                             var itemPath = System.IO.Path.Combine(Path, split[0]);
                             if (!items.Any(x => x.Path == itemPath))
                             {
                                 items.Add(new ZipStorageFolder(itemPath, ContainerPath, entry)
                                 {
                                     ZipEncoding = ZipEncoding
                                 });
                             }
                         }
                         else
                         {
                             items.Add(new ZipStorageFile(winPath, ContainerPath, entry));
                         }
                     }
                 }
             }
             return items;
         }
     }));
 }
Esempio n. 11
0
 private IAsyncOperation <ZipFile> OpenZipFileAsync(FileAccessMode accessMode)
 {
     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 = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, readWrite);
             if (hFile.IsInvalid)
             {
                 return null;
             }
             return new ZipFile((Stream) new FileStream(hFile, readWrite ? FileAccess.ReadWrite : FileAccess.Read));
         }
     }));
 }
Esempio n. 12
0
 private static bool CheckAccess(string path)
 {
     try
     {
         var hFile = NativeFileOperationsHelper.OpenFileForRead(path);
         if (hFile.IsInvalid)
         {
             return(false);
         }
         using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)))
         {
             zipFile.IsStreamOwner = true;
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 13
0
        private BaseBasicProperties GetBasicProperties()
        {
            var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath);

            if (hFile.IsInvalid)
            {
                return(new BaseBasicProperties());
            }
            using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)))
            {
                zipFile.IsStreamOwner = true;
                var znt   = new ZipNameTransform(ContainerPath);
                var entry = zipFile.GetEntry(znt.TransformFile(Path));
                if (entry != null)
                {
                    return(new ZipFileBasicProperties(entry));
                }
                return(new BaseBasicProperties());
            }
        }
Esempio n. 14
0
        public override IAsyncOperation <IRandomAccessStream> OpenAsync(FileAccessMode accessMode)
        {
            return(AsyncInfo.Run <IRandomAccessStream>(async(cancellationToken) =>
            {
                bool rw = accessMode == FileAccessMode.ReadWrite;
                var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, rw);
                if (hFile.IsInvalid)
                {
                    return null;
                }
                if (Path == ContainerPath)
                {
                    return new FileStream(hFile, FileAccess.Read).AsRandomAccessStream();
                }

                ZipFile zipFile = new ZipFile(new FileStream(hFile, rw ? FileAccess.ReadWrite : FileAccess.Read));
                zipFile.IsStreamOwner = true;
                var znt = new ZipNameTransform(ContainerPath);
                var entry = zipFile.GetEntry(znt.TransformFile(Path));
                if (!rw)
                {
                    if (entry != null)
                    {
                        return new NonSeekableRandomAccessStream(zipFile.GetInputStream(entry), (ulong)entry.Size)
                        {
                            DisposeCallback = () => zipFile.Close()
                        };
                    }
                }
                else
                {
                    return new RandomAccessStreamWithFlushCallback()
                    {
                        DisposeCallback = () => zipFile.Close(),
                        FlushCallback = WriteZipEntry(zipFile)
                    };
                }
                return null;
            }));
        }
Esempio n. 15
0
        public override IAsyncOperation <BaseStorageFolder> CreateFolderAsync(string desiredName, CreationCollisionOption options)
        {
            return(AsyncInfo.Run <BaseStorageFolder>(async(cancellationToken) =>
            {
                var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, true);
                if (hFile.IsInvalid)
                {
                    return null;
                }
                using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.ReadWrite)))
                {
                    zipFile.IsStreamOwner = true;

                    var znt = new ZipNameTransform(ContainerPath);
                    var zipDesiredName = znt.TransformDirectory(System.IO.Path.Combine(Path, desiredName));
                    var entry = zipFile.GetEntry(zipDesiredName);

                    zipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct));
                    if (entry != null)
                    {
                        if (options != CreationCollisionOption.ReplaceExisting)
                        {
                            zipFile.AbortUpdate();
                            return null;
                        }
                        zipFile.Delete(entry);
                    }
                    zipFile.AddDirectory(zipDesiredName);
                    zipFile.CommitUpdate();

                    var wnt = new WindowsNameTransform(ContainerPath);
                    return new ZipStorageFolder(wnt.TransformFile(zipDesiredName), ContainerPath)
                    {
                        ZipEncoding = ZipEncoding
                    };
                }
            }));
        }
Esempio n. 16
0
 private static bool CheckAccess(string path)
 {
     using var hFile = NativeFileOperationsHelper.OpenFileForRead(path);
     return(!hFile.IsInvalid);
 }
Esempio n. 17
0
        public override IAsyncOperation <IRandomAccessStream> OpenAsync(FileAccessMode accessMode)
        {
            return(AsyncInfo.Run <IRandomAccessStream>(async(cancellationToken) =>
            {
                bool rw = accessMode == FileAccessMode.ReadWrite;
                if (Path == ContainerPath)
                {
                    if (BackingFile != null)
                    {
                        return await BackingFile.OpenAsync(accessMode);
                    }
                    else
                    {
                        var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, rw);
                        if (hFile.IsInvalid)
                        {
                            return null;
                        }
                        return new FileStream(hFile, rw ? FileAccess.ReadWrite : FileAccess.Read).AsRandomAccessStream();
                    }
                }

                if (!rw)
                {
                    ZipFile zipFile = await OpenZipFileAsync(accessMode);
                    if (zipFile == null)
                    {
                        return null;
                    }
                    zipFile.IsStreamOwner = true;
                    var znt = new ZipNameTransform(ContainerPath);
                    var entry = zipFile.GetEntry(znt.TransformFile(Path));
                    if (entry != null)
                    {
                        return new NonSeekableRandomAccessStreamForRead(zipFile.GetInputStream(entry), (ulong)entry.Size)
                        {
                            DisposeCallback = () => zipFile.Close()
                        };
                    }
                }
                else
                {
                    var znt = new ZipNameTransform(ContainerPath);
                    var zipDesiredName = znt.TransformFile(Path);

                    using (ZipFile zipFile = await OpenZipFileAsync(accessMode))
                    {
                        var entry = zipFile.GetEntry(zipDesiredName);
                        if (entry != null)
                        {
                            zipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct));
                            zipFile.Delete(entry);
                            zipFile.CommitUpdate();
                        }
                    }

                    if (BackingFile != null)
                    {
                        var zos = new ZipOutputStream((await BackingFile.OpenAsync(FileAccessMode.ReadWrite)).AsStream(), true);
                        await zos.PutNextEntryAsync(new ZipEntry(zipDesiredName));
                        return new NonSeekableRandomAccessStreamForWrite(zos);
                    }
                    else
                    {
                        var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, true);
                        if (hFile.IsInvalid)
                        {
                            return null;
                        }
                        var zos = new ZipOutputStream(new FileStream(hFile, FileAccess.ReadWrite), true);
                        await zos.PutNextEntryAsync(new ZipEntry(zipDesiredName));
                        return new NonSeekableRandomAccessStreamForWrite(zos);
                    }
                }
                return null;
            }));
        }