zipper

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2020 License: MIT Imports: 3 Imported by: 0

README

zipper

Go Report Card Godoc Releases LICENSE

zipper provides a simple interface to create zip archives in a Go application.

Yup, that's it. It's a one-trick-pony.

Installation

go get github.com/ecnepsnai/zipper

Usage

Create a new ZIP Archive

import (
	"github.com/ecnepsnai/zipper"
)

func Example() {
	archive, err := zipper.New("/path/to/your/zip/file")
	if err != nil {
		panic(err)
	}

	// Add your files
	// (See examples below)

	archive.Close()
}

Adding Files

There are three ways to add files to the archive: Using a writer, copying bytes, and inserting an existing file.

Using a Writer

Archive.NewFile takes in a file name and returns a io.Writer. Writing to that writer will add data to the file name specified in the zip archive.

import (
	"bytes"
	"io"

	"github.com/ecnepsnai/zipper"
)

func Example() {
	var archive *zip.Archive // See zip.NewZipFile for instructions

	// NewFile adds a new file to the zip archive and returns a writer
	writer, err := archive.NewFile("my_file.txt")
	if err != nil {
		panic(err)
	}

	// NewFile returns a writer, so you can use any of the writer operations on it
	// such as io.Copy
	if _, err := io.Copy(writer, bytes.NewBuffer([]byte("Hello world!"))); err != nil {
		panic(err)
	}
}
Copying bytes

Archive.AddFile takes in a file name and slice of bytes. A new file is created with the given file name and populated with the bytes slice in the zip archive.

import (
	"github.com/ecnepsnai/zipper"
)

func Example() {
	var archive *zip.Archive // See zip.NewZipFile for instructions

	data := []byte("Hello world!")

	// AddFile adds a new file to the zip archive with the provided file name populated with the given data
	if err := archive.AddFile("my_file.txt", data); err != nil {
		panic(err)
	}
}
Inserting an Existing File

Archive.InsertFile takes in a file path and will attempt to copy that file to a new file in the zip archive. It will only use the file name, stripping any parent directories.

import (
	"github.com/ecnepsnai/zipper"
)

func Example() {
	var archive *zip.Archive // See zip.NewZipFile for instructions

	path := "/path/to/existing/file"

	// InsertFile copies an existing local file to the zip archive
	if err := archive.InsertFile(path); err != nil {
		panic(err)
	}
}

Documentation

Overview

Package zipper provides a simple interface to create zip archives in a Go application.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Archive

type Archive struct {
	FilePath string
	// contains filtered or unexported fields
}

Archive describes a zip archive

func New

func New(FilePath string) (*Archive, error)

New create a new empty zip archive with the given file path

Example
package main

import (
	"github.com/ecnepsnai/zipper"
)

func main() {
	archive, err := zipper.New("/path/to/your/zip/file")
	if err != nil {
		panic(err)
	}

	// Add your files
	// (See examples below)

	archive.Close()
}
Output:

func (*Archive) AddFile

func (a *Archive) AddFile(name string, data []byte) error

AddFile takes in a file name and slice of bytes. A new file is created with the given file name and populated with the bytes slice in the zip archive.

Example
package main

import (
	"github.com/ecnepsnai/zipper"
)

func main() {
	var archive *zipper.Archive // See zipper.NewZipFile for instructions

	data := []byte("Hello world!")

	// AddFile adds a new file to the zip archive with the provided file name populated with the given data
	if err := archive.AddFile("my_file.txt", data); err != nil {
		panic(err)
	}
}
Output:

func (*Archive) Close

func (a *Archive) Close()

Close flush and save the archive

func (*Archive) InsertFile

func (a *Archive) InsertFile(path string) error

InsertFile takes in a file path and will attempt to copy that file to a new file in the zip archive. It will only use the file name, stripping any parent directories.

Example
package main

import (
	"github.com/ecnepsnai/zipper"
)

func main() {
	var archive *zipper.Archive // See zipper.NewZipFile for instructions

	path := "/path/to/existing/file"

	// InsertFile copies an existing local file to the zip archive
	if err := archive.InsertFile(path); err != nil {
		panic(err)
	}
}
Output:

func (*Archive) NewFile

func (a *Archive) NewFile(name string) (io.Writer, error)

NewFile takes in a file name and returns a `io.Writer`. Writing to that writer will add data to the file name specified in the zip archive.

Example
package main

import (
	"bytes"
	"io"

	"github.com/ecnepsnai/zipper"
)

func main() {
	var archive *zipper.Archive // See zipper.NewZipFile for instructions

	// NewFile adds a new file to the zip archive and returns a writer
	writer, err := archive.NewFile("my_file.txt")
	if err != nil {
		panic(err)
	}

	// NewFile returns a writer, so you can use any of the writer operations on it
	// such as io.Copy
	if _, err := io.Copy(writer, bytes.NewBuffer([]byte("Hello world!"))); err != nil {
		panic(err)
	}
}
Output:

Jump to

Keyboard shortcuts

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