Git Product home page Git Product logo

opc's Introduction

opc

PkgGoDev Build Status Go Report Card codecov codeclimate License Mentioned in Awesome Go

Package opc implements the ISO/IEC 29500-2, also known as the Open Packaging Convention.

The Open Packaging specification describes an abstract model and physical format conventions for the use of XML, Unicode, ZIP, and other openly available technologies and specifications to organize the content and resources of a document within a package.

The OPC is the foundation technology for many new file formats: .docx, .pptx, .xlsx, .3mf, .dwfx, ...

Features

  • Package reader and writer
  • Package core properties and relationships
  • Part relationships
  • ZIP mapping
  • Package, relationships and parts validation against specs
  • Part interleaved pieces
  • Digital signatures

Examples

Write

// Create a file to write our archive to.
f, _ := os.Create("example.xlsx")

// Create a new OPC archive.
w := opc.NewWriter(f)

// Create a new OPC part.
name := opc.NormalizePartName("docs\\readme.txt")
part, _ := w.Create(name, "text/plain")

// Write content to the part.
part.Write([]byte("This archive contains some text files."))

// Make sure to check the error on Close.
w.Close()

Read

r, _ := opc.OpenReader("testdata/test.xlsx")
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, _ := f.Open()
  io.CopyN(os.Stdout, rc, 68)
  rc.Close()
  fmt.Println()
}

opc's People

Contributors

carlotacb avatar cristiancreteanu avatar qmuntal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

opc's Issues

Unable to add new parts to existing package

Hi,

I have a problem reopening an existing file and adding further parts to it.

I reopen the file with the following:

p.reader, err = opc.OpenReader(outputFilePath)
p.writer, err = opc.NewWriterFromReader(&bytes.Buffer{}, p.reader.Reader)

and I get no error. In the debugger I can also see that the correct file is opened with files added previously.

However, when I attach new files using:

name := opc.NormalizePartName(contentType.PackagePath + "/" + fileName)
part, err := p.writer.Create(name, contentType.Value)
bytes, err := ioutil.ReadFile(contentFilePath)
// Write content to the part.
part.Write(bytes)

I see in the debugger that everything is added to the object but nothing is finally added to the file.

I also tried

part, err := p.writer.CreatePart(&opc.Part{
        Name:        name,
	ContentType: contentType.Value,
}, opc.CompressionNormal)

instead of

part, _ := p.writer.Create(name, contentType.Value)

but it has the same effect.

What am I doing wrong or is there a bug?

Relationship Target attribute shall be relative to the "parent" part

According to the specs:

The package implementer might allow a TargetMode to be provided by a producer. [O1.5]
The TargetMode indicates whether or not the target describes a resource inside the package or outside the package. The valid values, in the Relationships schema, are Internal and External.
The default value is Internal. When set to Internal, the Target attribute shall be a relative reference and that reference is interpreted relative to the “parent” part. For package relationships, the package implementer shall resolve relative references in the Target attribute against the pack URI that identifies the entire package resource. [M1.29] For more information, see Annex B.

func relativePart() {
	filename := "test_files/relative_parts.zip"
	f, _ := os.Create(filename)
	w := opc.NewWriter(f)

	contentType := "plain/text"
	names := []string{"one", "two", "three", "four"}

	var parts []*opc.Part

	prev := ""
	for _, relativeName := range names {
		name := prev + "/" + relativeName + ".txt"
		parts = append(parts, &opc.Part{Name: name, ContentType: contentType})
		prev = prev + "/" + relativeName
	}
	var i int
	for i = 0; i < len(names)-1; i++ {
		parts[i].Relationships = append(parts[i].Relationships,
			&opc.Relationship{ID: strconv.Itoa(i), TargetMode: opc.ModeInternal, TargetURI: names[i+1], Type: contentType})
		pw, _ := w.CreatePart(parts[i], opc.CompressionNone)
		_, _ = pw.Write([]byte(names[i]))
	}
	pw, _ := w.CreatePart(parts[i], opc.CompressionNone)
	pw.Write([]byte(names[i]))

	w.Close()
}

The above code creates a package with absolutes Targets that actually are wrong. Probably the normalizeTargetURI() needs some refactory.

An example of the wrong absolute path at _one/two/rels/three.txt.rels

<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="2" Type="plain/text" Target="/four"></Relationship>
</Relationships>

Delete parts or update existing parts

Hi,

I'm trying to update a file in a package. I naively tried to simply re-add it after modification but this returns the following:

a package shall not contain equivalent part names

Is it possible to delete a part in the package or even just update it with a modified version?

Missing Dublin Core complex type in the Core Properties XML

According to Notes on the W3C XML Schemas for Qualified Dublin Core, the Core properties dates should be serialized with xsi:type="dcterms:W3CDTF" like dcterms:created and dcterms:modified in the below example:

<?xml version="1.0" encoding="UTF-8"?>
<coreProperties xmlns="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:dcterms="http://purl.org/dc/terms/"
    xmlns:dcmitype="http://purl.org/dc/dcmitype/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <dcterms:created xsi:type="dcterms:W3CDTF">2020-10-15T04:05:00Z</dcterms:created>
    <dc:creator>Microsoft Office User</dc:creator>
    <lastModifiedBy>Microsoft Office User</lastModifiedBy>
    <dcterms:modified xsi:type="dcterms:W3CDTF">2020-10-15T04:07:00Z</dcterms:modified>
    <revision>2</revision>
</coreProperties>

Microsoft Office validator complains when xsi:type="dcterms:W3CDTF" is missing

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.