Hello , i want to download a zip file from my website and store means i want to download that zip file in /usr/local folder and then i want to extract that downloaded zip in /usr/local/mypro/ext folder
how to do that?
The documentation for the net/http
package has an example of how to use an HTTP GET to get data from an HTTP Server:
resp, err := http.Get("http://example.com/")
(Just put the URL to the Zip that you want instead of "http://example.com/"
)
That will get you a *http.Response
object (if there was no error). If you check the documentation, you’ll see that there’s a Body
field in the Response
:
// Body represents the response body. // // The response body is streamed on demand as the Body field // is read. If the network connection fails or the server // terminates the response, Body.Read calls return an error. // // The http Client and Transport guarantee that Body is always // non-nil, even on responses without a body or responses with // a zero-length body. It is the caller's responsibility to // close Body. The default HTTP client's Transport may not // reuse HTTP/1.x "keep-alive" TCP connections if the Body is // not read to completion and closed. // // The Body is automatically dechunked if the server replied // with a "chunked" Transfer-Encoding. // // As of Go 1.12, the Body will also implement io.Writer // on a successful "101 Switching Protocols" response, // as used by WebSockets and HTTP/2's "h2c" mode. Body io.ReadCloser
With resp.Body
, you can use any of Go’s io.Reader
/io.Writer
functions (e.g. io.Copy
) to copy the data from the server’s response to a file.
From there, you can use the functions defined in the archive/zip
package (e.g. zip.OpenReader
) to open that file and access its contents.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.