Permalink
Browse files

as OpenReader boilerplate and example

  • Loading branch information...
qmuntal committed Feb 5, 2019
1 parent 8690a89 commit 73390f0e1de8e570083d8e58accc2b68a4b79e15
Showing with 75 additions and 3 deletions.
  1. +27 −0 example_test.go
  2. +2 −0 go.mod
  3. +7 −0 go.sum
  4. +39 −3 reader.go
@@ -2,7 +2,10 @@ package opc_test

import (
"bytes"
"fmt"
"io"
"log"
"os"

"github.com/qmuntal/opc"
)
@@ -32,3 +35,27 @@ func ExampleWriter() {
log.Fatal(err)
}
}

func ExampleReader() {
r, err := opc.OpenReader("testdata/test.xlsx")
if err != nil {
log.Fatal(err)
}
defer r.Close()

// Iterate through the files in the archive,
// printing some of their contents.
for _, f := range r.Files {
fmt.Printf("Contents of %s with type %s :\n", f.Name, f.ContentType)
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
_, err = io.CopyN(os.Stdout, rc, 68)
if err != nil {
log.Fatal(err)
}
rc.Close()
fmt.Println()
}
}
2 go.mod
@@ -1 +1,3 @@
module github.com/qmuntal/opc

require github.com/stretchr/testify v1.3.0
7 go.sum
@@ -1 +1,8 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -2,6 +2,7 @@ package opc

import (
"io"
"os"
"path/filepath"
"strings"
)
@@ -15,9 +16,44 @@ type archive interface {
Files() []archiveFile
}

type ReadCloser struct {
f *os.File
*Reader
}

// OpenReader will open the OPC file specified by name and return a ReadCloser.
func OpenReader(name string) (*ReadCloser, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
r, err := NewReader(f, fi.Size())
return &ReadCloser{f: f, Reader: r}, err
}

// Close closes the OPC file, rendering it unusable for I/O.
func (r *ReadCloser) Close() error {
return r.f.Close()
}

// File is used to read a part from the OPC package.
type File struct {
*Part
a archiveFile
}

func (f *File) Open() (io.ReadCloser, error) {
return f.a.Open()
}

// Reader implements a OPC file reader.
type Reader struct {
Parts []*Part
Files []*File
Relationships []*Relationship
Properties CoreProperties
p *pkg
@@ -48,7 +84,7 @@ func (r *Reader) loadPackage() error {
return err
}
files := r.r.Files()
r.Parts = make([]*Part, 0, len(files)-1) // -1 is for [Content_Types].xml
r.Files = make([]*File, 0, len(files)-1) // -1 is for [Content_Types].xml

for _, file := range files {
fileName := "/" + file.Name()
@@ -68,7 +104,7 @@ func (r *Reader) loadPackage() error {
return err
}
part := &Part{Name: fileName, ContentType: cType, Relationships: rels.findRelationship(fileName)}
r.Parts = append(r.Parts, part)
r.Files = append(r.Files, &File{part, file})
r.p.add(part)
}
}

0 comments on commit 73390f0

Please sign in to comment.