Skip to content

Commit

Permalink
更标准一点;全过程内存完成,不再用缓存文件夹
Browse files Browse the repository at this point in the history
比较新的书应该能通过EpubCheck
  • Loading branch information
Aeroblast committed Oct 7, 2020
1 parent 2e5b0cf commit 0c6dad1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 69 deletions.
81 changes: 47 additions & 34 deletions src/EpubBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Xml;
using System.Collections.Generic;
using System.IO.Compression;
namespace UnpackKindleS
{

Expand Down Expand Up @@ -62,48 +63,56 @@ public Epub(Azw3File azw3, Azw6File azw6 = null)
}

}
const string container = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\"> <rootfiles><rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/> </rootfiles></container>";
public void Save(string dir)
{
File.WriteAllText(Path.Combine(dir, "mimetype"), "application/epub+zip");
string container = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\"> <rootfiles><rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/> </rootfiles></container>";
Directory.CreateDirectory(Path.Combine(dir, "META-INF"));
File.WriteAllText(Path.Combine(dir, "META-INF\\container.xml"), container);

string oepbs = Path.Combine(dir, "OEBPS");
Directory.CreateDirectory(oepbs);
string subdir;

subdir = Path.Combine(oepbs, "Text");
Directory.CreateDirectory(subdir);
for (int i = 0; i < xhtml_names.Count; i++)
using (FileStream fs = new FileStream(dir, FileMode.Create))
using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Create))
{
string p = Path.Combine(subdir, xhtml_names[i]);
xhtmls[i].Save(p);
string ss = File.ReadAllText(p);
ss = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html>\n"
+ ss.Substring(ss.IndexOf("<html"));
File.WriteAllText(p, ss);
{
var entry = zip.CreateEntry("mimetype", CompressionLevel.NoCompression);
using (StreamWriter writer = new StreamWriter(entry.Open()))
writer.Write("application/epub+zip");
}
ZipWriteAllText(zip, "META-INF/container.xml", container);
for (int i = 0; i < xhtml_names.Count; i++)
{
string p = "OEBPS/Text/" + xhtml_names[i];
string ss = xhtmls[i].OuterXml;
ss = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html>\n"
+ ss.Substring(ss.IndexOf("<html"));
ZipWriteAllText(zip, p, ss);
}
for (int i = 0; i < css_names.Count; i++)
{
ZipWriteAllText(zip, "OEBPS/Styles/" + css_names[i], csss[i]);
}
for (int i = 0; i < img_names.Count; i++)
{
ZipWriteAllBytes(zip, "OEBPS/Images/" + img_names[i], imgs[i]);
}
ZipWriteAllText(zip, "OEBPS/toc.ncx", ncx);
ZipWriteAllText(zip, "OEBPS/nav.xhtml", nav);
ZipWriteAllText(zip, "OEBPS/content.opf", opf);
}

subdir = Path.Combine(oepbs, "Styles");
Directory.CreateDirectory(subdir);
for (int i = 0; i < css_names.Count; i++)
}
void ZipWriteAllText(ZipArchive zip, string path, string text)
{
var entry = zip.CreateEntry(path);
using (StreamWriter writer = new StreamWriter(entry.Open()))
{
File.WriteAllText(Path.Combine(subdir, css_names[i]), csss[i]);
writer.Write(text);
}

subdir = Path.Combine(oepbs, "Images");
Directory.CreateDirectory(subdir);
for (int i = 0; i < img_names.Count; i++)
}
void ZipWriteAllBytes(ZipArchive zip, string path, byte[] data)
{
var entry = zip.CreateEntry(path);
using (Stream stream = entry.Open())
{
File.WriteAllBytes(Path.Combine(subdir, img_names[i]), imgs[i]);
stream.Write(data, 0, data.Length);
}

File.WriteAllText(Path.Combine(oepbs, "toc.ncx"), ncx);
File.WriteAllText(Path.Combine(oepbs, "nav.xhtml"), nav);
File.WriteAllText(Path.Combine(oepbs, "content.opf"), opf);

}

void ProcNodes(XmlNode node)
{
if (node.Attributes != null)
Expand Down Expand Up @@ -448,10 +457,14 @@ void CreateOPF()
{
XmlElement x = meta.CreateElement("dc:identifier");
x.SetAttribute("id", "ASIN");
x.SetAttribute("opf:scheme", "ASIN");
//x.SetAttribute("opf:scheme", "ASIN");
string z = azw3.mobi_header.extMeta.id_string[504];
x.InnerXml = z;
meta.FirstChild.AppendChild(x);
XmlElement xd = meta.CreateElement("meta");
xd.SetAttribute("property", "dcterms:modified");
xd.InnerText = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ");
meta.FirstChild.AppendChild(xd);
}
if (azw3.mobi_header.extMeta.id_string.ContainsKey(100))
{
Expand Down
15 changes: 2 additions & 13 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Program
static bool append_log = false;
static bool overwrite = false;
static bool rename_when_exist = false;
static string temp_path = "_temp_";
static void Main(string[] args)
{
string temp_environment_dir = Environment.CurrentDirectory;
Expand Down Expand Up @@ -193,9 +192,6 @@ static void ProcPath(string[] args)
string outname = auther + azw3.title + ".epub";
outname = Util.FilenameCheck(outname);
Epub epub = new Epub(azw3, azw6);
if (Directory.Exists(temp_path)) DeleteDir(temp_path);
Directory.CreateDirectory(temp_path);
epub.Save(temp_path);
Log.log(azw3);
string output_path;
if (args.Length >= 2 && Directory.Exists(args[1]))
Expand Down Expand Up @@ -266,8 +262,7 @@ static void ProcPath(string[] args)
}
}
if (output_path != "")
Util.Packup(temp_path, output_path);
DeleteDir(temp_path);
epub.Save(output_path);
Log.log("azw3 source:" + azw3_path);
if (azw6_path != null)
Log.log("azw6 source:" + azw6_path);
Expand All @@ -286,7 +281,7 @@ static void DumpHDImage(string[] args)
if (!File.Exists(args[0])) { Log.log("File was not found:" + args[0]); return; }
Azw6File azw = new Azw6File(args[0]);
if (args.Length >= 3) outputdir = args[1];
else { outputdir = Path.Combine(Path.GetDirectoryName(args[0]),Util.FilenameCheck(azw.header.title)); }
else { outputdir = Path.Combine(Path.GetDirectoryName(args[0]), Util.FilenameCheck(azw.header.title)); }
if (!CreateDirectory(outputdir)) { return; }
foreach (var a in azw.image_sections)
{
Expand All @@ -310,12 +305,6 @@ static void DeDRM(string file)
p.Start();
p.WaitForExit();
}
static void DeleteDir(string path)
{
foreach (string p in Directory.GetFiles(path)) File.Delete(p);
foreach (string p in Directory.GetDirectories(path)) DeleteDir(p);
Directory.Delete(path);
}
static bool CreateDirectory(string path)
{
try
Expand Down
15 changes: 6 additions & 9 deletions src/template/template_cover.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Cover</title>
</head>
<body>
<div style="text-align: center; padding: 0pt; margin: 0pt;">
<svg xmlns="http://www.w3.org/2000/svg" height="100%" preserveAspectRatio="xMidYMid meet" version="1.1" viewBox="0 0 1135 1600" width="100%" xmlns:xlink="http://www.w3.org/1999/xlink">
<image width="1135" height="1600" xlink:href="../Images/{❕image}"/>
</svg>
<body epub:type="cover">
<div style="text-align: center; padding: 0; margin: 0;">
<img style="max-height: 100%; max-width: 100%; width: auto; height: auto; margin: 0; padding: 0;" src="../Images/{❕image}"/>
</div>
</body>
</html>
5 changes: 3 additions & 2 deletions src/template/template_opf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
{❕meta}
<!-- Other meta from azw3
{❕othermeta}-->
<dc:contributor opf:role="bkp">UnpackKindleS ver.{❕version}</dc:contributor>
<dc:contributor id="tool">UnpackKindleS ver.{❕version}</dc:contributor>
<meta refines="#tool" property="role" scheme="marc:relators">bkp</meta>
</metadata>
{❕manifest}
{❕spine}
</package>
</package>
10 changes: 0 additions & 10 deletions src/utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,6 @@ public static string Number(int number, int length = 4)
return r;
}

public static void Packup(string src, string outputfullpath)
{
if (File.Exists(outputfullpath))
{
File.Delete(outputfullpath);
}
ZipFile.CreateFromDirectory(src, outputfullpath);
Log.log("Saved:" + outputfullpath);
}

public static string FilenameCheck(string s)
{
return s
Expand Down
2 changes: 1 addition & 1 deletion src/version.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace UnpackKindleS
{
public class Version{public static string version="20200825";}
public class Version{public static string version="20201007";}
}

0 comments on commit 0c6dad1

Please sign in to comment.