Replace byte array in Go

I am trying to take a piece of bytes and compress them using the archive/zip package in Go. However, I cannot understand this. Are there any examples of how this can be done and is there any explanation for this mysterious package?

+4
source share
1 answer

Thanks to jamessan, I found an example (which definitely does not catch the eye).

Here is what I came up with as a 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) } 

Hope you find this helpful :)

+6
source

Source: https://habr.com/ru/post/1434396/


All Articles