how read epub file line by line

2,248 views
Skip to first unread message

mahdi

unread,
Jul 31, 2011, 4:33:08 PM7/31/11
to epublib
Hi , How can i read epub file line by line with epublib and show it on
textBox ?
thanks

Paul Siegmann

unread,
Aug 1, 2011, 12:17:35 PM8/1/11
to epublib
Hi Mahdi,

Not sure whether I understand your question correctly, but maybe a
little background will help.

An epub file is basically a zip file. It contains HTML files for the
book content, CSS, images and fonts.
It also contains several XML files that contain the book's title,
author and the order in which the chapters of the book should be read.

Epublib makes the contents of all these files available via the
Book.getResources() method.

The Epub standard defines three indexes into the book's contents.
The Spine, which is the list of chapters if you were to read the book
from cover to cover.
The Table of Contents.
And finally the Guide, which is a collection of predefined 'special'
pages.

Epublib makes all 3 ways of accessing the content available. The Spine
is available via the Book.getSpine() method, the Table of Contents is
available via the Book.getTableOfContents() method and the Guide is
available via the Book.getGuide() method.

The problem of this is that none of these methods guarantee to give
you the entire contents of the book.
The Spine often does not contain the cover page.
Sometimes large HTML files are split up in pieces to save memory.
These individual pieces will all be in the Spine, but only the first
piece will be referred to by the Table of Contents.
The Table of Contents may point to chapters that are not in the Spine
or the Guide.

Finally the Guide. The Guide is used to point to predefined 'special'
pages. Pages like the cover pages, the list of images page, the table
of contents page, etc.
Of the available Guide pages only the cover page is widely used.

Finally there may be HTML files in the epub file that are referenced
by none of these indexes.

Now a common question is: how do I retrieve the entire contents of the
book.
The answer is: you can't. At least not reliably.

Epublib has the Book.getContents() method. This gives you all
Resources of the Book that can be reached via the Spine, the
TableOfContents or the Guide.

Hope this helps,

Paul Siegmann

Mahdi Farzami

unread,
Aug 2, 2011, 10:50:52 AM8/2/11
to epu...@googlegroups.com
Hi
thanks for your answer . i use this code to show my epub book on textView but it show the picture that i attach it.

package com.mahdi.File_Reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.jar.JarFile;

import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.widget.AnalogClock;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.mahdi.File_Reader.jchmlib.*;

import nl.siegmann.epublib.*;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubReader;

public class File_ReaderActivity extends Activity {

    String str1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv1 = (TextView) findViewById(R.id.textView1);

        AssetManager assetManager = getAssets();
        try {
            InputStream inputStream = assetManager.open("xxx.epub");
            Book eBook = new EpubReader().readEpub(inputStream);
            str1 = eBook.getSpine().toString();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tv1.setText(str1);
    }

}


Can you give me code example to how to use to epublib
thanks alot
Untitled.png

Paul Siegmann

unread,
Aug 2, 2011, 2:28:58 PM8/2/11
to epublib
Hi Mahdi,

Try getting the contents of the first chapter by doing
Book.getSpine().getSpineReferences().get(0).getResource().getInputStream().
If there is no first chapter then this will crash with an
IndexOutOfBoundsException.
If there is a first chapter then the raw html source of it will be
printed in your TextView.

regards,
Paul
>  Untitled.png
> 231KViewDownload

Mahdi Farzami

unread,
Aug 2, 2011, 4:42:17 PM8/2/11
to epu...@googlegroups.com
ok i use this code :

str1 = eBook.getSpine().getSpineReferences().get(0).getResource().getInputStream().toString();

but it`s show in my textView this output :
java,io,ByteArrayInputStream@405c8c18

Xpepermint

unread,
Aug 12, 2011, 5:52:55 AM8/12/11
to epublib
Hey... you simply convert it to string like this


InputStream is =
book.getSpine().getSpineReferences().get(0).getResource().getInputStream();

BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
Log.e("epublib", sb.toString());

Paul Siegmann

unread,
Aug 14, 2011, 2:57:20 PM8/14/11
to epublib
Hi Xpepermint,

A slightly easier way is to call getReader directly, like this:
Reader reader =
book.getSpine().getSpineReferences().get(0).getResource().getReader()

This has as an additional benefit that you get the Reader in the
correct encoding.

regards,
Paul

Ravi Prajapati

unread,
Mar 23, 2017, 5:18:43 AM3/23/17
to epublib
Hi

I have epub file which having external css/font file in different located folder when i'm trying to read it using epublib-tool the content loaded successfully but the given css and font style is not working in page. 

Is there any changes need to do in epublib-tool or epublib-core code could please suggest me for the same.

Here I have attached that epub file for your reference.


Thank you.

Have a gread day.

Ravi 
dhigaMasti_2_2.epub
Reply all
Reply to author
Forward
0 new messages