Skip to content

Instantly share code, notes, and snippets.

@marianozunino
Created October 16, 2023 22:02
Show Gist options
  • Select an option

  • Save marianozunino/90b2c48157191046c99f3d4682ae0a6d to your computer and use it in GitHub Desktop.

Select an option

Save marianozunino/90b2c48157191046c99f3d4682ae0a6d to your computer and use it in GitHub Desktop.
asd
package main
import (
"archive/zip"
"bufio"
"fmt"
"io"
"os"
"runtime"
"time"
)
func main() {
go Monitor()
zipFilePath := "sample.zip"
outputDirectory := "./extracted"
// Open the ZIP file for reading.
zipFile, err := zip.OpenReader(zipFilePath)
if err != nil {
fmt.Println("Error opening ZIP file:", err)
return
}
defer zipFile.Close()
// Create the output directory if it doesn't exist.
if err := os.MkdirAll(outputDirectory, os.ModePerm); err != nil {
fmt.Println("Error creating output directory:", err)
return
}
for _, file := range zipFile.File {
// Open each file in the ZIP archive.
inFile, err := file.Open()
if err != nil {
fmt.Println("Error opening file in ZIP:", err)
return
}
defer inFile.Close()
// Create the output file.
outFile, err := os.Create(outputDirectory + "/" + file.Name)
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer outFile.Close()
// Copy the content from the ZIP file to the output file in chunks.
_, err = io.Copy(outFile, inFile)
if err != nil {
fmt.Println("Error copying content:", err)
return
}
}
fmt.Println("Extraction complete.")
csvFile, _ := os.Open("./extracted/sample.csv")
reader := bufio.NewReader(csvFile)
for {
_, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
fmt.Println("Error reading file:", err)
return
}
// NOTE: demora mas imprimiendo que la propia ejecución
// fmt.Println(line)
}
}
func Monitor() {
// print memory usage every 0.5 seconds and run the main function
for {
printMemUsage()
time.Sleep(100 * time.Millisecond)
}
}
func printMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Current = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotal = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment