Exemplo n.º 1
0
        public ModuleInfo ReadModuleInfo(string moduleFilePath)
        {
            string destFolder = Path.Combine(GetTempFolderPath(), Path.GetFileNameWithoutExtension(moduleFilePath));

            try
            {
                if (Directory.Exists(destFolder))
                {
                    Directory.Delete(destFolder, true);
                }

                Directory.CreateDirectory(destFolder);

                ZipLibHelper.ExtractZipFile(File.ReadAllBytes(moduleFilePath), destFolder, new string[] { SystemConstants.ManifestFileName });

                string manifestFilePath = Path.Combine(destFolder, SystemConstants.ManifestFileName);
                if (!File.Exists(manifestFilePath))
                {
                    throw new InvalidModuleException($"File '{manifestFilePath}' not found.");
                }

                return(Dessirialize <ModuleInfo>(manifestFilePath));
            }
            finally
            {
                ThreadPool.QueueUserWorkItem(DeleteDirectory, destFolder);
            }
        }
Exemplo n.º 2
0
        public ModuleInfo UploadModule(string originalFilePath, string moduleName)
        {
            if (Path.GetExtension(originalFilePath).ToLower() != SystemConstants.ModuleFileExtension)
            {
                throw new InvalidModuleException($"Please select a file with extension '{SystemConstants.ModuleFileExtension}'.");
            }

            var destFilePath = Path.Combine(GetModulesPackagesDirectory(), moduleName + SystemConstants.ModuleFileExtension);

            destFilePath = CopyModulePackage(originalFilePath, destFilePath);

            string destFolder = GetModuleDirectory(moduleName);

            if (Directory.Exists(destFolder))
            {
                Directory.Delete(destFolder, true);
            }

            Directory.CreateDirectory(destFolder);

            try
            {
                ZipLibHelper.ExtractZipFile(File.ReadAllBytes(destFilePath), destFolder);
                var module = GetModuleInfo(moduleName);
                CheckModule(module);

                if (module.Type == Scheme.Module.ModuleType.Bible || module.Type == Scheme.Module.ModuleType.Strong)
                {
                    var baseModule = GetModuleInfo(_configurationManager.ModuleShortName);

                    if (_bibleParallelTranslationManager.MergeModuleWithMainBible(baseModule, module))
                    {
                        UpdateModuleManifest(baseModule);
                    }
                    else
                    {
                        _bibleParallelTranslationManager.MergeAllModulesWithMainBible(baseModule,
                                                                                      GetModules(true).Where(m => m.ShortName != _configurationManager.ModuleShortName));
                    }
                }

                return(module);
            }
            catch (Exception ex)
            {
                throw new InvalidModuleException(ex.Message);
            }
        }
Exemplo n.º 3
0
        public static void ExtractZipFile(byte[] fileData, string directoryPath, string[] relativeFilePathsToExtract = null)
        {
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            ZipStrings.CodePage = 65001;    // 866

            using (MemoryStream ms = new MemoryStream(fileData))
            {
                using (ZipInputStream s = new ZipInputStream(ms))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string relativeDirectoryName = Path.GetDirectoryName(theEntry.Name);
                        string directoryName         = Path.Combine(directoryPath, relativeDirectoryName);
                        string relativeFileName      = Path.GetFileName(theEntry.Name);
                        string fileName = Path.Combine(directoryName, relativeFileName);


                        // create directory
                        if (relativeDirectoryName.Length > 0)
                        {
                            Directory.CreateDirectory(directoryName);
                        }

                        if (relativeFileName != String.Empty)
                        {
                            if (relativeFilePathsToExtract == null || relativeFilePathsToExtract.Contains(relativeFileName))
                            {
                                using (FileStream streamWriter = File.Create(fileName))
                                {
                                    ZipLibHelper.CopyZipEntryToStream(s, streamWriter);
                                }
                            }
                        }
                    }
                }
            }
        }