I am using the archive/zip
library and I need to make a modification to a file inside a zip archive on the fly before I upload it to s3. Currently I am getting back an io.ReadCloser
but the function I am using to modify the user requires an io.Reader
:
//Function Signature:
Convert(r io.Reader, w io.Writer, ...)
I also need an io.Writer
... Bonus points if you can help me determine how to create a writer as well. Here is some example code that may give some context:
for _, f := range r.File {
if filepath.Ext(f.Name) != ".txt" {
_, filename := path.Split(f.Name)
var rc io.ReadCloser
if rc, err = f.Open(); err == nil {
// FIXME: Convert
if err = Convert(rc.Reader, *zip.Writer.Create(f.Name), ...); err != nil {
errStr := fmt.Sprintf("Unable to convert")
log.Println(errStr)
} else {
log.Println("Success!")
}
// Upload to s3
.....
}
}
}
To convert an io.ReadCloser
to an io.Reader
you do:
An io.ReadCloser
is already an io.Reader
by virtue of the fact that all methods of the io.Reader
interface (specifically a Read(p []byte) (n int, err error)
method) are also provided by io.ReadCloser
.
Converting an io.Reader
into an io.Writer
is an entirely different matter. You can't really do that in any general way. You'll need to elaborate on what you're trying to do.