自动下载解压工具源码

EXE下载:https://xiaochennote.lanzoub.com/iZYIs3lc3u3i
密码:52sq

#Requires AutoHotkey v1.1
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Global IsRunning := false

IniFile := A_ScriptDir . "\config.ini"
IniRead, SavedUrl, %IniFile%, Settings, WebUrl, %A_Space%
IniRead, SavedSaveDir, %IniFile%, Settings, SaveDir, %A_Space%
IniRead, SavedUnzipDir, %IniFile%, Settings, UnzipDir, %A_Space%
IniRead, SavedTime, %IniFile%, Settings, TimeStr, %A_Now% 

Menu, Tray, NoStandard
Menu, Tray, Add, 显示主界面, RestoreWindow
Menu, Tray, Add, 彻底退出软件, ExitProgram
Menu, Tray, Default, 显示主界面

Gui, Font, s10, 微软雅黑
Gui, Add, Text, x15 y20 w90 h25, 下载网址:
Gui, Add, Edit, x105 y15 w355 h25 vWebUrl, %SavedUrl%

Gui, Add, Text, x15 y55 w90 h25, 保存文件夹:
Gui, Add, Edit, x105 y50 w270 h25 vSaveDir ReadOnly, %SavedSaveDir%
Gui, Add, Button, x385 y49 w75 h27 vBtnSelSave gBtnSelSave, 选择...

Gui, Add, Text, x15 y90 w90 h25, 解压文件夹:
Gui, Add, Edit, x105 y85 w270 h25 vUnzipDir ReadOnly, %SavedUnzipDir%
Gui, Add, Button, x385 y84 w75 h27 vBtnSelUnzip gBtnSelUnzip, 选择...

Gui, Add, Text, x15 y125 w90 h25, 每天定时:
Gui, Add, DateTime, x105 y120 w120 h25 vTimeStr Choose%SavedTime% 1, HH:mm 

Gui, Add, Button, x15 y170 w100 h35 gStartTask vBtnStart, ▶ 启动定时
Gui, Add, Button, x125 y170 w100 h35 gStopTask vBtnStop Disabled, ⏸ 停止定时
Gui, Add, Button, x235 y170 w100 h35 gRunNow vBtnTest, ⚡ 立即测试
Gui, Add, Button, x360 y170 w100 h35 gExitProgram, ❌ 退出软件

Gui, Add, Progress, x15 y220 w445 h22 vMyProgress -Smooth, 0
Gui, Add, Text, x15 y245 w445 h20 vProgressText, 状态: 等待任务开始...

Gui, Add, Text, x15 y275 w100 h20, 运行日志:
Gui, Font, s9
Gui, Add, Edit, x15 y295 w445 h130 vLogBox ReadOnly -Wrap +HScroll, 

Gui, Show, w475 h440, 自动下载解压工具
AddLog("软件已启动,就绪。")
return

BtnSelSave:
    FileSelectFolder, selFolder, , 3, 选择保存下载压缩包的文件夹
    if (selFolder != "")
        GuiControl,, SaveDir, %selFolder%
return

BtnSelUnzip:
    FileSelectFolder, selFolder, , 3, 选择要解压并覆盖到的文件夹
    if (selFolder != "")
        GuiControl,, UnzipDir, %selFolder%
return

StartTask:
    Gui, Submit, NoHide
    if (WebUrl = "" || SaveDir = "" || UnzipDir = "") {
        AddLog("错误:请先填好下载链接和两个文件夹路径!")
        return
    }
    
    IniWrite, %WebUrl%, %IniFile%, Settings, WebUrl
    IniWrite, %SaveDir%, %IniFile%, Settings, SaveDir
    IniWrite, %UnzipDir%, %IniFile%, Settings, UnzipDir
    IniWrite, %TimeStr%, %IniFile%, Settings, TimeStr
    
    GuiControl, Disable, BtnStart
    GuiControl, Disable, BtnTest
    GuiControl, Enable, BtnStop
    GuiControl, Disable, WebUrl
    GuiControl, Disable, SaveDir
    GuiControl, Disable, BtnSelSave
    GuiControl, Disable, UnzipDir
    GuiControl, Disable, BtnSelUnzip
    GuiControl, Disable, TimeStr
    
    FormatTime, DisplayTime, %TimeStr%, HH:mm
    AddLog("【定时已启动】每天 " . DisplayTime . " 自动执行任务。")
    SetTimer, TimerCheck, 1000 
return

StopTask:
    SetTimer, TimerCheck, Off
    GuiControl, Enable, BtnStart
    GuiControl, Enable, BtnTest
    GuiControl, Disable, BtnStop
    GuiControl, Enable, WebUrl
    GuiControl, Enable, SaveDir
    GuiControl, Enable, BtnSelSave
    GuiControl, Enable, UnzipDir
    GuiControl, Enable, BtnSelUnzip
    GuiControl, Enable, TimeStr
    AddLog("【定时已停止】")
return

TimerCheck:
    Gui, Submit, NoHide
    FormatTime, CurrentTime,, HH:mm
    FormatTime, CurrentDay,, yyyyMMdd
    FormatTime, TargetTime, %TimeStr%, HH:mm

    if (CurrentTime == TargetTime && LastRunDay != CurrentDay) {
        LastRunDay := CurrentDay
        AddLog("【时间到】开始执行每天的自动化任务...")
        GoSub, ExecuteTask
    }
return

RunNow:
    Gui, Submit, NoHide
    IniWrite, %WebUrl%, %IniFile%, Settings, WebUrl
    IniWrite, %SaveDir%, %IniFile%, Settings, SaveDir
    IniWrite, %UnzipDir%, %IniFile%, Settings, UnzipDir
    IniWrite, %TimeStr%, %IniFile%, Settings, TimeStr
    GoSub, ExecuteTask
return

ExecuteTask:
    if (IsRunning)
        return
    IsRunning := true

    Gui, Submit, NoHide
    if (WebUrl = "" || SaveDir = "" || UnzipDir = "") {
        AddLog("错误:缺少路径或网址!")
        IsRunning := false
        return
    }

    GuiControl,, MyProgress, 0
    GuiControl,, ProgressText, 状态: 准备中...
    GuiControl, Disable, BtnTest
    GuiControl, Disable, BtnStop
    GuiControl, Disable, BtnStart

    SplitPath, WebUrl, OutFileName
    OutFileName := RegExReplace(OutFileName, "\?.*$") 
    if (OutFileName = "")
        OutFileName := "AutoDownload.zip"
    ZipFilePath := SaveDir . "\" . OutFileName

    AddLog("正在连接服务器获取文件信息...")
    GuiControl,, ProgressText, 状态: 正在连接服务器...
    FileDelete, %A_Temp%\header.txt
    RunWait, cmd.exe /c curl.exe -sI -L "%WebUrl%" > "%A_Temp%\header.txt", , Hide
    FileRead, headers, %A_Temp%\header.txt
    
    TotalSize := 0
    pos := 1
    while (pos := RegExMatch(headers, "i)Content-Length:\s*(\d+)", match, pos)) {
        TotalSize := match1
        pos += StrLen(match)
    }

    if FileExist(ZipFilePath)
        FileDelete, %ZipFilePath%

    AddLog("开始下载文件: " . OutFileName)
    Run, cmd.exe /c curl.exe -s -L "%WebUrl%" -o "%ZipFilePath%", , Hide, PID_Curl
    
    TotalMB := Format("{:.2f}", TotalSize / 1048576)
    
    Loop {
        Process, Exist, %PID_Curl%
        if (!ErrorLevel) {
            break
        }
        
        FileGetSize, CurrentSize, %ZipFilePath%
        CurrentMB := Format("{:.2f}", CurrentSize / 1048576)
        
        if (TotalSize > 0) {
            Percent := Round((CurrentSize / TotalSize) * 100)
            if (Percent > 100)
                Percent := 100
            GuiControl,, MyProgress, %Percent%
            GuiControl,, ProgressText, 下载进度: %Percent%`% (%CurrentMB% MB / %TotalMB% MB)
        } else {
            GuiControl,, ProgressText, 下载中... 已下载: %CurrentMB% MB (未知总大小)
        }
        Sleep, 150
    }
    
    FileGetSize, FinalSize, %ZipFilePath%
    if (FinalSize < 100) {
        AddLog("下载异常失败!")
        GuiControl,, ProgressText, 状态: 下载失败
        GoSub, TaskEnd
        return
    }

    GuiControl,, MyProgress, 100
    GuiControl,, ProgressText, 状态: 下载完成,准备解压...
    AddLog("下载成功,开始解压替换...")
    Sleep, 500
    GuiControl,, MyProgress, 0

    SevenZipPath := "C:\Program Files\7-Zip\7z.exe"
    if !FileExist(SevenZipPath)
        SevenZipPath := "C:\Program Files (x86)\7-Zip\7z.exe"
        
    FileDelete, %A_Temp%\uz_prog.txt
    
    CmdStr := "cmd.exe /c """"" . SevenZipPath . """ x """ . ZipFilePath . """ -o""" . UnzipDir . """ -y -bsp1 > """ . A_Temp . "\uz_prog.txt"""""
    Run, %CmdStr%, , Hide, PID_7z

    Loop {
        Process, Exist, %PID_7z%
        if (!ErrorLevel) {
            break
        }
        
        FileRead, uz_out, %A_Temp%\uz_prog.txt
        pos := 1
        lastPct := 0
        while (pos := RegExMatch(uz_out, "(\d+)%", match, pos)) {
            if (match1 <= 100)
                lastPct := match1
            pos += StrLen(match)
        }
        
        if (lastPct > 0) {
            GuiControl,, MyProgress, %lastPct%
            GuiControl,, ProgressText, 解压进度: %lastPct%`%
        }
        Sleep, 150
    }

    GuiControl,, MyProgress, 100
    GuiControl,, ProgressText, 状态: 任务全部完成!
    AddLog("【任务完成】文件已成功解压并覆盖!")
    
    TaskEnd:
    IsRunning := false
    GuiControlGet, btnText,, BtnStop
    if (btnText != "⏸ 停止定时") {
        GuiControl, Enable, BtnTest
        GuiControl, Enable, BtnStart
    } else {
        GuiControl, Enable, BtnTest
        GuiControl, Enable, BtnStop
    }
return

AddLog(LogText) {
    Global LogBox
    FormatTime, NowTime,, yyyy-MM-dd HH:mm:ss
    GuiControlGet, CurrentLog,, LogBox
    NewLog := "[" . NowTime . "] " . LogText . "`r`n" . CurrentLog
    GuiControl,, LogBox, %NewLog%
}

GuiSize:
    if (ErrorLevel = 1) { 
        Gui, Hide
    }
return

GuiClose:
    Gui, Hide
return

RestoreWindow:
    Gui, Show
return

ExitProgram:
    ExitApp

 

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容