14 Jan 2010

Class epub

This class models an epub ebook. The approach is taken that the Zip handler will be used only to open and save a .epub file. All other operations will use the files in the folder to which the Zip file is extracted.

epub Members
Class epub exposes the following members:

Property
Description
_container
_container is a private instance of class container. It gives access to the rest of the epub documents in the ebook, mainly through its package instance.
_filePath
_filePath is a string holding the location in the file system where the .epub for this instance of epub is located.

epub Constructor
The class has a constructor with the signature:
public epub(string ebookPath, string fileSystemPath)
This allows the caller to create an epub instance which will locate and open the ebook at location ebookPath. The ebook is extracted from the .epub and written to the file system at location fileSystemPath.

epub Methods
Class epub exposes the following methods:

Method
Description
Create
Creates a new epub ebook with the given title at the given location. Uses the given working folder path to store the files that will be zipped into the .epub.
Open
Open an existing .epub file and expand it into the given working folder.
Save
Save the epub instance as a .epub file at the given location.
SaveEntry
Save the given text of a content document.

epub Properties
Class epub exposes the following properties:

Property
Description
container
Get or set the container instance belonging to the current instance of epub. This property is widely used to gain access to most of the ebook's package and NCX data.

Code of the epub class (14Jan10 14:20)
The code of the epub class is shown below.

#region using
using System;
using System.IO;
#endregion

namespace net.hazelhurst.epub
{
public class epub
{
#region Members
private container _container;
private string _filePath;
#endregion

#region Constructors
public epub(string eBookPath, string fileSystemPath)
{
Open(eBookPath, fileSystemPath);
}
#endregion

#region Methods
public static epub Create(string title, string booksPath, out string workPath)
{
epub result = null;
string emptyBookPath;

workPath = booksPath + title + "\\";
if (!Directory.Exists(workPath) && !File.Exists(booksPath + title + ".epub"))
{
string newbook = booksPath + title + ".epub";
emptyBookPath = booksPath + "empty\\";
File.Copy(emptyBookPath + "empty.zip", newbook);
result = new epub(newbook, workPath);
result.container.package.Title = title;
}
else
{
throw new Exception("A book with that title already exists");
}
return result;
}
public bool Open(string eBookPath, string fileSystemPath)
{
bool bookOpen = false;

_filePath = eBookPath;

// get the book's container
_container = new container(eBookPath, fileSystemPath);

// if the container is available
if (_container != null)
{
bookOpen = true;
}

return bookOpen;
}
public bool Save()
{
bool result = false;
_container.Save();
//ZipFileHandler.Save(Title, filePath, outputPath);
return result;
}
public bool SaveEntry(string fileName, string content)
{
bool result = false;

try
{
_container.SaveEntry(fileName, content);
}
catch (Exception ex)
{
throw ex;
}
return result;
}
#endregion

#region Properties
public container container
{
get
{
return _container;
}
set
{
_container = value;
}
}
public string Title
{
get { return _container.package.Title; }
set { _container.package.Title = value; }
}
#endregion

} //epub
}