Ejemplo n.º 1
0
        public async Task <bool> PackageIDPFFiles(ContentURI uri, string packageFilePathName,
                                                  IDictionary <string, string> args)
        {
            bool      bIsPackaged = false;
            string    errorMsg    = string.Empty;
            PackageIO packIO      = new PackageIO();

            packIO.CheckForPackageFileErrors(packageFilePathName, args,
                                             ref errorMsg);
            if (!string.IsNullOrEmpty(errorMsg))
            {
                return(bIsPackaged);
            }
            //files are found in this directory and its subfolders
            CurrentDirectory = FileStorageIO.GetDirectoryName(packageFilePathName);
            //Directory.SetCurrentDirectory(Path.GetDirectoryName(packageFilePathName));
            //1. copy container.xml file into a meta-inf subfolder
            string sContainerNewPath = await CopyContainerFile(uri, errorMsg);

            //2. copy mimetype.txt into root folder
            string sMimeTypeNewPath = await CopyEPubFile(uri, MIMETYPE_FILE, MIMETYPE_FILE, errorMsg);

            //3. create content.opf file and place in root folder
            //.opf extension is not liked when saving file so use temp.xml
            string sTempFileName = string.Concat(Helpers.GeneralHelpers.GetRandomInteger(0).ToString(),
                                                 Helpers.GeneralHelpers.EXTENSION_XML);
            string sContentNewPath = await CopyEPubFile(uri, CONTENT_FILE, sTempFileName, errorMsg);

            //copy the files in args into content.opf
            if (errorMsg == string.Empty)
            {
                await MakeContentFile(uri, Path.GetFileNameWithoutExtension(packageFilePathName),
                                      args, sContentNewPath, errorMsg);

                //4. create toc.ncx file and place in root
                //.ncx extension is not liked when saving file so use temp.xml
                sTempFileName = string.Concat(Helpers.GeneralHelpers.GetRandomInteger(12223).ToString(),
                                              Helpers.GeneralHelpers.EXTENSION_XML);
                string sNavigationNewPath = await CopyEPubFile(uri, NCX_FILE, sTempFileName, errorMsg);

                //copy whatever files from args that are needed into the ncx file
                await MakeNavigationFile(uri, packageFilePathName,
                                         args, sNavigationNewPath, errorMsg);

                //copy misc. files
                string sTitlePath = await CopyEPubFile(uri, TITLEPAGE_FILE, TITLEPAGE_FILE, errorMsg);

                //add the epub files to args for zipping
                args.Add(sContainerNewPath, "epub");
                args.Add(sMimeTypeNewPath, "epub");
                args.Add(sContentNewPath, "epub");
                args.Add(sNavigationNewPath, "epub");
                args.Add(sTitlePath, "epub");
                if (errorMsg == string.Empty)
                {
                    bIsPackaged = true;
                }
            }
            return(bIsPackaged);
        }
Ejemplo n.º 2
0
        public bool ZipFiles(ContentURI uri, string zipFilePath,
                             IDictionary <string, string> args)
        {
            bool      bIsZipped = false;
            string    errorMsg  = string.Empty;
            PackageIO packIO    = new PackageIO();

            packIO.CheckForPackageFileErrors(zipFilePath, args,
                                             ref errorMsg);
            if (!string.IsNullOrEmpty(errorMsg))
            {
                return(bIsZipped);
            }
            //filesystem
            //don't want the temp directory info in the file path, disrupts subfolders (i.e. in packages)
            Directory.SetCurrentDirectory(Path.GetDirectoryName(zipFilePath));
            string sCurrentDirectory = FileStorageIO.GetDirectoryName(zipFilePath);

            //init the zip with the zip's file name
            using (FileStream zipToOpen = new FileStream(Path.GetFileName(zipFilePath), FileMode.Create))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    string[] arrValues    = { };
                    string   sRelPartPath = string.Empty;
                    string   sKey         = string.Empty;
                    string   sValue       = string.Empty;
                    foreach (KeyValuePair <string, string> kvp in args)
                    {
                        sKey   = kvp.Key;
                        sValue = kvp.Value;
                        if (string.IsNullOrEmpty(sKey) == false &&
                            Helpers.FileStorageIO.URIAbsoluteExists(
                                uri, sKey))
                        {
                            //convert the absolute path of partFullPath into a path relative to the package root (getcurrentdirectory)
                            sRelPartPath = AppSettings.ConvertAbsPathToRelPath(sCurrentDirectory, sKey);
                            //will add files to the same directory as setcurrentdirectory
                            //2.0.0 change required adding a reference to System.IO.Compression.FileSystem
                            archive.CreateEntryFromFile(sKey, sRelPartPath);
                        }
                    }
                    bIsZipped = true;
                }
            }
            return(bIsZipped);
        }