Exemplo n.º 1
0
        public Item(EpubFile belongTo, EpubFileEntry entry, string id, string mediaType, string properties)
        {
            this.belongTo   = belongTo;
            this.id         = id;
            this.mediaType  = mediaType;
            this.properties = properties;
            string dir = Path.GetDirectoryName(belongTo.packageFile.fullName);

            href = Path.GetRelativePath(dir, entry.fullName).Replace('\\', '/');
        }
Exemplo n.º 2
0
        public T GetFile <T>(string fullName) where T : EpubFileEntry
        {
            EpubFileEntry r = null;

            foreach (var i in entries)
            {
                if (i.fullName == fullName)
                {
                    r = i;
                }
            }
            if (r == null || r.GetType() != typeof(T))
            {
                return(null);
            }
            return((T)r);
        }
Exemplo n.º 3
0
        public EpubFile(string path)
        {
            this.path = path;
            filename  = Path.GetFileNameWithoutExtension(path);
            entries   = new List <EpubFileEntry>();
            using (FileStream zipToOpen = new FileStream(path, FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
                {
                    foreach (var entry in archive.Entries)
                    {
                        string ext = Path.GetExtension(entry.Name).ToLower();
                        if (entry.FullName == "mimetype")
                        {
                            using (var stm = entry.Open())
                                using (StreamReader r = new StreamReader(stm))
                                {
                                    string s = Util.Trim(r.ReadToEnd());
                                    if (s != "application/epub+zip")
                                    {
                                        throw new EpubErrorException("The mimetype of epub should be 'application/epub+zip'. Current:" + s);
                                    }
                                    var i = new EpubMIMETypeEntry();
                                    entries.Insert(0, i);
                                }
                        }
                        else
                        {
                            switch (ext)
                            {
                            case ".xhtml":
                            case ".html":
                            case ".xml":
                            case ".css":
                            case ".opf":
                            case ".ncx":
                            case ".svg":
                            case ".js":

                                using (var stm = entry.Open())
                                    using (StreamReader r = new StreamReader(stm))
                                    {
                                        string s = r.ReadToEnd();
                                        var    i = new TextEpubFileEntry(entry.FullName, s);
                                        entries.Add(i);
                                    }
                                break;

                            default:
                                using (var stm = entry.Open())

                                {
                                    byte[] d = new byte[entry.Length];
                                    if (entry.Length < int.MaxValue)
                                    {
                                        stm.Read(d, 0, (int)entry.Length);
                                        var i = new EpubFileEntry(entry.FullName, d);
                                        entries.Add(i);
                                    }
                                    else
                                    {
                                        throw new EpubErrorException("File size exceeds the limit.");
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
            if (entries.Count == 0)
            {
                throw new EpubErrorException("Cannot find files in epub");
            }
            if (entries[0].GetType() != typeof(EpubMIMETypeEntry))
            {
                throw new EpubErrorException("Cannot find mimetype in epub");
            }
        }