~dricottone/epub2html

epub2html/epub.go -rw-r--r-- 1012 bytes
ae806b41Dominic Ricottone Fixing blockquotes and refactoring 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Functions for handling e-pub archives

package main

import (
	"io"
	"fmt"
	"strings"
	"archive/zip"
)

func ReadArchive(filename string) (map[string]string, error) {
	// Open archive
	archive_reader, err := zip.OpenReader(filename)
	if err != nil {
		return nil, err
	}
	defer archive_reader.Close()

	var archive = map[string]string{}

	// Loop over files in archive
	for _, file := range archive_reader.File {

		// Skip these less useful files
		if (file.Name == "mimetype" || file.Name == "content.opf" || strings.HasPrefix(file.Name, "META-INF") || strings.HasSuffix(file.Name, ".css")) {
			continue
		}

		// Open file
		file_reader, err := file.Open()
		if err != nil {
			fmt.Printf("error: %s\n", err)
		}

		// Copy file contents into a string builder
		buffer := new(strings.Builder)
		if _, err := io.Copy(buffer, file_reader); err != nil {
			fmt.Printf("error: %s\n", err)
		}

		// Store final string mapped by the file name
		archive[file.Name] = buffer.String()
	}

	return archive, nil
}