Esempio n. 1
0
        private static async Task ZipFolderCore(FileSystemStorageFolder Folder, ZipOutputStream OutputStream, string BaseFolderName, CompressionAlgorithm Algorithm, ByteReadChangedEventHandler ByteReadHandler = null)
        {
            IReadOnlyList <FileSystemStorageItemBase> ItemList = await Folder.GetChildItemsAsync(true, true).ConfigureAwait(false);

            if (ItemList.Count == 0)
            {
                if (!string.IsNullOrEmpty(BaseFolderName))
                {
                    ZipEntry NewEntry = new ZipEntry($"{BaseFolderName}/")
                    {
                        DateTime = DateTime.Now
                    };

                    OutputStream.PutNextEntry(NewEntry);
                    OutputStream.CloseEntry();
                }
            }
            else
            {
                ulong CurrentPosition = 0;

                foreach (FileSystemStorageItemBase Item in ItemList)
                {
                    switch (Item)
                    {
                    case FileSystemStorageFolder InnerFolder:
                    {
                        ulong InnerFolderSize = 0;

                        await ZipFolderCore(InnerFolder, OutputStream, $"{BaseFolderName}/{Item.Name}", Algorithm, (ByteRead) =>
                            {
                                InnerFolderSize = ByteRead;
                                ByteReadHandler?.Invoke(CurrentPosition + ByteRead);
                            }).ConfigureAwait(false);

                        ByteReadHandler?.Invoke(CurrentPosition += InnerFolderSize);

                        break;
                    }

                    case FileSystemStorageFile InnerFile:
                    {
                        using (FileStream FileStream = await InnerFile.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false))
                        {
                            ZipEntry NewEntry = new ZipEntry($"{BaseFolderName}/{Item.Name}")
                            {
                                DateTime          = DateTime.Now,
                                CompressionMethod = Algorithm == CompressionAlgorithm.None ? CompressionMethod.Stored : Enum.Parse <CompressionMethod>(Enum.GetName(typeof(CompressionAlgorithm), Algorithm)),
                                Size = FileStream.Length
                            };

                            OutputStream.PutNextEntry(NewEntry);

                            await FileStream.CopyToAsync(OutputStream, ProgressHandler : (s, e) =>
                                {
                                    ByteReadHandler?.Invoke(CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFile.SizeRaw));
                                }).ConfigureAwait(false);
                        }

                        OutputStream.CloseEntry();

                        ByteReadHandler?.Invoke(CurrentPosition += Item.SizeRaw);

                        break;
                    }
                    }
                }
            }
        }
Esempio n. 2
0
        private static async Task TarFolderCore(FileSystemStorageFolder Folder, TarOutputStream OutputStream, string BaseFolderName, ByteReadChangedEventHandler ByteReadHandler = null)
        {
            IReadOnlyList <FileSystemStorageItemBase> ItemList = await Folder.GetChildItemsAsync(true, true).ConfigureAwait(false);

            if (ItemList.Count == 0)
            {
                if (!string.IsNullOrEmpty(BaseFolderName))
                {
                    TarEntry NewEntry = TarEntry.CreateTarEntry($"{BaseFolderName}/");
                    OutputStream.PutNextEntry(NewEntry);
                    OutputStream.CloseEntry();
                }
            }
            else
            {
                ulong CurrentPosition = 0;

                foreach (FileSystemStorageItemBase Item in ItemList)
                {
                    switch (Item)
                    {
                    case FileSystemStorageFolder InnerFolder:
                    {
                        ulong InnerFolderSize = 0;

                        await TarFolderCore(InnerFolder, OutputStream, $"{BaseFolderName}/{InnerFolder.Name}", ByteReadHandler : (ByteRead) =>
                            {
                                InnerFolderSize = ByteRead;
                                ByteReadHandler?.Invoke(CurrentPosition + ByteRead);
                            }).ConfigureAwait(false);

                        ByteReadHandler?.Invoke(CurrentPosition += InnerFolderSize);

                        break;
                    }

                    case FileSystemStorageFile InnerFile:
                    {
                        using (FileStream FileStream = await InnerFile.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false))
                        {
                            TarEntry NewEntry = TarEntry.CreateTarEntry($"{BaseFolderName}/{InnerFile.Name}");
                            NewEntry.ModTime = DateTime.Now;
                            NewEntry.Size    = FileStream.Length;

                            OutputStream.PutNextEntry(NewEntry);

                            await FileStream.CopyToAsync(OutputStream, ProgressHandler : (s, e) =>
                                {
                                    ByteReadHandler?.Invoke(CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFile.SizeRaw));
                                }).ConfigureAwait(false);
                        }

                        OutputStream.CloseEntry();

                        ByteReadHandler?.Invoke(CurrentPosition += InnerFile.SizeRaw);

                        break;
                    }
                    }
                }
            }
        }
Esempio n. 3
0
        private static async Task ZipFolderCore(FileSystemStorageFolder Folder, ZipOutputStream OutputStream, string BaseFolderName, ProgressChangedEventHandler ProgressHandler = null)
        {
            List <FileSystemStorageItemBase> ItemList = await Folder.GetChildItemsAsync(true).ConfigureAwait(false);

            if (ItemList.Count == 0)
            {
                if (!string.IsNullOrEmpty(BaseFolderName))
                {
                    ZipEntry NewEntry = new ZipEntry($"{BaseFolderName}/");
                    OutputStream.PutNextEntry(NewEntry);
                    OutputStream.CloseEntry();
                }
            }
            else
            {
                ulong TotalSize = await Folder.GetFolderSizeAsync().ConfigureAwait(false);

                ulong CurrentPosition = 0;

                foreach (FileSystemStorageItemBase Item in ItemList)
                {
                    switch (Item)
                    {
                    case FileSystemStorageFolder InnerFolder:
                    {
                        ulong InnerFolderSize = await InnerFolder.GetFolderSizeAsync().ConfigureAwait(false);

                        await ZipFolderCore(InnerFolder, OutputStream, $"{BaseFolderName}/{Item.Name}", ProgressHandler : (s, e) =>
                            {
                                if (TotalSize > 0)
                                {
                                    ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32((CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFolderSize)) * 100d / TotalSize), null));
                                }
                            }).ConfigureAwait(false);

                        if (TotalSize > 0)
                        {
                            CurrentPosition += InnerFolderSize;
                            ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32(CurrentPosition * 100d / TotalSize), null));
                        }

                        break;
                    }

                    case FileSystemStorageFile InnerFile:
                    {
                        using (FileStream FileStream = await InnerFile.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false))
                        {
                            ZipEntry NewEntry = new ZipEntry($"{BaseFolderName}/{Item.Name}")
                            {
                                DateTime          = DateTime.Now,
                                CompressionMethod = CompressionMethod.Deflated,
                                Size = FileStream.Length
                            };

                            OutputStream.PutNextEntry(NewEntry);

                            await FileStream.CopyToAsync(OutputStream, (s, e) =>
                                {
                                    ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32((CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFile.SizeRaw)) * 100d / TotalSize), null));
                                }).ConfigureAwait(false);
                        }

                        if (TotalSize > 0)
                        {
                            CurrentPosition += Item.SizeRaw;
                            ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32(CurrentPosition * 100d / TotalSize), null));
                        }

                        break;
                    }
                    }
                }
            }
        }