I am trying to read the zip file in chunks. The scenario is, I am uploading the large file through our endpoint and trying to read that zip file in smaller chunks. I have been following this code but it throws as error stating that not a valid zip file.
package main
import (
"archive/zip"
"bytes"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
// Route to handle file uploads
e.POST("/upload", uploadHandler)
// Start the server
e.Logger.Fatal(e.Start(":8080"))
}
func uploadHandler(c echo.Context) error {
// Read the form data including the file
form, err := c.MultipartForm()
if err != nil {
return err
}
// Get the file from form data
fileHeaders := form.File["zipFile"]
if len(fileHeaders) == 0 {
return c.String(http.StatusBadRequest, "No file uploaded")
}
// Open the uploaded file
file, err := fileHeaders[0].Open()
if err != nil {
return err
}
defer file.Close()
// Create a new zip file to write the extracted contents
outFile, err := os.Create("extracted.zip")
if err != nil {
return err
}
defer outFile.Close()
// Create a zip writer for the output file
zipWriter := zip.NewWriter(outFile)
defer zipWriter.Close()
// Accumulate chunks of data until we have a complete zip file
var buf bytes.Buffer
_, err := io.CopyN(&buf, file, 8192) // Read 8KB chunks
if err != nil {
if err == io.EOF {
break
}
return err
}
// Create a zip reader for the current chunk of data
zipReader, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
if err != nil {
return err
}
// Iterate over each file in the zip file
for _, f := range zipReader.File {
// Open the file in the zip archive
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
// Create a new file in the output zip
outFile, err := zipWriter.Create(f.Name)
if err != nil {
return err
}
// Copy the file contents to the output zip
_, err = io.Copy(outFile, rc)
if err != nil {
return err
}
}
// Return success response
return c.String(http.StatusOK, "Files extracted and written to extracted.zip")
}
This below code block throwing the error:
// Create a zip reader for the current chunk of data
zipReader, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
if err != nil {
return err
}
The intention of this code is reading the zip file into smaller chunks and write it into another zip file.