diff --git a/export_csv.go b/export_csv.go index 6a097d6..76604ee 100644 --- a/export_csv.go +++ b/export_csv.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "encoding/csv" "fmt" "os" @@ -22,12 +23,14 @@ func (c *CsvExporter) Convert(filePath string, rows chan []any) error { return err } + buf := bufio.NewWriterSize(f, 4*1024*1024) + enc, err := c.Encoding.Encoder() if err != nil { return err } - w := csv.NewWriter(enc.Writer(f)) + w := csv.NewWriter(enc.Writer(buf)) w.Comma = ';' rowNum := 0 @@ -51,6 +54,12 @@ func (c *CsvExporter) Convert(filePath string, rows chan []any) error { return err } + err = buf.Flush() + if err != nil { + f.Close() + return err + } + err = f.Close() if err != nil { return err diff --git a/export_csvzip.go b/export_csvzip.go index 1acbad8..e81d4dc 100644 --- a/export_csvzip.go +++ b/export_csvzip.go @@ -2,6 +2,7 @@ package main import ( "archive/zip" + "bufio" "encoding/csv" "os" "path/filepath" @@ -22,7 +23,9 @@ func (c *CsvZipExporter) Convert(filePath string, rows chan []any) error { return err } - z := zip.NewWriter(f) + buf := bufio.NewWriterSize(f, 4*1024*1024) + + z := zip.NewWriter(buf) zw, err := z.Create(filepath.Base(filePath) + ".csv") if err != nil { @@ -65,6 +68,12 @@ func (c *CsvZipExporter) Convert(filePath string, rows chan []any) error { return err } + err = buf.Flush() + if err != nil { + f.Close() + return err + } + err = f.Close() if err != nil { return err