Skip to content

Instantly share code, notes, and snippets.

@joshi-kumar
Created February 17, 2018 13:36
Show Gist options
  • Save joshi-kumar/0fff76bb7f40cc56fa0a156671d27f96 to your computer and use it in GitHub Desktop.
Save joshi-kumar/0fff76bb7f40cc56fa0a156671d27f96 to your computer and use it in GitHub Desktop.
protected virtual string GetEpubPath()
{
return Path.Combine(_webHelper.MapPath("~/content/epubs/"));
}
public string CreateSampleEpub()
{
string[] files = Directory.GetFiles(GetEpubPath());//GetAllFiles
int k=0;
for (k = 0; k < files.Count(); k++)
{
string path = Path.GetDirectoryName(files[k]);
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(files[k]);
string newFile = Path.ChangeExtension(files[k], ".zip");
string newFileName = Path.GetFileName(newFile);
string newDirecotry = path + "\\SampleConstruction";
Directory.CreateDirectory(newDirecotry);
string newPath = path + "\\SampleConstruction\\" + newFileName;
System.IO.File.Copy(files[k], newPath);
string ExtractFilePath = newDirecotry + "\\" + fileNameWithoutExt;
Directory.CreateDirectory(ExtractFilePath);
System.IO.Compression.ZipFile.ExtractToDirectory(newPath, ExtractFilePath);
System.IO.File.Delete(newPath);
string[] extractZipFile = Directory.GetFiles(ExtractFilePath + "\\OEBPS", "*.opf");//Get opf Files
FileInfo file = new FileInfo(extractZipFile[0]);//Select File
string opsToXml = Path.ChangeExtension(extractZipFile[0], ".xml");
System.IO.File.Move(extractZipFile[0], opsToXml);
XmlDocument doc = new XmlDocument();
doc.Load(opsToXml);
var SpineRemovableIds = new List<string>();
var spineChilds = doc.GetElementsByTagName("spine")[0].ChildNodes;
int _spineChildsCount = ((spineChilds.Count * 40) / 100);
for (int i = 0; i < spineChilds.Count; i++)
{
string spineChildItem = spineChilds[i].Attributes["idref"].Value;
SpineRemovableIds.Add(spineChildItem);
}
SpineRemovableIds = (from s in SpineRemovableIds
select s).Skip(_spineChildsCount).ToList();
List<XmlNode> toRemoveSpine = new List<XmlNode>();
List<XmlNode> toRemoveManifest = new List<XmlNode>();
foreach (XmlNode xmlElement in spineChilds)
{
string spineChildItem = xmlElement.Attributes["idref"].Value;
if (SpineRemovableIds.Contains(spineChildItem))
{
toRemoveSpine.Add(xmlElement);
}
}
foreach (XmlNode xmlElement in toRemoveSpine)
{
XmlNode node = xmlElement.ParentNode;
node.RemoveChild(xmlElement);
}
var manifestChilds = doc.GetElementsByTagName("manifest")[0].ChildNodes;
foreach (XmlNode xmlElement in manifestChilds)
{
string manifestChildItem = xmlElement.Attributes["id"].Value;
if (SpineRemovableIds.Contains(manifestChildItem))
{
toRemoveManifest.Add(xmlElement);
}
}
foreach (XmlNode xmlElement in toRemoveManifest)
{
XmlNode node = xmlElement.ParentNode;
node.RemoveChild(xmlElement);
}
doc.Save(opsToXml);
string[] extractXMLFile = Directory.GetFiles(ExtractFilePath + "\\OEBPS", "*.xml");//Get xml Files
string xmlToOpc = Path.ChangeExtension(extractXMLFile[0], ".opf");
System.IO.File.Move(extractXMLFile[0], xmlToOpc);
string[] extractHTMLFile = Directory.GetFiles(ExtractFilePath + "\\OEBPS", "*.html");//Get html Files
foreach (var htmlfile in extractHTMLFile)
{
FileInfo fileInfo = new FileInfo(htmlfile);//Select File
if (SpineRemovableIds.Contains(fileInfo.Name.Replace(".html", "")))
System.IO.File.Delete(htmlfile);
}
System.IO.Compression.ZipFile.CreateFromDirectory(ExtractFilePath, newPath);
string epubCreate = Path.ChangeExtension(newPath,".epub");
string fileName = Path.GetFileNameWithoutExtension(epubCreate);
epubCreate = epubCreate.Replace(fileName, fileName + "_s");
System.IO.File.Move(newPath, epubCreate);
string fileNameEpub = Path.GetFileName(epubCreate);
string newFilePath = path + "\\" + fileNameEpub;
System.IO.File.Copy(epubCreate, newFilePath);
var dir = new DirectoryInfo(newDirecotry);
dir.Delete(true); // true => recursive delete
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment