Append raw Goreq.body response to file

71 views Asked by At

Hi I would like to append a downloaded file raw response to a file. Supposed that I'm trying to download a 1GB file and download process will be done in multiple request. Let say 100mb per request. Now this all the 10 parts should be put into one single file to be read. I don't know how to achieve this. I'm using GOREQ to request this resource and the raw data is in Goreq.body format. Below is my sample code

newFile := "./newfile.gz"
out, err := os.Create(newFile)
defer out.close()  

if err != nil {
    fmt.Println("Failed to download file")
    return
}

for i := 0; i < 10; i++ {
    // This will return the items in part of 100mb/part. 
    // GetRawDataFromEbay returns a goreq.body(raw) as its response
    response, _ = GetRawDataFromEbay(10485760) 

    out.Write(response) // This does not work
}

I know Write accept []byte as parameter. But how can I append the retrieve goreq.body raw data to a file?

2

There are 2 answers

0
MadzQuestioning On BEST ANSWER

Ok found the answer it seems that I don't need to do anything fancy. I just have to use io.copy and that's it. Just for reference for others having the same issue below is my code

newFile := "./newfile.gz"
out, err := os.Create(newFile)
defer out.close()   

if err != nil {
   fmt.Println("Failed to download file")
   return
}

for i := 0; i < 10; i++ {
    // This will return the items in part of 100mb/part. 
    // GetRawDataFromEbay returns a goreq.body(raw) as its response
    response, _ = GetRawDataFromEbay(10485760) 
    io.Copy(file, response) // Where file is an instance of *os.File
}

So basically if the Goreq.body response is in by part when you copy the different response (in part) in the same file it will append it automatically

0
IhtkaS On

You can use fileObj.WriteAt(content, offSet) to append content to the file.

package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println("test")
    f, err := os.Create("/Users/sakthi-2528/test/test213123.txt")
    if err != nil {
        fmt.Println(err)
        return
    }
    n, err := f.WriteAt([]byte("Foo"), 0)
    if err != nil {
        fmt.Println(err)
        return
    }
    off := n
    n, err = f.WriteAt([]byte("Bar"), int64(off))
    if err != nil {
        fmt.Println(err)
        return
    }
    off += n
    _, err = f.WriteAt([]byte("FooBar"), int64(off))
    if err != nil {
        fmt.Println(err)
        return
    }
    defer f.Close()
}