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 ¶
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 ¶
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) InsertFile ¶
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 ¶
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: