17

I am trying to take a chunk of bytes and compress them using the archive/zip package in Go. However, I can't understand it at all. Are there any examples of how it could be done and is there any explanation of that cryptic package?

3
  • 4
    There's an example in the documentation for the package -- golang.org/pkg/archive/zip/#example_Writer Commented Sep 15, 2012 at 20:47
  • 1
    Downvoted for “This question does not show any research effort”. Please tell us what you tried, and/or which references you used (e.g. official zip package, the functions you tried and seemed to match, but did not work). Commented Sep 15, 2012 at 21:20
  • 3
    @Kissaki One of the first barriers one has to overcome when learning a new computer language is making sense of the documentation. I put quite a lot of effort into trying to understand it, and writing bits of code that didn't make any sense at all. The last straw was that I found an article about the zip package that stated that "write() method was removed from Writer, because putting it there had been a mistake". That blew my mind completely and I turned to Stackoverflow. Commented Sep 16, 2012 at 10:05

1 Answer 1

31

Thanks to jamessan I did find the example (which doesn't exactly catch your eye).

Here is what I come up with as the result:

func (this *Zipnik) zipData() {

    // Create a buffer to write our archive to.
    fmt.Println("we are in the zipData function")
    buf := new(bytes.Buffer)

    // Create a new zip archive.
    zipWriter := zip.NewWriter(buf)

    // Add some files to the archive.
    var files = []struct {
        Name, Body string
    }{
        {"readme.txt", "This archive contains some text files."},
        {"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
        {"todo.txt", "Get animal handling licence.\nWrite more examples."},
    }
    for _, file := range files {
    zipFile, err := zipWriter.Create(file.Name)
        if err != nil {
            fmt.Println(err)
        }
        _, err = zipFile.Write([]byte(file.Body))  
        if err != nil {
            fmt.Println(err)
        }
    }

    // Make sure to check the error on Close.
    err := zipWriter.Close()
    if err != nil {
        fmt.Println(err)
    }

    //write the zipped file to the disk
    ioutil.WriteFile("Hello.zip", buf.Bytes(), 0777)    

}

I hope you find it useful :)

Sign up to request clarification or add additional context in comments.

4 Comments

You can accept your own answer. It helps others see that the question is resolved to your satisfaction.
How to write a test around this? how do you read from a buffer created for a zip?
Hi if you are meaning convert from a byte array to string before calling the zip this tells you how stackoverflow.com/questions/14230145/…. The above look like it only puts string content to ZIP.
you need to import archive/zip for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.