Skip to content

Commit

Permalink
feat: use zip.Reader instead of zip.ReadCloser
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu committed Aug 12, 2023
1 parent f6084e2 commit a35049a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.20
go-version: 1.21
- name: Checkout
uses: actions/checkout@v3
- name: install dep
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ This parser also only reads MetaData and Manifest.

### How to use?
```go
zipReader, err := zip.OpenReader("./test_epub_v3_0.epub")
binaryFile, err := os.ReadFile("./test_epub_v3_0.epub")
if err != nil {
t.Log(err.Error())
t.Fail()
println(err.Error())
return;
}
zipReader, err := zip.NewReader(bytes.NewReader(binaryFile), int64(len(binaryFile)))
if err != nil {
println(err.Error())
return;
}
defer zipReader.Close()

book, err := OpenBook(zipReader)
if err != nil {
t.Log(err.Error())
t.Fail()
println(err.Error())
return;
}
```
After that, all metadata and manifest data can be found in the book Object
4 changes: 2 additions & 2 deletions book.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
type Book struct {
Opf Package
Container Container
ZipReader *zip.ReadCloser
ZipReader *zip.Reader
}

func OpenBook(reader *zip.ReadCloser) (*Book, error) {
func OpenBook(reader *zip.Reader) (*Book, error) {
book := &Book{ZipReader: reader}
err := book.readXML("META-INF/container.xml", &book.Container)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package epub

import (
"archive/zip"
"bytes"
"os"
"strconv"
"testing"
)

func Test_parse_epub_3_0_opf(t *testing.T) {
zipReader, err := zip.OpenReader("./test_epub_v3_0.epub")
binaryFile, err := os.ReadFile("./test_epub_v3_0.epub")
if err != nil {
t.Log(err.Error())
t.Fail()
}
zipReader, err := zip.NewReader(bytes.NewReader(binaryFile), int64(len(binaryFile)))
if err != nil {
t.Log(err.Error())
t.Fail()
}
defer zipReader.Close()

book, err := OpenBook(zipReader)
if err != nil {
Expand Down

0 comments on commit a35049a

Please sign in to comment.