예제 #1
0
        /// <summary>
        /// 获取压缩过的文件
        /// </summary>
        /// <param name="filePaths">文件路径数组</param>
        /// <param name="isDeleteFiles">是否删除源文件</param>
        /// <param name="password">压缩密码</param>
        public static async Task <byte[]> GetFileOfZipAsync(string[] filePaths, bool isDeleteFiles = false, string password = null)
        {
            try
            {
                if (filePaths?.Length > 0)
                {
                    using var stream    = new MemoryStream();
                    using var zipStream = new ZipOutputStream(stream);

                    if (password.IsNotNullOrEmpty())
                    {
                        zipStream.Password = password;
                    }

                    zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
                    foreach (string file in filePaths)
                    {
                        using var fs = File.OpenRead(file);
                        var entry = new ZipEntry(Path.GetFileName(file))
                        {
                            Size = fs.Length, IsUnicodeText = true
                        };
                        //Setting the Size provides WinXP built-in extractor compatibility,
                        //but if not available, you can set zipOutputStream.UseZip64 = UseZip64.Off instead.
                        zipStream.PutNextEntry(entry);
                        var buffer = new byte[4096];
                        var count  = 0;
                        while ((count = await fs.ReadAsync(buffer, 0, buffer.Length)) != 0)
                        {
                            await zipStream.WriteAsync(buffer, 0, count);

                            await zipStream.FlushAsync();
                        }
                    }
                    zipStream.Finish();
                    return(stream.ToArray());
                }
                return(null);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //删除文件
                if (isDeleteFiles)
                {
                    foreach (var file in filePaths)
                    {
                        if (File.Exists(file))
                        {
                            File.Delete(file);
                        }
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 打包下载文件,返回压缩包地址
        /// </summary>
        /// <param name="filePaths">要下载的文件路径</param>
        /// <param name="accountKey">Azure连接字符串</param>
        /// <param name="path">压缩包的存储路径</param>
        /// <returns></returns>
        public static async Task <string> DownloadZIPFilesAsync(List <string> filePaths, string accountKey, string path)
        {
            try
            {
                path = BlobHelper.GetPath(path);
                var containerName = path;
                var blobName      = DateTime.Now.ToString("yyyyMMdd");
                if (path.Contains("/"))
                {
                    containerName = path.Split('/')[0];
                    blobName      = path.Substring(path.IndexOf('/') + 1) + "/" + blobName;
                }
                ///Azure连接串
                var container = await BlobHelper.GetBlobContainer(accountKey.GetConnectionString(), containerName);

                ///打包文件地址
                var zipPath = $"{blobName}/ZipFile/{Guid.NewGuid().ToString("N")}.zip";
                ///打包文件的Azure块
                var zipBlockBlob = container.GetBlockBlobReference(zipPath);

                using (var blobStream = await zipBlockBlob.OpenWriteAsync())
                {   ///读取压缩包的流
                    using (ZipOutputStream zipStream = new ZipOutputStream(blobStream))
                    {
                        ///循环要下载的文件地址
                        foreach (var file in filePaths)
                        {
                            ///获取排除https头的文件地址
                            var filePath = file.Substring(file.IndexOf(ConfigSetting.HttpHeader) + ConfigSetting.HttpHeader.Length + 1);
                            ///获取这个文件在Azure中的位置
                            var fileBlobName = filePath.Substring(filePath.IndexOf('/') + 1);
                            ///读取文件名(带后缀)
                            var fileName = file.Substring(file.LastIndexOf('/') + 1);
                            ///获取Azure中的文件
                            var fileBlockBlob = container.GetBlockBlobReference(fileBlobName);
                            using (var fileStream = await fileBlockBlob.OpenReadAsync())
                            {
                                ///文件流添加到压缩包流中
                                zipStream.PutNextEntry(new ZipEntry(fileName));
                                var buffer = new byte[fileStream.Length];
                                await fileStream.ReadAsync(buffer, 0, buffer.Length);

                                zipStream.Write(buffer, 0, buffer.Length);
                            }
                        }
                        //文件流写入
                        await zipStream.FlushAsync();
                    }
                }
                ///返回压缩包地址
                return(zipBlockBlob?.Uri.ToString());
            }
            catch (Exception ex)
            {
                return("打包下载失败," + ex.Message + ex.StackTrace);
            }
        }
예제 #3
0
        public async Task <IImmutableList <PackageResource> > GetPackageResources(
            Components.ComponentTree components,
            string version)
        {
            var tempDir = new DirectoryInfo(Path.Combine(_path.FullName, ".tmp"));

            if (tempDir.Exists)
            {
                tempDir.Delete(true);
            }

            tempDir.Create();

            var distFolder = new DirectoryInfo(Path.Combine(_sourcePath.FullName, "dist"));

            async Task AddFilesFrom(DirectoryInfo directory, ZipOutputStream zipStream)
            {
                if (directory.FullName == tempDir.FullName)
                {
                    return;
                }

                foreach (var file in directory.GetFiles())
                {
                    zipStream.PutNextEntry(new ZipEntry(_settings.GetRelativePath(file.FullName, distFolder.FullName)));

                    await zipStream.WriteAsync(await File.ReadAllBytesAsync(file.FullName));

                    zipStream.CloseEntry();
                }

                foreach (var childDirectory in directory.GetDirectories())
                {
                    await AddFilesFrom(childDirectory, zipStream);
                }
            }

            var appFilePath = Path.Combine(tempDir.FullName, "amplify-app.zip");

            await using var zipFile   = File.Create(appFilePath);
            await using var outStream = new ZipOutputStream(zipFile);

            await AddFilesFrom(distFolder, outStream);

            await outStream.FlushAsync();

            outStream.Close();

            var result = ImmutableList.Create(new PackageResource(
                                                  _settings.GetRelativePath(Path.Combine(_path.FullName, "amplify-app.zip"), components.Path.FullName),
                                                  await File.ReadAllBytesAsync(appFilePath)));

            tempDir.Delete(true);

            return(result);
        }
예제 #4
0
        /// <summary>
        /// 执行ZIP文件创建功能
        /// </summary>
        /// <param name="Source">待压缩文件</param>
        /// <param name="NewZipPath">生成的Zip文件名</param>
        /// <param name="ZipLevel">压缩等级</param>
        /// <param name="ProgressHandler">进度通知</param>
        public static async Task CreateZipAsync(FileSystemStorageItemBase Source, string NewZipPath, int ZipLevel, ProgressChangedEventHandler ProgressHandler = null)
        {
            if (await FileSystemStorageItemBase.CreateAsync(NewZipPath, StorageItemTypes.File, CreateOption.GenerateUniqueName).ConfigureAwait(false) is FileSystemStorageFile NewFile)
            {
                ZipStrings.CodePage = EncodingSetting.CodePage;

                using (FileStream NewFileStream = await NewFile.GetFileStreamFromFileAsync(AccessMode.Exclusive).ConfigureAwait(false))
                    using (ZipOutputStream OutputStream = new ZipOutputStream(NewFileStream))
                    {
                        OutputStream.SetLevel(ZipLevel);
                        OutputStream.UseZip64      = UseZip64.Dynamic;
                        OutputStream.IsStreamOwner = false;

                        switch (Source)
                        {
                        case FileSystemStorageFile File:
                        {
                            using (FileStream FileStream = await File.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false))
                            {
                                ZipEntry NewEntry = new ZipEntry(File.Name)
                                {
                                    DateTime          = DateTime.Now,
                                    CompressionMethod = CompressionMethod.Deflated,
                                    Size = FileStream.Length
                                };

                                OutputStream.PutNextEntry(NewEntry);

                                await FileStream.CopyToAsync(OutputStream, ProgressHandler).ConfigureAwait(false);
                            }

                            break;
                        }

                        case FileSystemStorageFolder Folder:
                        {
                            await ZipFolderCore(Folder, OutputStream, Folder.Name, ProgressHandler).ConfigureAwait(false);

                            break;
                        }
                        }

                        await OutputStream.FlushAsync().ConfigureAwait(false);
                    }
            }
            else
            {
                throw new UnauthorizedAccessException();
            }
        }
        private async void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == 0)
            {
                //create database backup

                SaveFileDialog sfd = new SaveFileDialog();

                sfd.Filter           = "DB Backup Files (*.db2bak)|*.db2bak";
                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                sfd.FileName         = DateTime.Now.ToString("yyyyMMddHHmmssfff");

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    ProgressDialog pgd = new ProgressDialog("Exporting Database");
                    pgd.Show(this);

                    List <string> table_names = new List <string>();

                    using (MySqlConnection sqlconn = new MySqlConnection(MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties.ConnectionString))
                    {
                        pgd.LabelText = "Opening Database Connection";

                        await sqlconn.OpenAsync();

                        pgd.Step();
                        pgd.Reset();
                        pgd.LabelText = "Retrieving Table List";

                        using (MySqlCommand cmdName = new MySqlCommand("show tables", sqlconn))
                            using (MySqlDataReader reader = cmdName.ExecuteReader())
                                while (await reader.ReadAsync())
                                {
                                    table_names.Add(reader.GetString(0));
                                }

                        pgd.Step();
                    }

                    MemoryStream[] output_streams = new MemoryStream[table_names.Count()];
                    for (int i = 0; i < table_names.Count(); i++)
                    {
                        output_streams[i] = new MemoryStream();
                    }

                    var parallelOptions = new ParallelOptions
                    {
                        MaxDegreeOfParallelism = 8
                    };

                    pgd.Reset();
                    pgd.LabelText = "Converting Tables";
                    pgd.Maximum   = table_names.Count();

                    //generate csv files for all tables in parallel and write to an in memory stream
                    Parallel.For(0, table_names.Count(), parallelOptions, async i =>
                    {
                        MemoryStream local_stream = output_streams[i];
                        StreamWriter writer       = new StreamWriter(local_stream);

                        string table_name = table_names[i];

                        List <string> column_names = new List <string>();

                        using (MySqlConnection sqlconn = new MySqlConnection(MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties.ConnectionString))
                        {
                            await sqlconn.OpenAsync();

                            using (MySqlCommand cmdName = new MySqlCommand("SHOW COLUMNS FROM `" + table_name + "`;", sqlconn))
                            {
                                //cmdName.Parameters.AddWithValue("@table_name", table_name);

                                using (MySqlDataReader reader = cmdName.ExecuteReader())
                                    while (await reader.ReadAsync())
                                    {
                                        column_names.Add(reader.GetString(0));
                                    }
                            }

                            //add names row
                            foreach (string column_name in column_names)
                            {
                                await writer.WriteAsync(column_name + (column_names.Last() == column_name ? "," : ","));
                            }

                            await writer.WriteLineAsync();

                            using (MySqlCommand cmdName = new MySqlCommand("select * from `" + table_name + "`;", sqlconn))
                            {
                                using (MySqlDataReader reader = cmdName.ExecuteReader())
                                {
                                    MethodInfo genericFunction = reader.GetType().GetMethod("GetFieldValue");

                                    while (await reader.ReadAsync())
                                    {
                                        int field_index = 0;
                                        foreach (string column_name in column_names)
                                        {
                                            Type t = reader.GetFieldType(column_name);

                                            MethodInfo realFunction = genericFunction.MakeGenericMethod(t);
                                            var ret = realFunction.Invoke(reader, new object[] { field_index });

                                            await writer.WriteAsync(ret + ",");

                                            field_index++;
                                        }

                                        await writer.WriteLineAsync();
                                    }
                                }
                            }

                            await writer.FlushAsync();
                        }

                        pgd.SyncStep();
                    });

                    pgd.Reset();
                    pgd.LabelText = "Compressing Tables in Memory";
                    pgd.Maximum   = table_names.Count() - 1;

                    using (MemoryStream zip_stream = new MemoryStream())
                        using (ZipOutputStream zos = new ZipOutputStream(zip_stream))
                        {
                            zos.SetLevel(9);

                            for (int i = 0; i < table_names.Count(); i++)
                            {
                                ZipEntry ze = new ZipEntry(table_names[i] + ".csv");

                                ze.DateTime = DateTime.Now;
                                ze.Size     = output_streams[i].Length;

                                zos.PutNextEntry(ze);

                                output_streams[i].Position = 0;

                                StreamUtils.Copy(output_streams[i], zos, new byte[4096]);

                                zos.CloseEntry();

                                pgd.Step();
                            }

                            await zos.FlushAsync();

                            zos.Finish();

                            pgd.Reset();
                            pgd.LabelText = "Writing Database Backup Archive to Disk";

                            zip_stream.Position = 0;
                            File.WriteAllBytes(sfd.FileName, zip_stream.ToArray());

                            pgd.Step();
                        }

                    pgd.Dispose();
                }
            }
        }
예제 #6
0
        /// <summary>
        /// 执行ZIP文件创建功能
        /// </summary>
        /// <param name="SourceItemGroup">待压缩文件</param>
        /// <param name="NewZipPath">生成的Zip文件名</param>
        /// <param name="ZipLevel">压缩等级</param>
        /// <param name="ProgressHandler">进度通知</param>
        /// <returns>无</returns>
        public static async Task CreateZipAsync(IEnumerable <FileSystemStorageItemBase> SourceItemGroup, string NewZipPath, CompressionLevel Level, CompressionAlgorithm Algorithm, ProgressChangedEventHandler ProgressHandler = null)
        {
            if (Level == CompressionLevel.Undefine)
            {
                throw new ArgumentException("Undefine is not allowed in this function", nameof(Level));
            }

            if (Algorithm == CompressionAlgorithm.GZip)
            {
                throw new ArgumentException("GZip is not allowed in this function", nameof(Algorithm));
            }

            if (await FileSystemStorageItemBase.CreateAsync(NewZipPath, StorageItemTypes.File, CreateOption.GenerateUniqueName).ConfigureAwait(false) is FileSystemStorageFile NewFile)
            {
                ulong TotalSize       = 0;
                ulong CurrentPosition = 0;

                foreach (FileSystemStorageItemBase StorageItem in SourceItemGroup)
                {
                    switch (StorageItem)
                    {
                    case FileSystemStorageFile File:
                    {
                        TotalSize += File.SizeRaw;
                        break;
                    }

                    case FileSystemStorageFolder Folder:
                    {
                        TotalSize += await Folder.GetFolderSizeAsync().ConfigureAwait(false);

                        break;
                    }
                    }
                }

                if (TotalSize > 0)
                {
                    ZipStrings.CodePage = EncodingSetting.CodePage;

                    using (FileStream NewFileStream = await NewFile.GetFileStreamFromFileAsync(AccessMode.Exclusive).ConfigureAwait(false))
                        using (ZipOutputStream OutputStream = new ZipOutputStream(NewFileStream))
                        {
                            OutputStream.SetLevel((int)Level);
                            OutputStream.UseZip64      = UseZip64.Dynamic;
                            OutputStream.IsStreamOwner = false;

                            foreach (FileSystemStorageItemBase StorageItem in SourceItemGroup)
                            {
                                switch (StorageItem)
                                {
                                case FileSystemStorageFile File:
                                {
                                    using (FileStream FileStream = await File.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false))
                                    {
                                        ZipEntry NewEntry = new ZipEntry(File.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) =>
                                            {
                                                ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32((CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * File.SizeRaw)) * 100d / TotalSize), null));
                                            }).ConfigureAwait(false);
                                    }

                                    OutputStream.CloseEntry();

                                    CurrentPosition += File.SizeRaw;
                                    ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32(CurrentPosition * 100d / TotalSize), null));

                                    break;
                                }

                                case FileSystemStorageFolder Folder:
                                {
                                    ulong InnerFolderSize = 0;

                                    await ZipFolderCore(Folder, OutputStream, Folder.Name, Algorithm, (ByteRead) =>
                                        {
                                            InnerFolderSize = ByteRead;
                                            ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32((CurrentPosition + ByteRead) * 100d / TotalSize), null));
                                        }).ConfigureAwait(false);

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

                                    break;
                                }
                                }
                            }

                            await OutputStream.FlushAsync().ConfigureAwait(false);
                        }
                }
            }
            else
            {
                throw new UnauthorizedAccessException();
            }
        }
예제 #7
0
        public static Task Backup(string file_name)
        {
            return(Task.Run(async() =>
            {
                DateTime now_is = DateTime.Now;



                using (MemoryStream zip_stream = new MemoryStream())
                    using (ZipOutputStream zos = new ZipOutputStream(zip_stream))
                    {
                        zos.SetLevel(9);

                        using (MemoryStream ms = new MemoryStream())
                        {
                            using (MySqlConnection sqlconn = new MySqlConnection(MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties.ConnectionString))
                                using (MySqlCommand cmd = new MySqlCommand())
                                    using (MySqlBackup mb = new MySqlBackup(cmd))
                                    {
                                        cmd.Connection = sqlconn;
                                        await sqlconn.OpenAsync();
                                        mb.ExportToMemoryStream(ms);
                                    }

                            ZipEntry ze = new ZipEntry("dbbak.sql");

                            ze.DateTime = now_is;
                            ze.Size = ms.Length;

                            zos.PutNextEntry(ze);

                            ms.Position = 0;

                            StreamUtils.Copy(ms, zos, new byte[4096]);

                            zos.CloseEntry();
                            await zos.FlushAsync();
                        }

                        //

                        using (MemoryStream ms = new MemoryStream())
                        {
                            var backprop = new BackupProperties();
                            backprop.Timestamp = now_is;
                            var corrected_var = MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties;
                            corrected_var.UID = "";
                            corrected_var.Password = "";
                            backprop.Database = corrected_var;

                            var bytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(backprop));
                            ms.Write(bytes, 0, bytes.Length);

                            ZipEntry ze = new ZipEntry("properties.json");

                            ze.DateTime = DateTime.Now;
                            ze.Size = ms.Length;

                            zos.PutNextEntry(ze);

                            ms.Position = 0;

                            StreamUtils.Copy(ms, zos, new byte[4096]);


                            zos.CloseEntry();
                            await zos.FlushAsync();
                        }



                        zos.Finish();

                        File.WriteAllBytes(file_name, zip_stream.ToArray());
                    }
            }));
        }
예제 #8
0
        /// <summary>
        /// 执行ZIP文件创建功能
        /// </summary>
        /// <param name="SourceItemGroup">待压缩文件</param>
        /// <param name="NewZipPath">生成的Zip文件名</param>
        /// <param name="ZipLevel">压缩等级</param>
        /// <param name="ProgressHandler">进度通知</param>
        /// <returns>无</returns>
        public static async Task CreateZipAsync(IEnumerable <FileSystemStorageItemBase> SourceItemGroup, string NewZipPath, int ZipLevel, ProgressChangedEventHandler ProgressHandler = null)
        {
            if (await FileSystemStorageItemBase.CreateAsync(NewZipPath, StorageItemTypes.File, CreateOption.GenerateUniqueName).ConfigureAwait(false) is FileSystemStorageFile NewFile)
            {
                ulong TotalSize       = 0;
                ulong CurrentPosition = 0;

                using (FileStream NewFileStream = await NewFile.GetFileStreamFromFileAsync(AccessMode.Exclusive).ConfigureAwait(false))
                    using (ZipOutputStream OutputStream = new ZipOutputStream(NewFileStream))
                    {
                        OutputStream.SetLevel(ZipLevel);
                        OutputStream.UseZip64      = UseZip64.Dynamic;
                        OutputStream.IsStreamOwner = false;

                        foreach (FileSystemStorageItemBase StorageItem in SourceItemGroup)
                        {
                            switch (StorageItem)
                            {
                            case FileSystemStorageFile File:
                            {
                                TotalSize += File.SizeRaw;
                                break;
                            }

                            case FileSystemStorageFolder Folder:
                            {
                                TotalSize += await Folder.GetFolderSizeAsync().ConfigureAwait(false);

                                break;
                            }
                            }
                        }

                        foreach (FileSystemStorageItemBase StorageItem in SourceItemGroup)
                        {
                            switch (StorageItem)
                            {
                            case FileSystemStorageFile File:
                            {
                                using (FileStream FileStream = await File.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false))
                                {
                                    ZipEntry NewEntry = new ZipEntry(File.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 * File.SizeRaw)) * 100d / TotalSize), null));
                                        }).ConfigureAwait(false);
                                }

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

                                break;
                            }

                            case FileSystemStorageFolder Folder:
                            {
                                ulong InnerFolderSize = await Folder.GetFolderSizeAsync().ConfigureAwait(false);

                                await ZipFolderCore(Folder, OutputStream, Folder.Name, (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;
                            }
                            }
                        }

                        await OutputStream.FlushAsync().ConfigureAwait(false);
                    }
            }
            else
            {
                throw new UnauthorizedAccessException();
            }
        }