/// <summary> /// 导出数据。 /// </summary> /// <param name="outputFilePath"></param> /// <param name="handler"></param> public void Export(string outputFilePath, RaiseChangedHandler handler) { lock (this) { if (nodes != null && nodes.Count > 0 && !string.IsNullOrEmpty(outputFilePath)) { this.RaiseChanged(handler, "开始检索数据..."); Stack<NodeValue> stack = null; Dictionary<String, List<String>> dict = new Dictionary<string, List<string>>(); foreach (TreeNode n in nodes) { stack = new Stack<NodeValue>(); this.GetAllNodeValue(n, stack); if (stack.Count > 0) { this.SetOutputData(stack.ToArray(), ref dict); } } ExportImportManifest mainfset = new ExportImportManifest("2.0"); mainfset.TeacherID = this.teacherID; List<String> list_ClassID_CatalogID = new List<string>(); List<LocalStudentWorkStore> listStore = new List<LocalStudentWorkStore>(); if (dict != null && dict.Count > 0) { #region 检索存储文件。 this.RaiseChanged(handler, "开始加载检索文件..."); foreach (string key in dict.Keys) { string[] arr = key.Split('_'); if (arr != null && arr.Length > 1) { LocalStudentWorkStore store = LocalStudentWorkStore.DeSerializer(this.teacherID, arr[1], arr[0]); if (store != null && store.Students != null) { list_ClassID_CatalogID.Add(key); List<String> list = dict[key]; if (list != null && list.Count > 0 && (list.Count != store.Students.Count)) { List<LocalStudent> removeStudents = new List<LocalStudent>(); foreach (LocalStudent ls in store.Students) { if (!list.Contains(ls.StudentID)) { removeStudents.Add(ls); } } if (removeStudents.Count > 0) { store.Students.Remove(removeStudents.ToArray()); } } listStore.Add(store); } } } #endregion } if (listStore.Count > 0) { mainfset.ClassID_CatalogID = list_ClassID_CatalogID.ToArray(); this.RaiseChanged(handler, "创建导出清单文件..."); outputFilePath = outputFilePath.Replace(Path.GetExtension(outputFilePath), ".zip"); using (FileStream outputStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write)) { this.RaiseChanged(handler, "创建压缩文件..."); Crc32 crc = new Crc32(); using (ZipOutputStream zipStream = new ZipOutputStream(outputStream)) { zipStream.SetLevel(9); byte[] data = UtilTools.Serializer<ExportImportManifest>(mainfset); if (data != null) { this.RaiseChanged(handler, "压缩导出清单文件..."); ZipEntry entry = new ZipEntry("ExportImportManifest.xml"); entry.DateTime = DateTime.Now; entry.Size = data.Length; crc.Reset(); crc.Update(data); entry.Crc = crc.Value; zipStream.PutNextEntry(entry); zipStream.Write(data, 0, data.Length); zipStream.CloseEntry(); } this.RaiseChanged(handler, "开始抽取压缩作品数据..."); this.BuildZipFile(zipStream,crc, listStore, handler); this.RaiseChanged(handler, "压缩作品数据完成"); } } this.RaiseChanged(handler, "作品数据导出完成"); } } } }
/// <summary> /// /// </summary> /// <param name="mainfest"></param> /// <param name="zipFile"></param> /// <param name="isImportFile"></param> private void ImportWorkData(ExportImportManifest mainfest, ZipFile zipFile, bool isImportFile, RaiseChangedHandler handler) { if (mainfest != null && mainfest.ClassID_CatalogID != null && mainfest.ClassID_CatalogID.Length > 0 && zipFile != null) { for (int i = 0; i < mainfest.ClassID_CatalogID.Length; i++) { if (!string.IsNullOrEmpty(mainfest.ClassID_CatalogID[i])) { string[] arr = mainfest.ClassID_CatalogID[i].Split('_'); if (arr != null && arr.Length > 1) { this.ImportWorkData(mainfest.ClassID_CatalogID[i], arr[0], arr[1], zipFile, isImportFile, handler); } } } } }
/// <summary> /// 序列化。 /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] Serializer(ExportImportManifest data) { return UtilTools.Serializer<ExportImportManifest>(data); }