如何仅使用内置内容(批处理脚本、资源管理器等)在 Windows 上压缩文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2883082/
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-09 07:20:27  来源:igfitidea点击:

How to Zip files on Windows using only what's built-in (batch script, explorer, etc)?

windowsbatch-filezip

提问by James

I am trying to write a script that I can give to user to have it automatically zip certain files and then load them onto an ftp site. Anyone know where I could find information on writing a batch file for auto-zipping files using only what is available to a user running Windows?

我正在尝试编写一个脚本,我可以提供给用户让它自动压缩某些文件,然后将它们加载到 ftp 站点。任何人都知道我在哪里可以找到有关仅使用运行 Windows 的用户可用的内容编写用于自动压缩文件的批处理文件的信息?

采纳答案by User

A better alternative to batch file would be to use a JAR and use Java's java.util.zip package to zip the files. Or how about distributing the bat file with a JAR. People could execute the BAT file and it in turn could run the JAR.

批处理文件的更好替代方法是使用 JAR 并使用 Java 的 java.util.zip 包来压缩文件。或者如何使用 JAR 分发 bat 文件。人们可以执行 BAT 文件,然后它又可以运行 JAR。

回答by Anders

It is possible to work with zip files by using the compressed folder (COM) support and Windows Scripting Host. You would start by creating the Shell.Applicationobject. The FolderItemobject has methods like CopyHere etc that will allow you to manipulate a zip file like a folder. See this pagefor zip and unzip sample code.

可以通过使用压缩文件夹 (COM) 支持和 Windows 脚本宿主来处理 zip 文件。您将从创建Shell.Application对象开始。该FolderItem对象具有类似于CopyHere等方法,将让你像操作的文件夹的zip文件。请参阅此页面以获取压缩和解压缩示例代码。

回答by dj_segfault

Since (AFAIK) Windows doesn't come with a program that can zip up files from the command line. For that matter, it doesn't come with a scriptable ftp program, either (there is an ftp command, but it's interactive).

由于(AFAIK)Windows 没有附带可以从命令行压缩文件的程序。就此而言,它也没有带有可编写脚本的 ftp 程序(有一个 ftp 命令,但它是交互式的)。

Your best bet is to write a program in some compiled language with access to libraries for zipping files and ftp.

最好的办法是用某种编译语言编写一个程序,可以访问用于压缩文件和 ftp 的库。

回答by JYelton

Multiple forums (I can't find an official Microsoft answer) suggest that the zip functionality built into Windows is provided by dlls for which there is no command line argument. For example:

多个论坛(我找不到 Microsoft 的官方答案)表明 Windows 中内置的 zip 功能是由没有命令行参数的 dll 提供的。例如:

The unzipping is a function of zipfldr.dll, so would use regsvr32.exe to invoke it, and as far as I know there are no arguements you can add to it for unzipping via batch file.

解压是 zipfldr.dll 的一个功能,所以会使用 regsvr32.exe 来调用它,据我所知,没有任何争论可以添加到它以通过批处理文件解压。

-- from technologyquestions.com

——来自technologyquestions.com

I would recommend a third party program, of which there are many (which you could provide along with a script): 7zip, winzip, pkzip, etc.

我会推荐第三方程序,其中有很多(您可以随脚本一起提供):7zipwinzippkzip等。

Also you might want to look into VBScriptor WSHas alternatives to batch files.

此外,您可能希望查看VBScriptWSH作为批处理文件的替代品。

回答by tom

Windows has its own zip command. You can put the code below in a backup.bat file and run.

Windows 有自己的 zip 命令。您可以将下面的代码放在一个 backup.bat 文件中并运行。

@echo off
:: variables
set drive=E:\backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y

echo ### Backing up My Documents...
%backupcmd% "e:\Repositories" "%drive%\SVNrepository\Repositories"



e:
cd "%drive%\SVNrepository"

zip -r -S -T -$ -m Repositories.zip Repositories


set filename="..\Repository_bak_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%.zip"
move .\*.zip %filename%
cd E:\backup\

echo Backup Complete!
rem @pause

回答by Andrea Casagrande

By [email protected]

通过 [email protected]

If you want convert a single file into a single zip file you can use gzip.exe

如果要将单个文件转换为单个 zip 文件,可以使用 gzip.exe

Let oProcess.Cwd = oOutput.PATH
    oProcess.SetUsePosition
    oProcess.SetUseSize 0, 0
    oProcess.ExeCmd "gzip.exe gzip -f -q -c -9 " & oSource.Source & " > " & oOutput.Source, True
    If oProcess.Error Then
       Let ErrorMsg = oProcess.ErrorMsg
       Exit Sub
    End If