Created
June 24, 2015 10:14
-
-
Save xyproto/f4915d7e208771f3adc4 to your computer and use it in GitHub Desktop.
gzip compression/decompression example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"compress/gzip" | |
"io" | |
"io/ioutil" | |
"bytes" | |
"log" | |
) | |
// Write gzipped data to a Writer | |
func gzipWrite(w io.Writer, data []byte) error { | |
// Write gzipped data to the client | |
gw, err := gzip.NewWriterLevel(w, gzip.BestSpeed) | |
defer gw.Close() | |
gw.Write(data) | |
return err | |
} | |
// Write gunzipped data to a Writer | |
func gunzipWrite(w io.Writer, data []byte) error { | |
// Write gzipped data to the client | |
gr, err := gzip.NewReader(bytes.NewBuffer(data)) | |
defer gr.Close() | |
data, err = ioutil.ReadAll(gr) | |
if err != nil { | |
return err | |
} | |
w.Write(data) | |
return nil | |
} | |
func main() { | |
s := "some data" | |
fmt.Println("original:\t", s) | |
var buf bytes.Buffer | |
err := gzipWrite(&buf, []byte(s)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("compressed:\t", buf.String()) | |
var buf2 bytes.Buffer | |
err = gunzipWrite(&buf2, buf.Bytes()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("decompressed:\t", buf2.String()) | |
} |
@Shailesh1994 Here's one way:
data, err := ioutil.ReadFile("test.xml")
if err != nil {
log.Fatal(err)
}
file, err := os.Create("test.xml.gz")
if err != nil {
log.Fatal(err)
}
err = gzipWrite(file, data)
if err != nil {
log.Fatal(err)
}
Thank you so much Sir
…On Thu, Jan 30, 2020, 13:44 Alexander F. Rødseth ***@***.***> wrote:
@Shailesh1994 <https://github.com/Shailesh1994> Here's one way:
data, err := ioutil.ReadFile("test.xml")if err != nil {
log.Fatal(err)
}
file, err := os.Create("test.xml.gz")if err != nil {
log.Fatal(err)
}
err = gzipWrite(file, data)if err != nil {
log.Fatal(err)
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/f4915d7e208771f3adc4?email_source=notifications&email_token=ALFWZPD465EIC5J6JDGTJVLRAKD7HA5CNFSM4KNQDQDKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGA5UG#gistcomment-3160899>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALFWZPGRP5KD6VI4R25QJKLRAKD7HANCNFSM4KNQDQDA>
.
Do you have a Read Me file of following code and I want to integrate this code for the sitemap .Can you help me out if possible
I don't understand the question. Please try Stack Overflow.
Thanks for the examples!!
Maybe it might be better to use io.Copy when you write?
func gunzipWrite(w io.Writer, data []byte) error {
// Write gzipped data to the client
gr, err := gzip.NewReader(bytes.NewBuffer(data))
defer gr.Close()
_, err := io.Copy(w, gr)
return err
}
Yes, io.Copy
is probably better.
can we read gz file in chunks?
Yes, just use io.CopyN
instead of io.Copy
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you to compress an xml file which is in the same directory where your program is saved how can we save the gzip file that you created.