windows 如何创建 .BAT 文件以下载和解压 zip 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2874387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:29:51  来源:igfitidea点击:

How to create a .BAT file to download and unpack a zip file?

windowshttpbatch-filezipunpack

提问by Rella

How to create a .BAT file to download and unpack a zip file from HTTP server?

如何创建 .BAT 文件以从 HTTP 服务器下载和解压 zip 文件?

We have links like http://example.com/folder.zipand absolute folder link like C:\Users\UserName\Some mixed Русский English Adress\

我们有类似的链接http://example.com/folder.zip和绝对文件夹链接C:\Users\UserName\Some mixed Русский English Adress\

if files from zip exist in directory owerrite them.

如果来自 zip 的文件存在于目录 owerrite 中。

using only native windows (xp vista win7 etc) BAT functions and files.

仅使用本机 Windows(xp vista win7 等)BAT 功能和文件。

Could you add code example, please.

你能添加代码示例吗?

采纳答案by wqw

Try this hybrid bat/vbs script

试试这个混合 bat/vbs 脚本

@echo off
 > %temp%\~tmp.vbs echo sUrl = "http://www.unicontsoft.com/file.zip"
>> %temp%\~tmp.vbs echo sFolder = "c:\temp\unzip"
>> %temp%\~tmp.vbs (findstr "'--VBS" "%0" | findstr /v "findstr")
cscript //nologo %temp%\~tmp.vbs
del /q %temp%\~tmp.vbs
goto :eof

'--- figure out temp file & folder
With CreateObject("WScript.Shell")  '--VBS
    sTempFile = .Environment("Process").Item("TEMP") & "\file.zip"  '--VBS 
    sTempFolder = .Environment("Process").Item("TEMP") & "\file.zip.extracted"  '--VBS
End With    '--VBS

'--- download
WiTh CreateObject("MSXML2.XMLHTTP") '--VBS
    .Open "GET", sUrl, false    '--VBS
    .Send() '--VBS
    If .Status = 200 Then   '--VBS
        ResponseBody = .ResponseBody    '--VBS
        With Createobject("Scripting.FileSystemObject") '--VBS
            If .FileExists(sTempFile) Then  '--VBS
                .DeleteFile sTempFile   '--VBS
            End If  '--VBS
        End With    '--VBS
        With CreateObject("ADODB.Stream")   '--VBS
            .Open   '--VBS
            .Type = 1 ' adTypeBinary    '--VBS
            .Write ResponseBody '--VBS
            .Position = 0   '--VBS
            .SaveToFile sTempFile   '--VBS
        End With    '--VBS
    End If  '--VBS
End With    '--VBS

'--- extract
With CreateObject("Scripting.FileSystemObject") '--VBS
    On Error Resume Next    '--VBS
    .CreateFolder sFolder   '--VBS
    .DeleteFolder sTempFolder, True '--VBS
    .CreateFolder sTempFolder   '--VBS
    On Error GoTo 0 '--VBS
    With CreateObject("Shell.Application")  '--VBS
        .NameSpace(sTempFolder).CopyHere .NameSpace(sTempFile).Items    '--VBS
    End With    '--VBS
    .CopyFolder sTempFolder, sFolder, True  '--VBS
    .DeleteFolder sTempFile, True   '--VBS
    .DeleteFile sTempFile, True '--VBS
End With    '--VBS

回答by Thariama

If you really want to use a bat-file you may have a look at: http://www.chami.com/tips/windows/062598W.html

如果你真的想使用 bat 文件,你可以看看:http: //www.chami.com/tips/windows/062598W.html

The batch file will use a command-line-tools called: URL2File

批处理文件将使用名为:URL2File 的命令行工具

EDIT: Your batch file should look something like (you need to have pkunzip or another cmd-line tool (7-zip f.e.) installed for that)

编辑:您的批处理文件应该类似于(您需要为此安装 pkunzip 或其他 cmd-line 工具(7-zip fe))

@echo off
c:
cd\files
URL2File http://www.server.com/file1.zip file1.zip

for %%f in (file1.zip) do pkunzip %%f c:\user\unziped_files\%%f\

回答by Salil

Since Windows 7 includes Powershell IMHO, you could use this powershell script: http://bwain-dump.blogspot.com/2009/01/powershell-script-to-unzip-many-files.html

由于 Windows 7 包含 Powershell 恕我直言,您可以使用此 powershell 脚本:http: //bwain-dump.blogspot.com/2009/01/powershell-script-to-unzip-many-files.html

If not powershell then, I think, there is no native way of doing it. You may go for a zip utility that provides command line eg 7-zip

如果不是powershell,我认为,没有本地的方式来做到这一点。您可以使用提供命令行的 zip 实用程序,例如 7-zip

回答by David Sykes

You can use curlto download the files.

您可以使用curl下载文件。

The manualincludes several examples

手册包括几个例子

回答by mr NAE

download_and_unzip.bat:

download_and_unzip.bat:

powershell -command "Start-BitsTransfer -Source http://example.com/folder.zip -Destination folder.zip"
powershell -command "Expand-Archive folder.zip folder/to/extract"

Download folder.zipto the current dir (or any other - must exist). Extract folder.zipto folder/to/extract(created automatically).

下载folder.zip到当前目录(或任何其他目录 - 必须存在)。提取folder.zipfolder/to/extract(自动创建)。

回答by guest257351

If your PC is like any other windows PC it should have powershell installed. if you are trying to run it from a cmd line or batch script, no problem, you can put the word powershell in front of any command to have it run its through the cmd prompt console! first you should upload the file you want people to be downloading to drop box. then get a sharable link, replace the www.dropbox.com with dl.dropboxusercontent.com to create a direct link that wont require people to click a download button. then make a script like this:

如果您的 PC 与任何其他 Windows PC 一样,则应安装 Powershell。如果您尝试从 cmd 行或批处理脚本运行它,没问题,您可以将 powershell 这个词放在任何命令前面,让它通过 cmd 提示符控制台运行它!首先,您应该将您希望人们下载的文件上传到投递箱。然后获取可共享链接,将 www.dropbox.com 替换为 dl.dropboxusercontent.com 以创建不需要人们单击下载按钮的直接链接。然后制作一个这样的脚本:

start /MAX *drop box link*
timeout 3 >nul
powershell Expand-Archive C:\Users\%USERNAME%\Downloads\*file name* C:/

this will download the file and unzip it to the C:/ drive it is simple and does exactly what it needs to do, does not work for .RAR files. I hope this solves your problem.

这将下载文件并将其解压缩到 C:/ 驱动器它很简单并且完全符合它需要做的事情,不适用于 .RAR 文件。我希望这能解决你的问题。