zoukankan      html  css  js  c++  java
  • Golang--计算文件的MD5值

    参考:
    http://blog.csdn.net/u014029783/article/details/53762363

    用法:

    $ go run 01.go -f 1.txt
    b9d228f114d3f42e82c6a0315dd21a3a 1.txt
    
    $ go run 01.go -d tmp
    503ff3936aeaf06adffe610788c7c091 tmpwswFileServer5files1.md
    3cda02c0f373006cebb29438bf0b01c6 tmpwswFileServer5main.go
    86d5206af37b6bcea4d24b54336eee6b tmpwswFileServer5staticjsjquery-2.1.3.min.js
    1ae0e64754a542cbea996dec63c326fd tmpwswFileServer5staticjsootstrap.min.js
    13986017db1f0f1010ed8ff4c81a0c3a tmpwswFileServer5README.md
    c93ebb8bb2ae3bc85ac9de96ef6bb827 tmpwswFileServer5files1.go
    b9d228f114d3f42e82c6a0315dd21a3a tmpwswFileServer5files1.txt
    2b04df3ecc1d94afddff082d139c6f15 tmpwswFileServer5filesKoala.jpg
    c43eeae863aad788074967b8cabd2dd7 tmpwswFileServer5index.html
    bb884d3b6b6b09481c5dc25fb4fac7e5 tmpwswFileServer5staticcssootstrap.min.css
    
    $ go run 01.go -d tmp -merge
    50f235f6435a3cc6700ca68844ced4c2 tmp
    
    

    代码:

    package main
    
    import (
        "crypto/md5"
        "flag"
        "fmt"
        "io/ioutil"
        "os"
        "path/filepath"
    )
    
    //初始化变量
    var directory, file *string
    var merge *bool
    
    //定义init函数,主要是flag的3个参数
    func init() {
        directory = flag.String("d", "", "The directory contains all the files that need to calculate the md5 value")
        file = flag.String("f", "", "The file that need to caclulate the md5 value")
        merge = flag.Bool("merge", false, "Merging all md5 values to one (Folder type only)")
    }
    
    func main() {
        flag.Parse()
    
        //返回Usage
        if *directory == "" && *file == "" {
            flag.Usage()
            return
        }
    
        if *file != "" {
            result, err := Md5SumFile(*file)
            if err != nil {
                panic(err)
            }
            fmt.Printf("%x %s
    ", result, *file) //这里是*file
        }
    
        if *directory != "" {
            result, err := Md5SumFolder(*directory)
            if err != nil {
                panic(err)
            }
    
            // 开启merge,则只计算总的MD5
            if *merge == true {
                var s string
                for _, v := range result {
                    s += fmt.Sprintf("%x", v)
                }
                fmt.Printf("%x %s
    ", md5.Sum([]byte(s)), *directory)
            } else {
                for k, v := range result {
                    fmt.Printf("%x %s
    ", v, k)
                }
            }
        }
    }
    
    func Md5SumFile(file string) (value [md5.Size]byte, err error) {
        data, err := ioutil.ReadFile(file)
        if err != nil {
            return
        }
        value = md5.Sum(data)
        return value, nil
    }
    
    func Md5SumFolder(folder string) (map[string][md5.Size]byte, error) {
        results := make(map[string][md5.Size]byte)
        filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }
    
            //判断文件属性
            if info.Mode().IsRegular() {
                result, err := Md5SumFile(path)
                if err != nil {
                    return err
                }
                results[path] = result
            }
            return nil
        })
        return results, nil
    }
    
    
  • 相关阅读:
    CustomDrawableTextView
    Snippet: align a TextView around an image
    How to import library ?
    Gradle自定义你的BuildConfig
    使用adb shell dumpsys检测Android的Activity任务栈
    Activity intent经常使用的 FLAG
    使用 ContentProviderOperation 来提升性能
    幻方算法
    自己制作的粉碎机批处理程序
    Ubuntu 16.10 server 相关
  • 原文地址:https://www.cnblogs.com/bvac/p/6290168.html
Copyright © 2011-2022 走看看