// 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
}