コード例 #1
0
        private void ReportImport()
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.Filter = "标签模板|*.lbpt";
            if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            Encoding gbk = Encoding.GetEncoding("gbk");

            ZipStrings.CodePage = gbk.CodePage;
            ZipFile zipFile = new ZipFile(openFileDialog.FileName);
            var     json    = zipFile.ZipFileComment;
            Report  report  = JsonConvert.DeserializeObject <Report>(json);

            if (ReportList.Count(r => r.Id == report.Id) > 0)
            {
                if (System.Windows.Forms.MessageBox.Show("已包含该模板,确定覆盖吗", "询问",
                                                         System.Windows.Forms.MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
            }

            for (int i = 0; i < zipFile.Count; i++)
            {
                var zipEntry = zipFile[i];
                if (zipEntry.IsDirectory)
                {
                    var folderPath = $"{Funs.ReportsFolder}/{zipEntry.Name}";
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    continue;
                }

                var filePath = $"{Funs.ReportsFolder}/{zipEntry.Name}";
                using (FileStream fs = new FileStream(filePath, FileMode.Create))
                {
                    using (Stream input = zipFile.GetInputStream(zipEntry))
                    {
                        byte[] buffer = new byte[10 * 1024];
                        int    length = 0;
                        while ((length = input.Read(buffer, 0, 10 * 1024)) > 0)
                        {
                            fs.Write(buffer, 0, length);
                        }
                    }
                }
            }

            if (ReportList.Count(r => r.Id == report.Id) <= 0)
            {
                ReportAdd(report.Id, report.ReportName);
            }

            SelectedReport = ReportList.First(r => r.Id == report.Id);
            System.Windows.Forms.MessageBox.Show("导入完成", "提示");
        }