-
-
Save wangwenchao/85234d4103096fe634db2232fedc48b8 to your computer and use it in GitHub Desktop.
Gzip archives in Go
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
import ( | |
"compress/gzip" | |
"fmt" | |
"io" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func gzipit(source, target string) error { | |
reader, err := os.Open(source) | |
if err != nil { | |
return err | |
} | |
filename := filepath.Base(source) | |
target = filepath.Join(target, fmt.Sprintf("%s.gz", filename)) | |
writer, err := os.Create(target) | |
if err != nil { | |
return err | |
} | |
defer writer.Close() | |
archiver := gzip.NewWriter(writer) | |
archiver.Name = filename | |
defer archiver.Close() | |
_, err = io.Copy(archiver, reader) | |
return err | |
} |
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
import ( | |
"compress/gzip" | |
"fmt" | |
"io" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func ungzip(source, target string) error { | |
reader, err := os.Open(source) | |
if err != nil { | |
return err | |
} | |
defer reader.Close() | |
archive, err := gzip.NewReader(reader) | |
if err != nil { | |
return err | |
} | |
defer archive.Close() | |
target = filepath.Join(target, archive.Name) | |
writer, err := os.Create(target) | |
if err != nil { | |
return err | |
} | |
defer writer.Close() | |
_, err = io.Copy(writer, archive) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment