Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Gateway API
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Golang] Mastering File Handling in Go: Download, Extract, and Analyze ZIP Archives

Posted on December 28, 2023December 28, 2023 By nim No Comments on [Golang] Mastering File Handling in Go: Download, Extract, and Analyze ZIP Archives

writing a Go program to download the file, extract its contents, and compute the required information (MD5, SHA1, SHA256 hashes, filename, and size) for each file in the ZIP archive.

package main

import (
    "archive/zip"
    "bytes"
    "crypto/md5"
    "crypto/sha1"
    "crypto/sha256"
    "fmt"
    "io"
    "net/http"
)

func main() {
    // Step 1: Download the ZIP file
    resp, err := http.Get("https://github.com/hungptran/sample-files/raw/main/2_Levels_Archive.zip")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    // Read the body into a buffer
    buf, err := io.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    // Step 2: Read the ZIP file
    zipReader, err := zip.NewReader(bytes.NewReader(buf), int64(len(buf)))
    if err != nil {
        panic(err)
    }

    // Step 3: Iterate through each file
    for _, file := range zipReader.File {
        f, err := file.Open()
        if err != nil {
            panic(err)
        }

        // Read file content
        content, err := io.ReadAll(f)
        if err != nil {
            panic(err)
        }
        f.Close()

        // Compute hashes
        md5Hash := md5.Sum(content)
        sha1Hash := sha1.Sum(content)
        sha256Hash := sha256.Sum256(content)

        // Step 4: Output the information
        fmt.Printf("Filename: %s\n", file.Name)
        fmt.Printf("Size: %d bytes\n", file.UncompressedSize64)
        fmt.Printf("MD5: %x\n", md5Hash)
        fmt.Printf("SHA1: %x\n", sha1Hash)
        fmt.Printf("SHA256: %x\n\n", sha256Hash)
    }
}

To check the information of the ZIP file itself (i.e., the hash values and size of the 2_Levels_Archive.zip file), you don’t need to extract the contents. Instead, you can directly compute the hashes and get the size of the downloaded ZIP file. Here’s the updated Go code for this purpose:

package main

import (
    "crypto/md5"
    "crypto/sha1"
    "crypto/sha256"
    "fmt"
    "io"
    "net/http"
)

func main() {
    // Step 1: Download the ZIP file
    resp, err := http.Get("https://github.com/hungptran/sample-files/raw/main/2_Levels_Archive.zip")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    // Create hash writers
    md5Hash := md5.New()
    sha1Hash := sha1.New()
    sha256Hash := sha256.New()

    // Create a multi-writer to write to all hash writers simultaneously
    writer := io.MultiWriter(md5Hash, sha1Hash, sha256Hash)

    // Copy the response body to the multi-writer
    size, err := io.Copy(writer, resp.Body)
    if err != nil {
        panic(err)
    }

    // Step 2: Output the information
    fmt.Printf("Filename: 2_Levels_Archive.zip\n")
    fmt.Printf("Size: %d bytes\n", size)
    fmt.Printf("MD5: %x\n", md5Hash.Sum(nil))
    fmt.Printf("SHA1: %x\n", sha1Hash.Sum(nil))
    fmt.Printf("SHA256: %x\n", sha256Hash.Sum(nil))
}

The usage of io.MultiWriter in combination with io.Copy and the direct hash computation using md5.Sum(content), sha1.Sum(content), and sha256.Sum256(content) represents two different approaches for calculating hashes in Go, each suitable for different scenarios:

  1. Using io.MultiWriter with io.Copy:
    • This approach is stream-oriented and is particularly efficient when dealing with large data sets or when the data is being received as a stream (like reading from a network response, as in your case).
    • With io.MultiWriter, you write the data once, and it gets passed simultaneously to multiple writers—in this case, the hash functions. This method is memory efficient because it doesn’t require loading the entire data into memory; it processes the data as it is being read.
    • It’s ideal for situations where you are reading data from an io.Reader (like an HTTP response or a file) and you want to process this data in multiple ways (like computing different hashes) while reading it.
  2. Using md5.Sum(content), sha1.Sum(content), and sha256.Sum256(content):
    • This approach is more straightforward but requires you to have the entire data available in a byte slice (content in your example).
    • It’s suitable when you have small data or when the entire data is already present in memory. Since the entire data is available, you can directly pass it to the hash function to get the result.
    • However, this method can be less memory-efficient for large data sets because it requires the entire data to be loaded into memory.

In your original context (downloading a file and computing its hash), using io.MultiWriter with io.Copy is more efficient, especially for large files, because it processes the data in a streaming manner without the need to load the entire file into memory. The direct hash computation method (using md5.Sum(content), etc.) is simpler and more direct but is more suitable for situations where the data is already in memory or when dealing with small amounts of data.

Golang

Post navigation

Previous Post: How to code a tool to connect Kafka by Golang
Next Post: [Golang] Implement Job Queue inside GOlang.

More Related Articles

[kubernetes/client-go] Go clients for talking to a kubernetes cluster. Golang
[Golang] Validate trong golang và echo framework. Golang
[Golang] Approach Channel in Calling another function with Golang Golang
[Golang] Writing file in Golang Golang
How to code a tool to connect Kafka by Golang Golang
[Golang] Return Error in Golang Golang

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Rancher/EKS] Rancher from v2.12.x can not work on eks cluster. April 15, 2026
  • [Telegram/Openclaw] Configure openclaw bot in a Telegram group. March 31, 2026
  • Tutorial: Gateway API + Traefik + oauth2-proxy (Microsoft Entra ID) March 30, 2026
  • Full + incremental backup: When restoring, do deleted files come back? March 27, 2026
  • [K8S] Create long-lived kubeconfig on k8s March 23, 2026

Archives

  • April 2026
  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • AI
    • OpenClaw
  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Gateway API
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2026 NimTechnology.