zippity

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2020 License: MIT Imports: 4 Imported by: 0

README

🚟 zippity

GoDoc

Creates Zip Files, quickly.


Install

go get github.com/groundbreaker/zippity

Usage

import (
  "fmt"

  "github.com/groundbreaker/zippity"
)

func main() {
  // Read a file
	pdf := zippity.ReadFile("test.pdf", "fine.pdf")

  // Or create one from a []byte
	txt := &zippity.File{
		Name: "test.txt",
		Body: []byte("Already have the bytes? Then, create a literal File."),
	}

  // Create a new Zipfile
	zf := zippity.New()

  // Chain as many Add
	zf.Add(pdf).Add(txt)

  zip := zf.Done() // returns the Zipfile as []byte

  // or you can save it to disk with:
  //   zf.Save("path/to/write/file.zip")

  fmt.Printf("zip is %d bytes", len(zip))
}

Documentation

Overview

Package zippity creates zip files, quickly.

Example
package main

import (
	"fmt"

	"github.com/groundbreaker/zippity"
)

func main() {
	pdf := zippity.ReadFile("test.pdf", "fine.pdf")

	zf := zippity.New()
	zip := zf.Add(pdf).Done()
	fmt.Printf("zip is %d bytes", len(zip))
}
Output:

zip is 705454 bytes
Example (Chained)
package main

import (
	"fmt"

	"github.com/groundbreaker/zippity"
)

func main() {
	pdf := zippity.ReadFile("test.pdf", "fine.pdf")

	txt := &zippity.File{
		Name: "test.txt",
		Body: []byte("Already have the bytes? Then, create a literal File."),
	}

	zf := zippity.New()
	zip := zf.Add(pdf).Add(txt).Done()
	fmt.Printf("zip is %d bytes", len(zip))
}
Output:

zip is 705620 bytes

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version retuns the SemVer for this library.

Types

type File

type File struct {
	Name string
	Body []byte
}

File models the data that will be included in a Zipfile.

func ReadFile

func ReadFile(name string, path string) *File

ReadFile reads the File.Body from the given path, and returns a new instance of File using the given name.

type Zipfile

type Zipfile struct {
	Body   *bytes.Buffer
	Client *zip.Writer
}

Zipfile encapsulates the data and behviour required to create zip file archives.

func New

func New() *Zipfile

New creates a new instance of Zipfile.

func (*Zipfile) Add

func (zf *Zipfile) Add(file *File) *Zipfile

Add a File to the Zipfile. This method can be chained to add multiple files.

func (*Zipfile) Done

func (zf *Zipfile) Done() []byte

Done returns the Zipfile as bytes. It must be called after you are done Adding Files to the Zipfile. You should only call Done() or Save(), but never both, or the second call will panic.

func (*Zipfile) Save

func (zf *Zipfile) Save(path string)

Save the Zipfile to the given path. It must be called after you are done Adding Files to the Zipfile. You should only call Save() or Done(), but never both, or the second call will panic.

Example
package main

import (
	"github.com/groundbreaker/zippity"
)

func main() {
	pdf := zippity.ReadFile("test.pdf", "fine.pdf")

	txt := &zippity.File{
		Name: "test.txt",
		Body: []byte("Already have the bytes? Then, create a literal File."),
	}

	zf := zippity.New()
	zf.Add(pdf).Add(txt).Save("test.zip")
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL