go文件去重

2020-04-21  本文已影响0人  月下蓑衣江湖夜雨
// 跟踪文件去重
// 作者:张开
// 日期:2020-04-21
package main

import (
    "archive/zip"
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "path/filepath"
    "strings"
)

// 常量定义
const(
    EmptyStr = ""
)

var dirPath string        // 源目录,需要处理的文件放在这里
var workPathPrefix string // 工作目录,解压后放在这个目录里

// 用户imsi表
var users = []string{
    "4600728015*****",
    "4600728015*****",
    "4600728015*****",
    "4600728015*****",
    "4600728015*****",
    "4600091331*****",
    "4600728015*****",
    "4600728015*****",
    "4600751615*****",
    "4600751615*****",
}


func main(){
    //dirPath = "D:/跟踪/421/0924"
    //workPathPrefix = "D:/00zkai"
    fmt.Println("请输入需要去重的文件夹路径...")
    fmt.Scanln(&dirPath)
    fmt.Println("请输入去重后的文件夹路径...")
    fmt.Scanln(&workPathPrefix)
    fmt.Printf("去重文件夹路径[%v], 去重后文件路径[%v]\n", dirPath, workPathPrefix)

    initWorkSpace(workPathPrefix)
    traverse(dirPath, workPathPrefix)
    UnZipAll(workPathPrefix)
    MoveAll(workPathPrefix)
    fmt.Println("End!")
    fmt.Println("输入任何键结束。。。")
    fmt.Scanln(&dirPath)
}

// 遍历文件夹,获取该文件夹下所有文件
func traverse(dirPath, workPathPrefix string){
    filepath.Walk(dirPath, func (path string, info os.FileInfo, err error) error {
        if !isZipFile(path) {
            return nil
        }

        // 获取文件所属用户
        imsi := getUser(path)
        if imsi == EmptyStr {
            return nil
        }

        // 拷贝到目标位置处
        fileName := getFileName(path)
        fmt.Printf("path[%v], imsi[%v], fileName[%v]\n", path, imsi, fileName)
        moveFile(path, workPathPrefix + "/" + imsi + "/" + fileName)

        return nil
    })
}

// 判断是否为zip文件
func isZipFile(path string) bool {
    return strings.Contains(path, ".zip")
}

// 判断是哪个用户的压缩文件
func getUser(path string) string {
    for k,_ := range users {
        if strings.Contains(path, users[k]) {
            return users[k]
        }
    }

    return EmptyStr
}

// 移动文件:就是重命名
func moveFile(source, target string) {
    err := os.Rename(source, target)
    if err != nil {
        fmt.Printf("moveFile failed, source[%v], target[%v], err[%v]", source, target, err)
    }
}

// 获得文件名
func getFileName(path string) string {
    strs := strings.Split(path, "\\")
    if len(strs) == 0 {
        return EmptyStr
    }

    return strs[len(strs)-1]
}

// 初始化工作目录
func initWorkSpace(workPathPrefix string) {
    // 先删除所有
    os.RemoveAll(workPathPrefix)

    // 目录是否存在
    _, err := os.Stat(workPathPrefix)
    if err == nil {
        fmt.Println("initWorkSpace:工作目录存在!")
        return
    } else {
        err := os.Mkdir(workPathPrefix, os.ModePerm)
        if err != nil {
            fmt.Printf("initWorkSpace:创建工作目录失败,err[%v]\n", err)
            return
        }
    }

    for k, _ := range users {
        fmt.Printf("initWorkSpace:创建工作目录[%v]\n", workPathPrefix + "/" + users[k])
        os.Mkdir(workPathPrefix + "/" + users[k], os.ModePerm)
    }
}

// 解压工作目录下的所有文件
func UnZipAll(workPathPrefix string){
    for k,_ := range users {
        path := workPathPrefix + "/" + users[k]
        filepath.Walk(path, func (path string, info os.FileInfo, err error) error {
            if !isZipFile(path) {
                return nil
            }

            Unzip(path, path + ".dir")
            os.Remove(path)
            return nil
        })
    }
}


// 解压文件
func Unzip(zipFile string, destDir string) error {
    fmt.Printf("Unzip zipFile[%v], destDir[%v]", zipFile, destDir)
    zipReader, err := zip.OpenReader(zipFile)
    if err != nil {
        return err
    }
    defer zipReader.Close()

    for _, f := range zipReader.File {
        fpath := filepath.Join(destDir, f.Name)
        if f.FileInfo().IsDir() {
            os.MkdirAll(fpath, os.ModePerm)
        } else {
            if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
                return err
            }

            inFile, err := f.Open()
            if err != nil {
                return err
            }
            defer inFile.Close()

            outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
            if err != nil {
                return err
            }
            defer outFile.Close()

            _, err = io.Copy(outFile, inFile)
            if err != nil {
                return err
            }
        }
    }
    return nil
}

// 移动全部
func MoveAll(workPathPrefix string) {
    // 先建立目标文件夹
    for k, _ := range users {
        fmt.Printf("initWorkSpace:创建工作目录[%v]\n", workPathPrefix + "/" + users[k])
        os.Mkdir(workPathPrefix + "/" + users[k] + "_reduce", os.ModePerm)
    }

    for k, _ := range users {
        path := workPathPrefix + "/" + users[k]
        filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
            if !strings.Contains(path, ".gz") && !strings.Contains(path, ".ptmf") {
                return nil
            }
            // 源文件,目标文件
            targetFileName := workPathPrefix + "/" + users[k] + "_reduce" + "/" + getFileName(path)
            fmt.Printf("源文件[%v], 目标文件[%v]\n", path, targetFileName)
            _, err2 := os.Stat(targetFileName)
            if err2 == nil {
                fmt.Printf("targetFileName[%v]存在!\n", targetFileName)
                os.Remove(path) // 删除原始的!
                return nil
            } else {
                moveFile(path, targetFileName)
            }

            return nil
        })
    }

    // 删除空的文件夹
    for k, _ := range users {
        fmt.Printf("initWorkSpace:创建工作目录[%v]\n", workPathPrefix + "/" + users[k])
        //if isDirEmpty(workPathPrefix + "/" + users[k]) {
            os.RemoveAll(workPathPrefix + "/" + users[k])
        //}
    }
}

func isDirEmpty(path string) bool{
    dir, _ := ioutil.ReadDir(path)
    if len(dir) == 0 {
        return true
    }

    return false
}
上一篇下一篇

猜你喜欢

热点阅读